Advertisement

Unknown rotation behaviour of gameObject while trying to rotate using LocalEulerAngles

Started by November 18, 2016 01:02 AM
0 comments, last by Nypyren 8 years ago

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

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...

This topic is closed to new replies.

Advertisement