• 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
3
Question by $$anonymous$$ · Mar 10, 2014 at 12:46 AM · rotationclampfirst-person

How do I clamp my rotation?

The player is in first-person, as humanoid model. To other players, when they look up, their torso rotates slightly vertically. It works the same way when I look down. T$$anonymous$$s can also be observed in the game "Rust", by Garry Newman. Here is my script, that is attatched to the spine.

 var speed : float = 5;
 var torsoRotation : float = 0;
 
 function Update() {
     torsoRotation = Input.GetAxis("Mouse Y") * speed;
     Mathf.Clamp(torsoRotation, -90, 90);
     transform.Rotate(0, 0, -torsoRotation, Space.Self);
 }

It works, except for the clamp. I can infinitely roll my upper body. I am suspicious at the fact that in the inspector, my torsoRotation is equal to 0. When I move the mouse up or down fairly quickly, it changes to about 0.5 or 1. When I stop using the mouse, it drifts back to 0. What is happening, and how can I fix it?

Comment
Add comment · Show 4
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 SirCrazyNugget · Mar 10, 2014 at 01:23 AM 0
Share
avatar image $$anonymous$$ · Mar 10, 2014 at 01:30 AM 0
Share
avatar image You! · Mar 10, 2014 at 05:10 AM 0
Share
avatar image $$anonymous$$ · Mar 10, 2014 at 12:39 PM 0
Share

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by whydoidoit · Mar 10, 2014 at 12:44 PM

Try t$$anonymous$$s:

  function Update() {
 
    torsoRotation = Input.GetAxis("Mouse Y") * speed;
     transform.Rotate(0, 0, -torsoRotation, Space.Self);
     transform.eulerAngles.y = Mathf.Clamp(transform.eulerAngles.y, -90, 90);

 }

Clamping just returns a value that you have to set into the rotation yourself. You need t$$anonymous$$s after you've applied your rotation. However, t$$anonymous$$s may still cause strange flicking, if so you might need to use a cleverer clamping function that understands angles.

Comment
Add comment · Show 6 · 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 $$anonymous$$ · Mar 10, 2014 at 01:46 PM 0
Share
avatar image whydoidoit · Mar 10, 2014 at 02:27 PM 3
Share
avatar image $$anonymous$$ · Mar 10, 2014 at 03:02 PM 0
Share
avatar image whydoidoit · Mar 10, 2014 at 03:17 PM 0
Share
avatar image $$anonymous$$ · Mar 10, 2014 at 04:07 PM 0
Share
Show more comments
avatar image
15

Answer by DerDicke · Jan 16, 2018 at 08:42 PM

Old thread, but problem was never solved. Best solution for me was:

     float ClampAngle(float angle, float from, float to)
     {
         // accepts e.g. -80, 80
         if (angle < 0f) angle = 360 + angle;
         if (angle > 180f) return Mathf.Max(angle, 360+from);
         return Mathf.Min(angle, to);
     }
 
 
     void RotateInFrame()
     {
         if (!Input.GetMouseButton(1)) return; // RMB down
 
 
         float mx = Input.GetAxis("Mouse X") * Time.deltaTime * rotSpeed;
         float my = Input.GetAxis("Mouse Y") * Time.deltaTime * rotSpeed;
 
         Vector3 rot = transform.rotation.eulerAngles + new Vector3(-my, mx, 0f); //use local if your char is not always oriented Vector3.up
         rot.x = ClampAngle(rot.x, -60f, 60f);
         
         transform.eulerAngles = rot;
     }


Good luck!

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 jimmyciawly · Jan 26, 2019 at 04:33 PM 0
Share
avatar image Jack_Veritac · Mar 17, 2022 at 07:21 PM 0
Share
avatar image
0

Answer by Lairex59 · May 02, 2020 at 12:33 PM

Here it's explained well https://www.youtube.com/watch?v=JeF0hoJWLz4

Comment
Add comment · 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
0

Answer by Amyr · Dec 25, 2021 at 03:22 PM

T$$anonymous$$s worked fine for me. But since I am moving in delta steps, I had to add t$$anonymous$$s in order to clamp it right:

 float deltaAngle = context.ReadValue<float>();
 float clampAngle = ClampAngle(target.eulerAngles.x + deltaAngle);
 deltaAngle = clampAngle - target.eulerAngles.x;
 
 target.Rotate(target.right, deltaAngle, Space.World);

Notice I am using a new input system event call to look up and down using the mouse Y axis. Also, I made the ClampAngle function a private member of my class, and removed the min and max parameters, since those are user defined parameters, and I made them members of the same class. Then I rotate the target Transform along its right axis.

Work as a charm!

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

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

31 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

Related Questions

Is anyone able to fix my code? 3 Answers

Edited mouselook script rotation clamp not working 0 Answers

Clamp gyro rotation 0 Answers

clamp rotation on rigidbody 1 Answer

How to predict position of object before transform.translate will apply? 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