- Home /
Question by
prateekkp · Feb 17, 2015 at 02:50 PM ·
movementlerpcheckpoint
Smoothly move player over a collection of checkpoints
I have a collection of vector3 points in an array. Currently my player is experiencing a jerk while moving (after reaching each point in an array it stops and start its movement). Below is my logic for movement. How do i make the movement smooth?
void Update ()
{
if (isMoving)
MoveToTargetPoint ();
}
void MoveToTargetPoint ()
{
// if player has reached the threshold distance of each checkpoint change the target to next checkpoint
if (index <= path.Length - 1 && Vector3.Distance (target, parentT.position) <= 0.3f) {
target = path [index++];
movementTime = 0f;
}
//if player has reached the last checkpoint then stop moving
else if (index > path.Length - 1 && Vector3.Distance (target, parentT.position) <= 0) {
isMoving = false;
}
void FixedUpdate ()
{
if (isMoving) {
movementTime += Time.deltaTime * 0.8f;
parentT.position = Vector3.Lerp (parentT.position, target, movementTime);
}
}
}
Comment
Dont know will it work but u can try :)
parentT.position = Vector3.Lerp (parentT.position, target, movementTime * Time.deltaTime);
Use Vector3.$$anonymous$$oveTowards() ins$$anonymous$$d of Vector3.Lerp().
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, other.position, movementTime);
Note that in this form of $$anonymous$$oveTowards, the first parameter must be the current position of the object, not the starting position of the object.
A more complicated approach is to create a bezier curve from your points (http://en.wikipedia.org/wiki/B%C3%A9zier_curve). If you want to go in this direction you can use iTween or LeanTween.