Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 mehzzz · Nov 27, 2014 at 08:14 AM · mathf.clampz-axis

Mathf.Clamp on Z-Axis to limit rotation using transform.Rotate

Hi,

I'm using a leap to rotate an object on its z-axis. Currently I am unsure about how to properly use Mathf.Clamp to limit the rotation using transform.Rotate. Tho I realize this is currently incorrect, this is my starting point. In this case roll is the raw position of the palm in space. Suggestions? Thanks.

 if ((roll > 0.1f) && (roll < 0.6f)) {
       transform.Rotate(Mathf.Clamp(0,0,rotSpeed * Time.deltaTime ),0f,10.0f); 
 }else if ((roll < 0) && (roll > -0.8)){
        transform.Rotate(Mathf.Clamp(0,0,-rotSpeed * Time.deltaTime ),0f,-10.0f); 
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Nov 27, 2014 at 05:00 PM

That approach won't work. The math in clamp is wrong (it's always 0,) but beyond that, the whole idea is wrong.

Think of adding 1 to a number, but not past 30. x=x+1 is "dumb". It will add 1 to any number the same as Rotate will spin any angle. if(x<30) x=x+1; solves it. It's our job, using the if, to only run the "add 1" when we need to. Another way is x=x+1; x=Math.Clamp(x,0,30);. In that case, it's our job to personally examine the final value and fix it.

Then think about x=x+Math.Clamp(y,0,30);. Suppose x is 20 and y is 40. This checks y, clamps to 30, then adds 30 to x giving 50. Uck. If you did it again, x would be 80. In no way does it check the final value of x. You can't write one command that means "add 1 but not past 30." The same say, you can't write one command that means "spin, but not past 30 degrees."

Using Rotate to only go so far is tricky. The Answers for it here look complicated, because it is. You can sort of check transform.euler.z, either before or after the rotate. That bugs out in many cases, but lots of people use it. Or, depending exactly on the limits and ways you can spin, you can check transform.up.y or Quaternion.Angle ... .

But, like adding 1, the way to do it is to check the angle before or after. And either don't do the spin, or "fix" the spin afterwards.

Comment
Add comment · Show 3 · 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 mehzzz · Nov 27, 2014 at 07:30 PM 0
Share

Hmm. Alright, how about a different approach. I get what you are saying, to a point, but still feel a bit lost. This (below) works. The thing is I can't really use Input.GetAxis as leap data is not a button being pressed. Its like a stream.

 spin -= Input.GetAxis("Horizontal") * 30 * Time.deltaTime;
 spin = $$anonymous$$athf.Clamp(spin, -30, 30);
 transform.rotation = Quaternion.Euler(0,0,spin);

So ins$$anonymous$$d of trying to solve Clamp, I'll try and better solve how I'm getting input. $$anonymous$$aybe that's a better way to go about it?

thanks for the reply btw.

avatar image Owen-Reynolds · Nov 27, 2014 at 08:14 PM 0
Share

Right, that way works since you personally limit the total spin to +/-30 (in line 2) before using it. Also note that Rotate adds to the spin you have. This method just sets it to spin.

Just get rid of line 1, and use any method you want. For example: if(leapRight==true) spin+=10*Time.deltaTime;

avatar image Lionheart7 Owen-Reynolds · Apr 30, 2020 at 08:37 PM 0
Share

Thanks this worked for me

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate w/ Mouse Click: Random(?) Z.Axis problem (2D) 2 Answers

Unity2D- Screen view out of frustum? 1 Answer

Can't Stop Rotation along the Z Axis 0 Answers

Two Clamps on the Same axis 1 Answer

What am I doing wrong? Using mathf.clamp to cap an int value. value 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