Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Leroterr · Mar 28, 2015 at 11:31 AM · rotationquaternionangleeuleranglestransform.rotation

How do I clamp the Z-Axis Rotation for this code?

The code below rotates my wheel on the Z-Axis, but it does so in the whole 360 degrees (0 to 360).

I only want it to be able to rotate from 180 to 360.

 if (Input.touchCount > 0) 
         {
             
             for(int i = 0; i < Input.touchCount; i++)
             {
                 
                 ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
                 
                 if(Input.touches[i].phase == TouchPhase.Began)
                 {
                     
                     if(Physics.Raycast(ray, out hit) && hit.collider == this.GetComponent<Collider>())
                     {
                         Vector3 wp = Input.GetTouch(i).position;
                         var dir = Camera.main.WorldToScreenPoint(transform.localPosition);
                         dir = wp - dir;
                         baseAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
                         baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
                     }
 
                 }
 
                 if(Input.touches[i].phase == Moved)
                 {
                     Vector3 wp = Input.GetTouch(i).position;
                     var dir = Camera.main.WorldToScreenPoint(transform.localPosition);
                     dir = wp - dir;
                     angle =  Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - baseAngle;
                     transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
                 }
 
             }
 
         }

Thanks! :)

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 fafase · Mar 28, 2015 at 11:58 AM

Lucky you I did one similar thing lately:

 public void RotateShip(Vector3 tapPosition)
 {
     float polarity = (tapPosition.x < halfWidth) ? 1f : -1f;
     transform.Rotate(Vector3.forward, polarity * Time.deltaTime * speed);
     float angle = transform.eulerAngles.z;
     transform.eulerAngles = new Vector3(0f, 0f, ClampAngle(angle, -45f,45f));
 }
 private float ClampAngle(float angle, float min, float max)
 {
     if(angle < 90f || angle > 270f)
     {
         if(angle > 180)
         {
             angle -= 360f;
         }
         if(max > 180)
         {
             max -= 360f;
         }
         if(min > 180)
         {
             min -=360f;
         }
     }
     angle = Mathf.Clamp (angle, min, max);
     if(angle < 0)
     {
         angle += 360f;
     }
     return angle;
 }

tapPosition is the screen position of the touch as it was for our project, in your case, I guess it will be the touch position.

The parameters in ClampAngle define the range of rotation, in the example it is from -45 to 45 considering 0 to be the initial rotation.

The ClampAngle method comes from there: http://answers.unity3d.com/questions/141775/limit-local-rotation.html

you would have to change polarity maybe as in my case it was just if the touch was on left or right half of the screen.

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 yusolaifdfer · Mar 28, 2015 at 12:03 PM 0
Share

And why should he just not put $$anonymous$$athf.Clamp after the line 28 as I said ?

avatar image fafase · Mar 28, 2015 at 12:52 PM 0
Share

As you said where? The long version allows to use the rotation clamping in all situation, that is whether you need from 0 to 180 or 180 to 360 (which is 0, there is why you cannot just put clamp on line 28) or -45 to 45.

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

21 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

Related Questions

Object makes random jumps in rotation 1 Answer

Head and body rotate threshold 1 Answer

How to Constrain Quaternion Rotations Over Time? 1 Answer

quaternion.eulerAngles 1:1 angle conversion 1 Answer

How can I multiply the rotation of a mirrored object? 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