- Home /
camera movement in world space, but when the rotated on y axis
I am trying to make a camera that moves exactly like the scene view camera. The camera in the scene view only moves in the x-z plane no matter what the rotation is. meaning, if you change the rotation it doesn't go right through the planar landscape in a game. But with current code. When the point the point the camera into the landscape and I press forward it would go right through it. would like it move only x and z axis.
function Start () {
}
var speed = 5.0f;
var movement = Vector3.zero;
function Update () {
movement.z = Input.GetAxis("Vertical");
movement.x = Input.GetAxis("Horizontal");
transform.Translate(movement * speed * Time.deltaTime, Space.self);
}
Answer by Wolfram · Jun 02, 2012 at 12:28 AM
One solution would be to store the transform's y position, and reset it to that value after you've done your translation:
...
var originalHeight=transform.position.y;
transform.Translate(movement * speed * Time.deltaTime, Space.self);
transform.position.y=originalHeight;
Note that your forward/backward movement will become very slow if you are looking way up/down. To compensate for this, scale your movement.z by the factor determined by that angle:
// place your two movement assignments here
var planarForward=transform.forward;
planarForward.y=0;
movement.z /= planarForward.magnitude;
EDIT: Note the shift in height is exactly movement.z*transform.forward.y, so instead of the originalHeight method you could just use:
movement.y=-movement.z*transform.forward.y;
Thank you for the idea. I couldn't get your script to work, but I understood what you were telling.
Your answer
Follow this Question
Related Questions
Camera setup question 2 Answers
Character movement when I switch cameras. 0 Answers
I need help with TPS controls! 0 Answers
How to make camera position relative to a specific target. 1 Answer
Camera Orbit for spaceship 1 Answer