I’m hoping this is the right section to post my question in! I’m having an issue that I haven’t been able to solve for the last couple of days. I’m working on a game that moves my player from position to position using Vector3.MoveTowards and updating the player’s targetPosition about every 1 unit moved. The problem is, this is giving very jerky/stuttery results when I update the player’s targetPosition.
This is in my Update loop. I check if the player reaches it’s destination, if it did, I fire an event which in turn updates the user’s _targetPosition. Rinse and repeat. Any idea what could be going on? I noticed that if I lowered the velocity it fixes it but I don’t want the player to move that slow. Thanks in advance!
I use it and it’s smooth, but I don’t use any physics. It’s just moving an empty with a model parented to it. I haven’t tried it with a rigidbody or anything. I’m sure it would need to be set to kinematic at the least.
And how is your target position generated? I would bet the problem lies there.
If you want for example constant speed movement, then every time you change target position it must be in same distance from the player’s current position.
Then it could be that if you are moving your player by let’s say 1 unit (third argument of Vector3.MoveTowards) and the total distance between source and target position is for example 9.1 unit, then the tenth movement will be not 1 unit, but just 0.1 unit and this will cause jerky movement.
In this case when reaching target position you will have to check if there is some “missing remainder” of movement and fix it. Maybe the calculations will be easier with Vector3.Lerp, the method I originaly thought you are using.
Great suggestion but I have been logging out the position that it changes to and it never has the same position twice.
Hey Fido, I did notice that the delta between the player’s position were not always the same and almost was a range, I think you might be onto something. I break up my screen into a grid and it’s moving in between cells so each movement should always be the same distance.
Is there a way to make it so each delta of the player position is the same?
EDIT:
I could just make sure the distance is divisible by the speed, but the problem is I actual want to vary the velocity for acceleration and deceleration.
No, the code should be ok as is. Only if there is distanceReminder greater than zero (point something) the player must travel this piece of distance in another section for movement to look smooth.
It doesn’t seem to work, what should nextTargetPosition be? I assume you meant it to be the spot the player should move to after it reaches _targetPosition (which is just increasing the Y by 1 currently)?
Also, if this is the case, wouldn’t it almost always ignore the first MoveTowards? One more thing, isn’t the third argument for MoveTowards speed, not relating to distance. I would the think the maxDistance would be equal to the distance between the player’s current position and the targetPosition upon updating it. Would it be helpful if I attached a simple test project?
Really appreciate your guys’ help on this. Attached is the most basic example of the problem, hope it’s clear. Please let me know if you have any questions.
All that does is lerp the camera position to always keep the player in view. That was just a helper script so you could see the issue right in the game view. It shouldn’t affect the player position at all.
Can I ask you about the goal of your design? Do you want the camera to always be approx a unit behind, (and catching up?). Do you want some other close follow?