Sign in to follow this  
Jois

Unknown rotation behaviour of gameObject while trying to rotate using LocalEulerAngles

Recommended Posts

I am new to game development , I am following a book Unity in action by Manning . This is the code snippet used to rotate a game object (from the book):

public RotationAxes axes = RotationAxes.MouseXandY;
public float sensitivityHorizontal = 9.0f;
public float sensitivityVert = 9.0f;

private float rotationX = 0;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;

// Update is called once per frame
void Update () {
float input = Input.GetAxis ("Mouse X");

//Vertical Rotation
if (axes == RotationAxes.MouseY) {
rotationX-= Input.GetAxis ("Mouse Y") * sensitivityVert;
rotationX = Mathf.Clamp (rotationX, minimumVert, maximumVert);
float rotationY = transform.localEulerAngles.y;

transform.localEulerAngles = new Vector3 (rotationX, rotationY, 0);

}

}

This code actually works but the other way around i.e when my move my mouse upwards the object rotates down and up when I move down . I figured out that it was rotationX-= Input.GetAxis ("Mouse Y") * sensitivityVert; this that was causing the problem so I change it to rotationX+= Input.GetAxis ("Mouse Y") * sensitivityVert; and now it rotates really strangely ! I have made sure that it only rotates in vertical axis but it's rotating in all the directions i.e even in horizontal and z axis. Can somebody please tell me where I am going wrong? ThankYou

Share this post


Link to post
Share on other sites

There are other things happening here which we can't see from the code you posted.

 

For example:  What changes the 'axes' variable to allow that if statement to function at all?  Your scene/prefab object instance?  If nothing's changing it, the code you posted should do literally nothing at all...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this