This site uses strictly necessary cookies. More Information

X- Home /

# How to properly reference a Euler?

I am trying to make a basic camera movement portion of my script though it is working terribly and no Rotation numbers seem match up with what it is displaying in the Inspector and what is being printed out by the console..

I am trying to make the camera have a roll around effect over the top of the object (as seen in the video), though what is happening is I cannot seem to match up the numbers in the Inspector my GameObjects transfrom is set to Rotation: x:180, y:180, z:180 though it states that the First Euler angle in the console I am being told that the Angle is infact 0° not 180°

Video of my Intentions for this code: https://youtu.be/6Zoa-uSiVHY

The aim is persay the cameras rotation is 0,0,0 (y rotation is not used for this purpose) if I push the [W] key the camera tilts forward on the x rotation until it hits its maximum, the maximum should be 45° and the minimum being 0° so If I hit that 45° limit, I now can no longer move forward no matter if Im pressing [W] or not, now I should be able to press [S] to start moving backward back toward 0°. The same goes for Moving Left and right, though the difference is that 0° is the Middle ground, +50° is tilted all the way to the right and -50° is tilted all the way to the left.

So long story short how I i make a Minimum and Maximum that the rotations can play around and be contained in? as the way my code currently works is

Right(+z): Stop at +50° which in Euler Terms is 50° and in Unity Transform terms is 230 Left(-z): (Should) Stop at -50° which in Euler Terms is 310° and in Unity Transfrom Terms is 130 Forward (+x): (Should) Stop at +45° which in Euler Terms is 330° and in Unity Transform terms is 210 Backward(-x): (Should) Stop at 0° which in Euler Terms is 0° and in Unity Transform Terms is 180

Code:

```
void moveLeft()
{
Debug.Log("z Rotation is Currently: " + cameraTrans.eulerAngles.z);
if (cameraTrans.eulerAngles.z < 310 || cameraTrans.eulerAngles.z > 50)
{
cameraTrans.Rotate(0, 0, -Time.deltaTime * moveSpeed);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
void moveRight()
{
Debug.Log("z Rotation is Currently: " + cameraTrans.eulerAngles.z);
if (cameraTrans.eulerAngles.z < 50 || cameraTrans.eulerAngles.z > 310)
{
cameraTrans.Rotate(0, 0, Time.deltaTime * moveSpeed);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
void moveForward()
{
Debug.Log("x Rotation is Currently: " + cameraTrans.eulerAngles.x);
if (cameraTrans.eulerAngles.x > minXRot)
{
cameraTrans.Rotate(-Time.deltaTime * moveSpeed, 0, 0);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
void moveBackward()
{
Debug.Log("x Rotation is Currently: " + cameraTrans.eulerAngles.x);
if (cameraTrans.eulerAngles.x < maxXRot)
{
cameraTrans.Rotate(Time.deltaTime * moveSpeed, 0, 0);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
```

Please understand I have been stuck on this for over 2 days now, trying all different types of numbers variations and code, so my code is super messy now and probably to some does not make any sense (for today I have been ignoring to work on the forward and backward functions.. it is broken beyond beleif and am simply trying to work out it's left and right movement before I bombard myself with 4 different angles to try get right at once.)

Current Code at work Video: https://youtu.be/o6fjq-dJupA

**Answer** by Llama_w_2Ls
·
Dec 21, 2020 at 10:53 AM

I deleted my old answer because I re-did this in my own project and successfully managed to clamp the rotation by setting the rotation of the camera transform using its euler angles. Here's the full script. Hopefully, you can adapt it to make it work for the z-axis as well.

```
using UnityEngine;
public class RotateCamera : MonoBehaviour
{
public Camera cam;
public float degrees = 0;
public float RotationSpeed = 10f;
public float MaxRotationLimit = 20f;
public float MinRotationLimit = -20f;
// Update is called once per frame
void Update()
{
Vector3 EulerRotation = cam.transform.rotation.eulerAngles;
//While holding down 'a', rotate camera to the left
if (Input.GetKey(KeyCode.A))
{
if (degrees < MaxRotationLimit)
{
degrees += Time.deltaTime * RotationSpeed;
cam.transform.rotation = Quaternion.Euler(EulerRotation.x, EulerRotation.y, degrees);
}
}
//While holding down 'd', rotate camera to the right
else if (Input.GetKey(KeyCode.D))
{
if (degrees > MinRotationLimit)
{
degrees -= Time.deltaTime * RotationSpeed;
cam.transform.rotation = Quaternion.Euler(EulerRotation.x, EulerRotation.y, degrees);
}
}
}
}
```

@Fury-Fight3r

Thats awesome it works perfectly, thank you so much for spending the time to help me with this problem.

This is the final code that I got working, I did have to do it in localRotation as when I set y=0 it would set the 'local' rotation to 90 I needed it to be set back to 0 every frame/check as while traversing the Y angle would start gaining a random number and throw the camera at a wierd angle so now y is locked to 0 and x and Z are able to move nicely with the controls.

```
void moveCamera()
{
Vector3 eulerAngles = cameraTrans.rotation.eulerAngles;
if (Input.GetKey(KeyCode.W) && currentXAngle > minXRot)
{
currentXAngle -= Time.deltaTime * current$$anonymous$$oveSpeed;
cameraTrans.localRotation = Quaternion.Euler(currentXAngle, 0, eulerAngles.z);
}
else if (Input.GetKey(KeyCode.S) && currentXAngle < maxXRot)
{
currentXAngle += Time.deltaTime * current$$anonymous$$oveSpeed;
cameraTrans.localRotation = Quaternion.Euler(currentXAngle, 0, eulerAngles.z);
}
else if (Input.GetKey(KeyCode.A) && currentZAngle > minZRot)
{
currentZAngle -= Time.deltaTime * current$$anonymous$$oveSpeed;
cameraTrans.localRotation = Quaternion.Euler(eulerAngles.x, 0, currentZAngle);
}
else if (Input.GetKey(KeyCode.D) && currentZAngle < maxZRot)
{
currentZAngle += Time.deltaTime * current$$anonymous$$oveSpeed;
cameraTrans.localRotation = Quaternion.Euler(eulerAngles.x, 0, currentZAngle);
}
if (Input.GetKey(KeyCode.LeftShift))
{
current$$anonymous$$oveSpeed = fast$$anonymous$$oveSpeed;
}
else
{
current$$anonymous$$oveSpeed = moveSpeed;
}
}
```

Again thanks so much!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Camera rotation behaving strangely 2 Answers

,How to clamp my turret rotation? 1 Answer

Rotation on Android vs. PC 0 Answers

Help with rotation code. 2 Answers

Rotating a model with increments 1 Answer