Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 anwe · Jun 05, 2011 at 08:53 PM · rotationmathfclamplimit

How to limit the rotation of an object??

Yeah, I know I could use Mathf.Clamp, but the problem is that it goes in contrast with something else, so I wuold like to have a smoother effect. Have u got any idea?? THNKs.

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 anwe · Jun 08, 2011 at 07:09 PM 0
Share

yes, $$anonymous$$athf.Clamp would be probably the best, but I've the little problem that my object stops at the maximum or $$anonymous$$imum, but it shakes itself

avatar image noradninja · Jun 08, 2011 at 07:26 PM 0
Share

That is actually why the if angle statements are in the function, to deal with that. The vibration occurs because without the if statements, Unity doesn't know how to deal with values above 360 or below -360, so you get a shimmer as it is trying to interpolate that values. The if statements make it so if you go above or below the max/$$anonymous$$ rotation, the angle value 'wraps' around in a loop.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by testure · Jun 05, 2011 at 08:58 PM

Mathf.Clamp is the answer. Perhaps if you elaborate on "something else" we could help you figure out what to do?

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 noradninja · Jun 05, 2011 at 09:38 PM

// Hacked and gutted from the MouseOrbit script that comes with the Standard Package as well as the control script that comes with iPhone Basic. Combined with a script to tilt an object with the iPhone accelerometer.

 var xSens = 1.0; 
 var ySens = 1.0; 
 var zMinLimit : float = -20; 
 var zMaxLimit : float = 80; 
 var xMinLimit : float = -20; 
 var xMaxLimit : float = 80; 
 var angleComp : float = 0.5;

 private var x = iPhoneInput.acceleration.x; 
 private var y = iPhoneInput.acceleration.y; 

 function Start ()
 {
 
 for (var evt : iPhoneTouch in iPhoneInput.touches)
 {
     if (evt.phase == iPhoneTouchPhase.Moved)
     {
 normalizedSpeed = evt.position.y / Screen.height;              
 x = transform.eulerAngles.x; 
 y = transform.eulerAngles.y; 

     }
 }
 }

 
 function FixedUpdate () { 
  
 var accelerator : Vector3 = iPhoneInput.acceleration;
    
    accelerator.y = ClampAngle((accelerator.y * ySens), xMinLimit, xMaxLimit); 
    accelerator.x = ClampAngle(((accelerator.x + angleComp) * xSens), zMinLimit, zMaxLimit); 
        
 var rot = Quaternion.Euler(accelerator.x, 0, accelerator.y); 
 
 
 transform.rotation = rot; 
 //Debug.Log (accelerator);
  
 } 

 static function ClampAngle (angle : float, min : float, max : float) { 
    if (angle < -360) 
       angle += 360; 
    if (angle > 360) 
       angle -= 360; 
    return Mathf.Clamp (angle, min, max); 
 } 

This script is something I cobbled together for an iPhone game; it takes the accelerometer input and clamps the rotation to the values defined in x/z Min/MaxLimit. You could add Y rotation with some small modification, and it is easily modifiable to take input from any source. Hope it points you in the right direction, just drop the script on the object whose rotation you want to limit.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

limit accelerometer controlled rotation 1 Answer

gesture rotation clamp 2 Answers

Controlling camera rotation limits. 1 Answer

Clamp Rotation Problem 1 Answer

Quick Question About Limiting a Rotation. 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