• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by epa720 · Sep 24, 2016 at 11:40 AM · c#rotationobjectquaternionz-axis

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.

T$$anonymous$$s 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 t$$anonymous$$s code?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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. T$$anonymous$$s says... on start (w$$anonymous$$ch 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);
 }
Comment
epa720
jepidoptera
Gilderdale96

People who like this

3 Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image epa720 · Sep 24, 2016 at 12:18 PM 0
Share

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

Or am i doing something wrong?

avatar image b1gry4n epa720 · Sep 24, 2016 at 03:58 PM 0
Share

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

avatar image epa720 · Sep 24, 2016 at 06:30 PM 0
Share

Thank you! This helped me very much.

avatar image Shmiddy · Jan 28, 2018 at 01:32 AM 0
Share

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.

avatar image Gilderdale96 · Aug 31, 2018 at 02:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

246 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Child Object Rotation interpolation problem, 0 Answers

Rotation Perpendicular to Line Segment 1 Answer

Limiting gameObjects rotation on Zaxis giving wierd results 1 Answer

Unity lookat at finger position 3D C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges