Jump to content
  • Advertisement
Sign in to follow this  

Unknown rotation behaviour of gameObject while trying to rotate using LocalEulerAngles

This topic is 754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!