Apply/Update Position

Flamehead

Member
I'm trying to build an ability that will have an effect on the player's position. So I've started with a super simple example to get things working and so far it's not working. I placed the character at world position (0, 0, 0) and wrote the following code grabbing the m_Transform.position from the Ability namespace. I then created a Transform for the target and then placed an empty game object in the scene to provide the target transform.

I've placed the lerp logic inside both the ApplyPosition and UpdatePosition, never them at the same time, but neither would produce player transform lerp.

Firstly, what logic am I missing to lerp the player to the target?

Secondly, would this go into Apply Position or Update Position?

Thanks
 

Attachments

  • Screenshot 2022-07-16 152725.jpg
    Screenshot 2022-07-16 152725.jpg
    105.8 KB · Views: 5
so your probably goofing up on the code for the lerp position. your lerping from Vector3.zero, to Target.position, by a very very small amount (deltaTime), so every frame your character is moving, but only a tiny tiny amount, and your never actually increasing the value, so your character is staying in that same position every frame.

Try something like m_Transform.position = Vector3.Lerp(m_Transform.position, target.position, lerpSpeed * Time.deltaTime). notice the lerp speed variable, which will increase or decrease the speed of the lerp, and also notice we are lerping between the current position of the characters transform, and the target position, but then we store that position INTO the m_transform position, so that the next time we call this function, the character has in fact moved (which is the problem with your current implementation, Vector3.zero is never moving).

Also note that UpdatePosition is called first, ApplyPosition is called last, and collisions, various user input forces, and external forces(such as gravity) will still affect the player during these functions.
 
so your probably goofing up on the code for the lerp position. your lerping from Vector3.zero, to Target.position, by a very very small amount (deltaTime), so every frame your character is moving, but only a tiny tiny amount, and your never actually increasing the value, so your character is staying in that same position every frame.

Try something like m_Transform.position = Vector3.Lerp(m_Transform.position, target.position, lerpSpeed * Time.deltaTime). notice the lerp speed variable, which will increase or decrease the speed of the lerp, and also notice we are lerping between the current position of the characters transform, and the target position, but then we store that position INTO the m_transform position, so that the next time we call this function, the character has in fact moved (which is the problem with your current implementation, Vector3.zero is never moving).

Also note that UpdatePosition is called first, ApplyPosition is called last, and collisions, various user input forces, and external forces(such as gravity) will still affect the player during these functions.
Thanks buddy this was helpful! Gonna give it another go with this.
 
@TimTheTerrible definitely has a good recommendation. The other thing to keep in mind is that the transform position should not be set directly - you should instead set the ability motor. The drive ability has a good example but in essence it's:

Code:
            m_CharacterLocomotion.AbilityMotor = deltaPosition

This is within the update position since it's done before collision detection.
 
@TimTheTerrible definitely has a good recommendation. The other thing to keep in mind is that the transform position should not be set directly - you should instead set the ability motor. The drive ability has a good example but in essence it's:

Code:
            m_CharacterLocomotion.AbilityMotor = deltaPosition

This is within the update position since it's done before collision detection.
Thanks for the reply. Can you explain the differences and uses for AbilityMotor, MoveDirection (Which is a directional vector but not real sure how to use it with these other Vector3's), MotorThrottle, and any others that have to do with position? I've looked through the documentation and scripts but it is really hard to get a general grasp of what each of them is doing and how to use them. It would be helpful to have a reference to understand these better.
 
AbilityMotor should be used within UpdatePoistion. This gets added to the MotorThrottle which then gets added to the MoveDirection. The MoveDirection should be used within ApplyPosition. I know that this is a lot to keep track of and I've simplified it in version 3.
 
Top