Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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 this but not work. I also tried to find some old question in unity-answers but nothing. 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
Add comment
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
3

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

Well, try this, 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
Add comment · 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
-1

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

1) Check out this function.

2) Search old questions.

3) Look in MouseLook script in Standard Assets, it has this implemented

Comment
Add comment · 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 ($$anonymous$$athf.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.. :(

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

daily up ...

Show more comments
avatar image
0

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
Add comment · 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.

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

Following another object's position/rotation like parent/child relationship? 4 Answers

Smooth Rotation Help 1 Answer

I want to restrict the rotation with the following Script,I want to restrict tranform.Rotation 0 Answers

How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers

Limiting rotation of object, specifically using scroll wheel 2 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