- Home /
How to prevent Z axis rotation ?
hello, first sorry for my bad english ..
i want to rotate camera around object at X and Y axis only, how could i prevent Z axis rotation and keep it at zero ?
here is my code
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject Target;
public float rotateSpeed = 100f;
public Vector3 offside;
void Start () {
offside = Target.transform.position;
transform.LookAt (offside);
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.DownArrow))
transform.RotateAround (offside, Vector3.left, rotateSpeed * Time.deltaTime);
else if (Input.GetKey (KeyCode.UpArrow))
transform.RotateAround (offside, Vector3.left, -rotateSpeed * Time.deltaTime);
else if (Input.GetKey (KeyCode.LeftArrow))
transform.RotateAround (offside, Vector3.up, -rotateSpeed * Time.deltaTime);
else if (Input.GetKey (KeyCode.RightArrow))
transform.RotateAround (offside, Vector3.up, rotateSpeed * Time.deltaTime);
}
}
please help me with example, i still noob ..
Comment
Best Answer
Answer by Cherno · Mar 19, 2015 at 12:04 PM
Just set teh eulerangle of the desired axis to zero after calling the RotateAround (or any other rotate function).
if (Input.GetKey (KeyCode.DownArrow))
transform.RotateAround (offside, Vector3.left, rotateSpeed * Time.deltaTime);
Quaternion q = transform.rotation;
q.eulerAngles = new Vector3(q.eulerAngles.x, q.eulerAngles.y, 0);
transform.rotation = q;
Your answer
Follow this Question
Related Questions
How to prevent Z axis rotation ? 0 Answers
Lock rotation axis? 4 Answers
Turning character with transform.Rotate 0 Answers
Making an object move in the direction the player is "facing" 2 Answers
transform.lookat() limitation... 1 Answer