• 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 Ekta-Mehta-D · Nov 30, 2015 at 05:44 PM · rotationangleeuleranglesslerp

Limit rotation angle on Z axis while slerp

hii,..

I am doing slep of rotation and i want to set maximum rotation angle to -3 and minimum rotation angle to 17.

But i dont understand how do i set. code i have used for slep is here :

     // Update is called once per frame
     void Update ()
     {
         if (Input.GetMouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, 100)) {
                 if (hit.collider.name == "LeftCollider") {
                     finalRotation = Quaternion.Euler (0, 0, 5) * kite.transform.rotation;
                     if (kite.transform.eulerAngles.z < 14 || kite.transform.eulerAngles.z > 350) {
                         rotating = true;
                         StartCoroutine (rotateKiteDown ());
                     }
 
                 }
                 if (hit.collider.name == "RightCollider") {
                     finalRotation = Quaternion.Euler (0, 0, -5) * kite.transform.rotation;
                     if (kite.transform.eulerAngles.z < 16 || kite.transform.eulerAngles.z > 350) {
                         rotating = true;
                         StartCoroutine (rotateKiteUp ());
                     }
                 }
             }
         }
     
     }
 
     IEnumerator rotateKiteUp ()
     {
         while (kite.transform.rotation != finalRotation) {
             kite.transform.rotation = Quaternion.Slerp (kite.transform.rotation, finalRotation, Time.deltaTime * 2.5f);
             yield return 0;
         }
         kite.transform.rotation = finalRotation;
     }
 
     IEnumerator rotateKiteDown ()
     {
         while (kite.transform.rotation != finalRotation) {
             kite.transform.rotation = Quaternion.Slerp (kite.transform.rotation, finalRotation, Time.deltaTime * 2.5f);
             yield return 0;
         }
         kite.transform.rotation = finalRotation;
             
     }
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 Fattie · Nov 30, 2015 at 07:37 PM 0
Share

your code is far too much, try to just post the relevant few lines

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Fattie · Nov 30, 2015 at 07:29 PM

Ekta. Conceivably, just use void LateUpdate() and very simply look in there at the eulerAngles, and crush them down to your limits.

(So, quite simply like ...

 e = ... eulerAngles
 newZ = Mathf.Clamp( e.z, -3f, 17f)
 e.z = new Z
 .. eulerAngles = e

.. and you're done.)

It's quite difficult to fight against slerping, you could get flicker.

IT can be very, very difficult to achieve what you're up to, but using the "trick" of LateUpdate can work in certain ranges of motion. Hope it helps.

Comment
Add comment · Show 5 · 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 Arju2011 · Dec 01, 2015 at 07:59 AM 0
Share

This is the same solution that I was about to give. :D In my experience, it did not work very well, but it mostly worked.

avatar image Ekta-Mehta-D · Dec 01, 2015 at 08:31 AM 0
Share

I will try tonight and let you know. Thanks @Fattie

avatar image Ekta-Mehta-D · Dec 01, 2015 at 08:33 AM 0
Share

sir Can you post exact full code? and will it behave same like slerp on every hit ?

avatar image Ekta-Mehta-D · Dec 01, 2015 at 06:06 PM 0
Share
 if (hit.collider.name == "RightCollider") {
                     finalRotation = Quaternion.Euler (0, 0, -5) * kite.transform.rotation;
                     Vector3 eulerAgles = finalRotation.eulerAngles;
                     float z = $$anonymous$$athf.Clamp (eulerAgles.z, -3.0f, 17.0f);
                     eulerAgles.z = z;
                     finalRotation.eulerAngles = eulerAgles;
                     kite.transform.rotation = finalRotation;

}

I have done something like this. But its not working smoothly like slerp. @Fattie

avatar image sankalp22 · Dec 04, 2015 at 12:49 PM 0
Share

you can use slerp. Clamp rotation angle before calling coroutines. I didn't have any problem with slerp.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

35 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 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

How to use Euler angles to rotate an object ? 5 Answers

Problems caused by non-unique Euler angle solutions 1 Answer

How to get Angle float of specific axis.(Turret clamping related). 2 Answers

Camera Zoom from rotation. 0 Answers

Make Quaternion Slerp happen over a 1 second period 3 Answers


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