• 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 keilovergames · 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 keilovergames · 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 keilovergames · 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 keilovergames 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.

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

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

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

Issue with Slerping 1 Answer

Rigidbody spins out of control when colliding into corner 0 Answers

LookRotation is Returning Weird Values 0 Answers

Rotate objects face to camera 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges