- Home /
Question by
xnik_ · Aug 02, 2020 at 10:27 AM ·
gameobjectlerp
GameObject stuttering on the same spot when trying to lerp
Hello guys,
Im currently trying to test out the Vector3 Lerp function and want my GameObject to move between two Vector3 positions. At the moment the GameObject doesn't want to move properly, it keeps shaking on the same spot the whole time, maybe you know what I'm doing wrong... Here's the code I tried:
public static GameObject CreateSphere()
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f);
return sphere;
}
//This function is getting called in the Update() function.
public static void MoveSphere(GameObject gameObj)
{
gameObj.transform.position = Vector3.Lerp(new Vector3(0.5f, 0.1f, -0.5f), new Vector3(0.5f, 0.1f, 1.5f), 1.10F * Time.deltaTime);
}
Maybe you can help me out, I'm kind of confused.
Comment
Best Answer
Answer by PlayCreatively · Aug 02, 2020 at 01:56 PM
The last value in lerp decides the position, 0f = vectorA, 1f = vectorB and then it interpolates in between. Lerp usually clamps at 1f but sometimes you get the option of having it unclamped. The problem is that you're not counting towards 1f. Do this elapsedTime += Time.time
Vector3.lerp(vectA, vectB, elapsedTime/lerpTime)