Move object towards a moving point C#
I have a point attached to my third person vehicle that I want the camera to float to. Right now I calculate the direction vector, and then just move towards the direction vector.
public GameObject positionObject;
public float cameraMoveSpeed = 1;
void Update () {
Vector3 movementDirection = positionObject.transform.position - this.transform.position;
this.transform.position += movementDirection * (Time.deltaTime * cameraMoveSpeed);
}
This produces what I want but it jerks the camera around at a fast enough speed. I looked into MoveTowards but it causes the same kind of effect.
float step = cameraMoveSpeed * Time.deltaTime; //maximum step
transform.position = Vector3.MoveTowards(transform.position, positionObject.transform.position, step);
And I know Slerp is for moving between point A and point B smoothly, but if point B moves thats how you get issues. plus instead of a step it's a percentage of completion.
Wo the question is what's the best way to move an object to a specific point that is moving?
Answer by HenryStrattonFW · Nov 06, 2015 at 11:26 AM
I would just do the lerp yourself, that way you can impose a speed limit on it.
Calculate the position difference between A and B. you can then lerp towards it simply by moving that vector times by some fractional value, lets for arguments sake say 0.5f; Now how to add your speed limit?
Instead of directly moving by this value take that value and clamp it by your max movement speed, and that should then move you towards the target, easing out of the move as it approaches. You can also do easing in but that's a little more tricky.