• 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 BEFaughnan · Apr 16, 2015 at 09:27 PM · rotationmobilemathf.clamp

Clamp rotation of object with C# on mobile.

Hello everyone!

I'm having a problem getting my object to stop rotating at 30 and -30 degrees. It will always continue past them in positive and negative.

Here is my code. I have found a few topics on here that are similar but nothing has worked so far.

 private float h;
 private float horozontalSpeed = 0.75f;
 private Touch touch;
 private bool active;



 void Update()
 {
     if(!active)
     {
         if (Input.touchCount == 1)
         {
             touch = Input.GetTouch(0);

             if (touch.phase == TouchPhase.Moved)
             {
                 h = Mathf.Clamp(horozontalSpeed * touch.deltaPosition.x, -30.0f, 30.0f);
                 transform.Rotate( 0.0f, -h, 0.0f, Space.World);
                 print (h);
             }
         }
     }


 }

 public void StopRotate()
 {
     print ("Stop rotating, damn you!");
     active = !active;
 }

}

Please let me know if you need any more information!

Comment
Add comment · Show 1
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 Glurth · Apr 17, 2015 at 07:14 PM 0
Share

Well, the value that you are clamping is the INPUT to your rotate call. You are NOT clamping the final rotation. transform.Rotate will use your transform's current orientation, and ADD to that.

Perhaps if you set the EulerAngles directly, with your input, rather that Rotate(), you will have better luck? http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

1 Reply

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

Answer by termway · Apr 17, 2015 at 07:26 PM

Do not use transform.Rotate() it's using your current object rotation. transform.eulerAngles (http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html) should solve your problem.

 float rotationY = transform.localEulerAngles.y + h;
 float relativeRotationY = (rotationY < 180)? rotationY : - 360 + rotationY;
 relativeRotationY = Mathf.Clamp(relativeRotationY, -30, 30);
 transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, relativeRotationY,transform.localEulerAngles.z);


But it's a bad pratice to change rotation with eulerAngles because of potential Gimbal lock. Instead you should test your current orientation and rotate only when your value is not too high or too low like this :

 float min = -30, max = 30;
 float relativeRotationY = (transform.localEulerAngles.y < 180)? transform.localEulerAngles.y : - 360 + transform.localEulerAngles.y;
 
 if(h < 0 && relativeRotationY > min ||
    h > 0 && relativeRotationY < max)
     transform.Rotate( 0.0f, h, 0.0f, Space.World);
 
 if(relativeRotationY < min)
     transform.Rotate( 0.0f, -relativeRotationY + min, 0.0f, Space.World);
 if(relativeRotationY > max)
     transform.Rotate( 0.0f, -relativeRotationY + max, 0.0f, Space.World);    



Comment
Add comment · Show 13 · 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 Glurth · Apr 17, 2015 at 07:34 PM 0
Share

Looks to me like that code does not check to see if the final rotation is [-3030], it just checks the initial rotation, and adds whatever the input is, even if that takes it out of bounds. Am I missing something?

avatar image BEFaughnan · Apr 17, 2015 at 08:09 PM 0
Share

Well this gave me some interesting results anyways! It will clamp the first time it rotates in negative. But it will spin infinitely in positive still.

I'm gona keep messing with it. Thanks for the new ideas! Please let me know if you think of anything else!

avatar image termway · Apr 17, 2015 at 08:16 PM 0
Share

No you're right. There is some work to add in order to make that constraint. I also missed the fact that eulerAngles are absolute value. It should be ok now.

avatar image BEFaughnan · Apr 17, 2015 at 09:12 PM 0
Share

Closer! But still a bit strange, Now when I am dragging my finger it will rotate smoothly for a bit and then snap to a larger degree (80-90 range).

avatar image termway · Apr 17, 2015 at 09:23 PM 0
Share

Your object have a parent ?

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Limiting rotation to 75 degrees to the left and 75 to the right 4 Answers

Camera Rotation stay behind player and not flip 2 Answers

After adding script for rotation to look up down, look left right stopped working,After writing code for the rotation for looking up down, looking right left stopped 0 Answers

Using Mathf.Clamp with Accelerometer to control movement speed has slow response time, how can I smooth it out? 0 Answers

Limiting 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