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
1
Question by mossbeard1 · Sep 13, 2018 at 07:50 PM · rotationrotation axisslerpquaternionsrotating

need my object to rotate between specific angles on the x axis

so currently I'm trying to make a spotlight for a ship and in the editor, I know that the points that I need are between two vectors ie. vector3(60,-90,-90) and vector3(120,-90,-90)

and I'm wanting to just have a locked rotation between these two points and I've tried everything I can think of using quaternion.Lerp or LerpAngle. or using mathf.lerp, or vector3.lerp. or something as simple as transform.rotate and played with using the lookat function. but it seems no matter how I format this the rotation is straight nonsense.

if you have any example, suggestions or require more information I would be happy to record a short youtube video for you guys.

Thank you for all your time and effort. and keep on keeping on!

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

2 Replies

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

Answer by maxoja · Sep 13, 2018 at 08:27 PM

I hope this snippet of code could help. It uses Quaternion.Lerp to interpolate rotation between your 2 angular points.

 public float speed = 1;

 float ratio = 0;
 Quaternion rotationA = Quaternion.Euler(60, -90, -90);
 Quaternion rotationB = Quaternion.Euler(120, -90, -90);

 public void Update()
 {
     ratio += Time.deltaTime; 

     if (ratio >= 2f)
         ratio -= 2;

     if (ratio <= 1f)
         transform.rotation = Quaternion.Lerp(rotationA, rotationB, ratio);
     else
         transform.rotation = Quaternion.Lerp(rotationA, rotationB, 1-(ratio-1));
 }


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 mossbeard1 · Sep 13, 2018 at 11:20 PM 0
Share

This worked for me!!! thank you. one question though could you briefly explain this to me else transform.rotation = Quaternion.Lerp(rotationA, rotationB, 1-(ratio-1));

I'm not entirely sure how the 1-(ratio -1) works.

avatar image maxoja mossbeard1 · Sep 13, 2018 at 11:38 PM 1
Share

Normally when we Lerp(a, b, ratio) while increasing ratio constantly from 0.0 to 1.0, it basically means "from a to b". In order to reverse the phrase to "from b to a", I can Lerp(a, b, 1-ratio) ins$$anonymous$$d. However, in our case, the ratio in else block is changing from 1.0 to 2.0 which means its value is 1.0 ahead. That is why I need to subtract is by 1, and the final statement becomes Lerp(a, b, 1-(ratio-1))

avatar image mossbeard1 maxoja · Sep 13, 2018 at 11:42 PM 0
Share

wow man smart! thank you!

avatar image
1

Answer by Atiyeh123 · Sep 13, 2018 at 08:22 PM

Hi @keilovergames If your problems is just limiting the angle of rotation you can do this.

      angle = Mathf.Clamp(angle, min, max); // it clamps the angle between a min and max angle you want


the rest code is like this:

        float angle = Mathf.Atan2 (transform.position.y - target.transform.position.y,
                     transform.position.x - target.transform.position.x) * Mathf.Rad2Deg;  
 angle = Mathf.Clamp(angle, min, max); 
                     Quaternion rotateTo = Quaternion.AngleAxis (angle  , Vector3.forward);
                     transform.rotation= Quaternion.Slerp (transform.rotation, rotateTo, Time.deltaTime * 10f);



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

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

Strange rotation pattern. 0 Answers

Prevent child rotation but still control it yourself 1 Answer

How to "clamp" a y rotation in unity? 0 Answers

Quaternions; how do I rotate an object around only 2 axis based on another rotation? 1 Answer

Issue with Slerping 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