Move Camera towards Object

Hello everyone…

I just want my camera to move towards my player.

For that i tried :

gameObject.transform.localPosition = Vector3.MoveTowards( gameObject.transform.localPosition ,target.transform.localPosition , 2);

But i just want to move my camera on x-z axis. Y position should not be changed.

Which method shall i use ?

Thanks…

This isn't much of an answer but you can just change Y value for your vector.

–

Exclude Y values by setting them to the same value when putting in MoveTowards method and set it back to original value. A workaround but easy.

–

1 Answer

1

Just make a new Vector3 and only give it the x and z values of the target

 gameObject.transform.localPosition = Vector3.MoveTowards( gameObject.transform.localPosition , new Vector3(target.transform.localPosition.x,0f, target.transform.localPosition.z), 2);

or

 gameObject.transform.localPosition = Vector3.MoveTowards( gameObject.transform.localPosition , new Vector3(target.transform.localPosition.x,gameObject.transform.localPosition.y, target.transform.localPosition.z), 2);

Also you should be multiplying maxDistance input by Time.DeltaTime that way it will move at the same rate regardless of frame-rate

–