• 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 /
avatar image
Question by PaxStyle · May 31, 2014 at 09:34 AM · rotationtransformlimit

Transform.rotate limits

Hey. Ther's a way to limit the angles of rotation with transform.rotate? Can someone suggest me an example? I tried t$$anonymous$$s but not work. I also tried to find some old question in unity-answers but not$$anonymous$$ng. Thanks a lot.

 public GameObject[] arm;
     
     float speedUp;
     float speedDown;
     Vector3 StartPosition;
 
 
     void Awake()
     {
         speedUp = 80f; 
         speedDown = 60f;
     }
     
     // Update is called once per frame
     void Update () {
 
 
         
         //go up
         if (Input.GetKey (KeyCode.Z)) {
             
             arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speedUp);
 
             if(transform.Rotate > 50) //rotation limits 
             {
                 transform.Rotate = 50;
                 
             }
             
         }
         
         //go down
         if (Input.GetKey (KeyCode.X)) {
             
             arm[0].transform.Rotate(-Vector3.right * Time.deltaTime*speedDown);
 
             
         }
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by cl0n · Nov 03, 2016 at 12:50 PM

Well, try t$$anonymous$$s, It's easy to understand as well.

(Declaration) public float rotationZ; public float sensitivityZ = 5;

         void Start()
         {
                 transform.rotation = Quaternion.identity;
         }
 
         void Update()
         {
                 Debug.Log(transform.rotation.eulerAngles.z);
                 LockedRotation();
         }
 
         void LockedRotation()
         {
                 rotationZ += Input.GetAxis("Mouse ScrollWheel") * sensitivityZ * 10;
                 rotationZ = Mathf.Clamp(rotationZ, -45.0f, 45.0f);
 
                 transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ);
         }
Comment
omnivore
OpperDarwin
shivamahlawat34

People who like this

3 Show 1 · 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 OpperDarwin · Dec 18, 2017 at 02:15 PM 0
Share

Thank you!

avatar image

Answer by Lovrenc · May 31, 2014 at 10:03 AM

1) Check out t$$anonymous$$s function.

2) Search old questions.

3) Look in MouseLook script in Standard Assets, it has t$$anonymous$$s implemented

Comment
Fredex8

People who like this

-1 Show 7 · 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 PaxStyle · May 31, 2014 at 03:41 PM 0
Share

I applied the mathf.Clamp to my script but unity give me error.

 arm[0].transform.Rotate(Vector3.right (Mathf.Clamp(10, 1, 3) * Time.deltaTime*speedUp);
avatar image PaxStyle · Jun 01, 2014 at 04:02 PM 0
Share

but for me is very difficult because is all in english! So I try to understand from examples. And i don't need to lear all, i use the scrips very little

avatar image PaxStyle · Jun 02, 2014 at 11:51 AM 0
Share

Someone know an example of mathf.Clamp that work with transform.rotate?

avatar image PaxStyle · Jun 14, 2014 at 08:37 AM 0
Share

Uff i tried this way but give me error.. :(

 Mathf.Clamp(arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speedUp), minRotation, maxRotation);
avatar image PaxStyle · Jun 15, 2014 at 09:34 AM 0
Share

daily up ...

Show more comments
avatar image

Answer by Khaeops · Aug 28, 2015 at 11:13 AM

I haven't tried it yet, but I believe:

transform.localRotation.x = Mathf.Clamp(transform.localRotation.x, Max, Min);

where the transform.localRotation.x in the brackets is your current value, then it restricts it between your max and min values. You can set them with floats or integers.

Correct me if I'm wrong though, I myself am trying to get a local rotation restriction going too :P

Comment

People who like this

0 Show 2 · 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 khamosh132 · May 19, 2016 at 07:37 AM 0
Share

Well for starters you cannot individually set a single variable of any property(position, scale, rotation) of transform. In order to change only one variable you must set the whole property by keeping the other variables same.

avatar image Khaeops khamosh132 · Dec 19, 2017 at 10:59 PM 0
Share

At the time I posted my answer, I was aware that you could do it in Javascript (Unityscript?). By now, I use C#, and yes, you do need to modify the property via another temporary variable.

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

25 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

Related Questions

How do i rotate an object "Once" 1 Answer

How can i rotate an object at a certain point and stop 1 Answer

accessing an array of rigidbodies at once? c# 2 Answers

I'm interpolating an object between positions and rotations. Now I need to make sure it happens in front of the camera. 1 Answer

Player not facing the mouse correctly 1 Answer


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