- Home /
MoveTowards won't translate on the y axis and lerp leaves character jittering in mid-air
Im trying to move my player character to a trigger object. I basically duplicated the c# examples for both Vector3.MoveTowards and Vector3.Lerp but neither seems to be working neatly. MoveTowards bounces my character along on the x and z axes but then just leaves it bouncing underneath my target, and Lerp interpolates it on all three axes but seems to have trouble getting to the target and after getting within 1 point slowly bounces it closer all told leaving the player in the air for several seconds.
IEnumerator Mover(Vector3 target)
{
/*
// MoveTowards Test
CharacterMotor characterMotor;
characterMotor = GetComponent<CharacterMotor>();
characterMotor.SetControllable(false);
while (Vector3.Distance(transform.position, target) > 0.25)
{
Debug.Log(Vector3.Distance(transform.position, target));
float step = 1.0f * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, 1.0f*Time.deltaTime);
yield return null;
}
characterMotor.SetControllable(true);
*/
// Lerp Test
CharacterMotor characterMotor;
characterMotor = GetComponent<CharacterMotor>();
characterMotor.SetControllable(false);
float speed = 0.5f;
float startTime;
float journeyLength;
Transform starter = transform;
startTime = Time.time;
journeyLength = Vector3.Distance(transform.position, ledge.transform.position);
while(Vector3.Distance(transform.position, target) > 0.25)
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp (transform.position,target,fracJourney);
yield return null;
}
characterMotor.SetControllable(true);
}
I think the character jitters due to the character controller trying to control your character and you on one hand trying to move it.
I would suggest that when you are moving your character , disable the character control(as you are already doing so). But once you are done with moving your character, in the next frame(next Update() ), enable your controller again. That way it will give time to your $$anonymous$$ove function to do the task and your character control won't try to take over the physics.
One more thing, I would suggest that you do only one $$anonymous$$oveTowards or Lerp in a single frame, rather than while loop. And keep doing it every update or frame until it reaches your condition ($$anonymous$$imum distance), then you can choose not to perform the $$anonymous$$oveTowards or Lerp operation and ins$$anonymous$$d can enable your controller in that frame.
See if this logic works.
sumeet: the while loop above is doing one move per update. It's in a coroutine with the standard yield return null;
.
But, yes, $$anonymous$$oveTowards is probably working perfectly, but there's someone else moving it at the same time. I'd guess a rigidbody (which is why the bouncing underneath?) If so, set is$$anonymous$$inematic
?
So the lerp wasnt working because I used transform.position as the starting point for the lerp on every frame meaning it was always trying to move closer to the goal but never getting there (I think). So converted starter to a Vector3 variable (because I had it in there but was never using it as a transform) and set it to my starting location and swapped it into the lerp command.
Thanks for looking at it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133350im_/https://answers.unity.com/themes/thub/images/avi.jpg)