Need help getting the camera to rotate up and down

Updated on November 5, 2017 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on November 4, 2017

Hello!

This is my first post so please understand if I’m placing it in the wrong section.

I have just recently started learning programming(so the terminology is VERY scarce) and am trying to get a camera to rotate up and down with the mouse in place. I followed Brackeys “How to make RTS Camera Movement in Unity” and thought maybe it would work if I simply made simple changes to the code(like changing position to rotation). This was not the case however and it seems I have to get into some advanced stuff… So any help?

  • Liked by
Reply
0 on November 4, 2017

As you are new to Unity you should check these tutorials too:

https://unity3d.com/de/learn/tutorials

They are very helpful and do the basics you should know about.

 

About your camera movement you can try this:

public float mouseSpeedX;
public float mouseSpeedY;
public GameObject camera;
 
void Update()
{
//Sight/Mouse Movement
float xRot = camera.transform.rotation.eulerAngles.x - Input.GetAxis("Mouse Y") * mouseSpeedY * Time.deltaTime;
float yRot = camera.transform.rotation.eulerAngles.y + Input.GetAxis("Mouse X") * mouseSpeedX * Time.deltaTime;
camera.transform.rotation = Quaternion.Euler(xRot, yRot, 0);
}

 

  • Liked by
Reply
Cancel
2 on November 5, 2017

Wow! That did it! Thank you!

Now I’m trying to set a limit to how high and how low if should be able to rotate as in the last video but it doesn’t seem to work the same way when it’s about rotation… So a bit more hep would be greatly appreciated.

Helpful
on November 5, 2017

Before setting xRot and yRot in the Quaternion.Euler() you can use a min() and max() function to set a limit for the max and lowest value.

Helpful
on November 5, 2017

I know what you mean:

public float mouseSpeedX, mouseSpeedY;
public float xMax, yMax;
public GameObject camera;
  
void Update()
{
//Sight/Mouse Movement
float xRot = camera.transform.rotation.eulerAngles.x - Input.GetAxis("Mouse Y") * mouseSpeedY * Time.deltaTime;
float yRot = camera.transform.rotation.eulerAngles.y + Input.GetAxis("Mouse X") * mouseSpeedX * Time.deltaTime;
 xRot = Mathf.Clamp(xRot, -xMax, xMax);
 yRot = Mathf.Clamp(yRot, -yMax, yMax);
camera.transform.rotation = Quaternion.Euler(xRot, yRot, 0);
}

 

But curiously it shows then a strange behaviour on my game. If I reach the 0 rotation of the x or y axis it jumps back to the positive maximum of the respective axis.

I can’t go over to the negative rotation values.

 

Show more replies
  • Liked by
Reply
Cancel