- Home /
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 $$anonymous$$oveTowards method and set it back to original value. A workaround but easy.
Answer by JanusMirith · Oct 14, 2014 at 06:54 AM
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
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Unity FBX to Prefab Position reseting 1 Answer
Fly-Through using Unity and Oculus Rift 2 Answers
How to use vuforia AR Camera for Zoom 1 Answer
Orthographic Camera 2 Answers