- Home /
How to prevent Z axis rotation ?
hi, i want to rotate camera with arrowkey around object at X and Y only, how to prevent Z axis rotation / keep Z axis at zero ? i have looked around on web, but i still dont understand ..
here 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);
}
}
i'm still noob at programming, so please answers with example ... thanks
You don't need to do this in code. Just slap a gravityless kinematic rigidbody on it and freeze its Z rotation.
Alternatively, if you must.
float zeroedrotation = 0f; //Set this to whatever you want.
//after the other rotations
transform.rotation.eulerAngles = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, zeroedrotation);
@Nymisu, it doesnt work .. its throw me some error .. but i have found the answer, i just add this code after previous code
Quaternion q = transform.rotation;
q.eulerAngles = new Vector3(q.eulerAngles.x, q.eulerAngles.y, 0);
transform.rotation = q;
its thanks to @Cherno, btw sorry for double question .. i not seeing the notification up there and thanks for answering.
Your answer
Follow this Question
Related Questions
How to prevent Z axis rotation ? 1 Answer
LookAt once 0 Answers
problem with vector3.right and up 2 Answers
Vector3 precision issue? 2 Answers
How to use x axis for transform.LookAt 3 Answers