# How to limit only one axis rotation? c#

I'm trying to limit object rotation. I don't want to affect x and y rotation. Only to z-axis rotation.

This code limits z-axis rotation to 30 degrees but it affects to x and y.

```
using UnityEngine;
using System.Collections;
public class LimitRotation : MonoBehaviour
{
float z;
float x;
float y;
void Start()
{
z = transform.eulerAngles.z;
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
}
void Update()
{
z -= Input.GetAxis("Horizontal") * 30 * Time.deltaTime;
z = Mathf.Clamp(z, -30, 30);
transform.rotation = Quaternion.Euler(x, y, z);
}
}
```

How i should edit this code?

**Answer** by b1gry4n
·
Sep 24, 2016 at 11:51 AM

Setting the euler to 0 in the update function (every frame), the object will appear to not rotate and will stay locked at the "rotation" value of 0.

Setting the euler to the transforms euler angle every frame simply says "do what you were doing". We want it to "do what it was doing" except we want to change 1 specific value, the z axis.

In your original code you were setting xyz to the transforms eulers in the START function. This says... on start (which is only called once) the xyz values are equal to whatever they were set to at the start of play mode. I am guessing it was at 0,0,0. So now...x = 0, y = 0, z = 0. By setting the xyz in the update now... you were saying transform.eulerAngles.x = 0, transform.eulerAngles.y = 0, transform.eulerAngles.z = 0

By setting the euler angles to what they currently are in the update function, it "frees up" those values to be able to change... freely.

TLDR : update the direct values of any euler you dont want to change, you cant store the value in start and reference it later because it will be changing. Move the x/y/z from start to update

I'm trying to limit object rotation. I don't want to affect x and y rotation. Only to z-axis rotation.

```
void Update()
{
float h = Input.GetAxis("Horizontal") * 30 * Time.deltaTime;
z = transform.eulerAngles.z;
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
Vector3 desiredRot = new Vector3(x, y, z + h);
if (desiredRot.z > 30)
{
desiredRot = new Vector3(x, y, 30);
}
else if (desiredRot.z < -30)
{
desiredRot = new Vector3(x, y, -30);
}
transform.rotation = Quaternion.Euler(desiredRot);
}
```

This does still freeze x and y rotation. I want them to change freely.

Or am i doing something wrong?

Misread the question/skimmed over it. Updated the answer.

Thank you so much! I know this is an old post, but what I was missing was setting the rotation of the object via Quaternion.Euler(desiredRot). I'm now able to lock the rotation for the puzzle in my game after days of trying different methods. At least all these hours spent led to a better understanding of Unity's rotation systems. What a relief.

Great answer! I've been struggling to move an object on certain axis for a few hours now and this definitely helped. Thanks!

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

How to rotate object back and forth from one rotation to another? 2 Answers

Rotating Object Smoothly Exactly 90 degrees on click (for infinite rotations) 0 Answers

Rotation Perpendicular to Line Segment 1 Answer

How to set a max rotation angle 1 Answer

Limiting gameObjects rotation on Zaxis giving wierd results 1 Answer