• 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 JustinC · Jan 07, 2015 at 05:28 PM · mathf.clamp

Mathf.Clamp help

I am trying to get an object that rotates on the X axis by input of mouse Y and stay with in a certain rotation area, this is what I have so far

 public float Min = 15f;
 public float Max = 10f;

 void Update () 
 {
     transform.Rotate  ((Mathf.Clamp(Input.GetAxis ("Mouse Y"), Min, Max)), 0, 0);
 }


but all that happens is that it spins in circles non stop. What did I do wrong here. Any help is greatly appreciated.

Comment
Add comment · Show 2
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 Scribe · Jan 07, 2015 at 05:34 PM 0
Share

Rotate is a method that rotates a transform by a certain amount on an axis every time it is called, the clamping that you are doing stops it rotating faster or slower than $$anonymous$$/max degrees each frame, not stopping it from rotating past those degrees.

Get the standard assets package and look at the $$anonymous$$ouseLook script included in there as that does the clamping well. Also checkout transform.rotation.eulerAngles!

avatar image NoseKills · Jan 07, 2015 at 05:43 PM 0
Share

What @Scribe said

...and also your $$anonymous$$AX is less than your $$anonymous$$IN which results in you getting either 10 or 15 as the rotation value every time and nothing in between them.

2 Replies

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

Answer by jpthek9 · Jan 07, 2015 at 06:01 PM

Often a good procedure to follow in a bug is to decide and conquer. See what's the problem. First, you can try transform.Rotate(x) and replace x with something you know is independent and not bugged. If i.e. transform.rotate(5) works, then you know it's not that part.

Next, you can try the Mathf.clamp by itself and see what value it turns up.

print(Mathf.Clamp(Input.GetAxis ("Mouse Y"), Min, Max));

You will see that it will turn up as 10, because that's what clamp does. If your input is smaller than Min, and often mouse input is, then it gets clamped by Min.

If you want to clamp the rotation of your object, you're going to have to clamp the rotation of the object - not the input.

 //Add this after your rotation
 transform.EulerAngles.y = Mathf.clamp (transform.EulerAngles.y, Min, Max);
Comment
Add comment · Show 10 · 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 JustinC · Jan 07, 2015 at 06:42 PM 0
Share

so I changed it to read

 public float $$anonymous$$in = -10f;
     public float $$anonymous$$ax = 15f;
 
     void Update () 
     {
         float $$anonymous$$ouseRot = Input.GetAxis ("$$anonymous$$ouse Y");
         transform.Rotate ($$anonymous$$ouseRot,0,0);
         transform.eulerAngles.y = $$anonymous$$athf.Clamp (transform.EulerAngles.y, $$anonymous$$in, $$anonymous$$ax);
 
     }

it will rotate with the mouse without the eulerAngles line, but when I add it to limit the rotation i get an error:

'UnityEngine.Transform' does not contain a definition for 'EulerAngles' and no extension method 'EulerAngles' accepting a first argument of type 'UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)

am I looking at this completely wrong?

avatar image InvincibleCat · Jan 07, 2015 at 06:46 PM 0
Share

you need to store you eulerAngles first:

 Vector3 eulerAngles = transform.eulerAngles;
 eulerAngles.y = $$anonymous$$athf.Clamp (eulerAngles.y, $$anonymous$$in, $$anonymous$$ax);
 transform.eulerAngles = eulerAngles;
avatar image jpthek9 · Jan 07, 2015 at 06:50 PM 0
Share

@InvincibleCat I'm not 100% sure but I think you can directly modify Euler angles. @Justin That's exactly right (if what I think is write). If you can't directly modify Euler angles, do what InvincibleCat posted to clamp your rotation. Also note: Angles have wraparounds. You should use something like this:

 while (transform.eulerAngles.y > 180){
 transform.eulerAngles.y -= 360;
 }
 while (transform.eulerAngles.y < -180){
 transform.eulerAngles.y += 360;
 }

and then you can properly clamp your angle.

avatar image InvincibleCat · Jan 07, 2015 at 06:51 PM 0
Share

No you can't modify transform.eulerAngles.y directly :)

avatar image jpthek9 · Jan 07, 2015 at 07:42 PM 0
Share

Oh, dang :C I wish Unity would automatically do the temporary variable thing for you but oh well. I agree with you on the modulo. It's much more efficient but modulo's can't be negative. For the system to work, you'll have to add a little something...

 int Charge = 1;
 if (angle < 0)
 {
 Charge = -1;
 angle *= -1;
 }
 angle = (angle % 360) * Charge;
Show more comments
avatar image
1

Answer by InvincibleCat · Jan 07, 2015 at 06:01 PM

This behavior makes sense. First of all, your Max shouldn't be less than your Min value. And most of all, Input.GetAxis will return a value between -1 and 1. As the value you pass in the Clamp function is always less than the Min, the Clamp function will always return the Min value! So Input.GetAxis is useless in this case! And you also should use Time.deltaTime or the result will be different depending of the framerate (I assume that's not he behavior you expect)

Hope I solve your problem!

Cheers!

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

6 People are following this question.

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

Related Questions

limiting rotation of a rigidbody in C# 3 Answers

Mathf.Clamp isn't stopping movement 1 Answer

Clamping local movement 0 Answers

Trouble with using Mathf.Clamp function in order to create collision detection with raycasts 0 Answers

Mathf.Clamp01 CoRoutine 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