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 rahul_ · May 23, 2014 at 07:02 AM · rotation3dquaterniondirectionslerp

Rotate object to target position

I have an object in my Unity scene which receives rotation values from another script. These rotation values are angles for rotating the object in the world space (and not around object's local axes).

The object already has an orientation in World Space (say 50deg pitch, 50 deg yaw) and we can take this current orientation to be S. When the object receives rotation values like (0,180,0), it has rotate from it's current orientation by 180 degrees around Y axis (in world space). When it receives the next rotation value (0,270,0), it has to rotate from current orientation to the orientation - S + 270(around Y axis in world space)

I tried to do Quaternion.Slerp but Slerp always takes the shortest path because of which this doesn't work correctly. Here's what my code looks like right now:

 IEnumerator RotateObject(Transform thisTransform, Vector3 endDegrees, float time){
         Quaternion startQuat = transform.rotation;
         Quaternion endQuat = transform.rotation * Quaternion.Euler (endDegrees);
 
         float rate = 1.0f / time;
         float t = 0.0f;
         while (t < 1.0) {
             t += Time.deltaTime * rate;
             transform.rotation = Quaternion.Slerp(startQuat, endQuat, t);
             yield return 0;
         }
     }



I am not sure how I can interpolate the eulerAngles directly, because it is not recommended to read the eulerAngles values (should be write-only), but I'd need to read the angles to get the current orientation and add/subtract rotation values to it.

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 robertbu · May 23, 2014 at 07:12 AM 0
Share

If your object is not been impacted by physics, then all you have to do is maintain your own Vector3 and assign it to the transform.eularAngles. So when you do your Vector3.Lerp() or Vector3.$$anonymous$$oveTowards(), you do it to your Vector3 variable.

1 Reply

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

Answer by robertbu · May 23, 2014 at 07:21 AM

I just read your comment on my old question. In particular, you will keep a class variable...something like myAngles:

 private Vector3 myAngles;

I don't know how you have your objects setup. If they always start at (0,0,0) rotation, then you can just assign Vector3.zero; Or you can make 'myAngles' public and then initialize eulerAngles to a set value. Or usually you can get away reading eulerAngles once in Start() or Awake().

After that you will always do your Lerp() to your variable. Here is a untested rewrite of your code to give you the idea:

 IEnumerator RotateObject(Transform thisTransform, Vector3 deltaDegrees, float time){
    Vector3 start = myAngles;
    Vector3 end = start + deltaDegrees;
    float rate = 1.0f / time;
    float t = 0.0f;
    while (t < 1.0) {
      t += Time.deltaTime * rate;
      myAngles = Vector3.Lerp(start, end, t);
      transform.eulerAngles = myAngles;
      yield return 0;
    }
 }
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 rahul_ · May 23, 2014 at 08:03 AM 0
Share

@robertbu: The objects in the World Space have their own orientation and are not at eulerAngles(0,0,0) at the start. As you mentioned, I can read the eulerAngles once on Start()/Awake() but there's a problem I see happening in this case. The very initial orientation is say S(50,50,0);

The first rotation value that comes in is 180 degrees, so endDegrees vec3 is found by S+vec3(0,180,0); and then angles are lerp-ed to reach the target orientation.

Now, the second rotation value which is 270 comes in, and the object has to move from current orientation to S+vec3(0,270,0).

However, for lerp-ing, I need to know the current euler angles of the object which I can't read because they might not have the correct representation. How would I solve this?

$$anonymous$$eanwhile, I'll give it a shot by just having myAngles as the orientation value from which to continue for the next rotation.

avatar image robertbu · May 23, 2014 at 01:06 PM 0
Share

You do know the current eulerAngles...or at least one representation of it. As long as nothing but this code is doing the rotation, myAngles will be a representation of the current rotation. Note in re-reading your question I'm assu$$anonymous$$g that S+vec3(0,270,0) represents a delta rotation from the existing rotation. I made a small change in my code sample. Originally I assumed it was an absolute rotation.

avatar image rahul_ · May 24, 2014 at 01:13 PM 0
Share

@robertbu : Thanks. This was super helpful! Was finally able to solve the problem. You saved the day! $$anonymous$$any thanks ^ 1000. :-)

avatar image rahul_ · Jul 03, 2014 at 06:44 AM 0
Share

@robertbu : I happened to come back to this problem again and facing a slight problem. The above question deals with adding a rotation value to the existing orientation of the object, but I am able to do the above only in world space.

The new scenario where I am facing the problem: The object already has an orientation in World Space (say 50deg pitch, 50 deg yaw) and we can take this current orientation to be S. When the object receives rotation values like (0,180,0), it has rotate from it's current orientation by 180 degrees around Y axis (in local space). Note that in the above question, the rotation was in world space, but this new scenario has to have rotations in local space. How do I deal with euler angles in the above answer to get the same rotation in local space (i.e. the object should be rotating around its own local Y-axis in this case)?

avatar image robertbu · Jul 03, 2014 at 07:20 AM 0
Share

You should probably open this as a new question with a full explanation of the setup. If you have a world rotation of (0,50,50) and then you want to apply local rotations, consider isolating the (0,50,50) in a parent object and using Transform.localEulerAngles(). You can also play games with Quaternion.AngleAxis() using the the local axes (transform.up, transform.right, transform.forward) converted to world coordinates. And there are some other approaches.

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

20 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

Related Questions

Rotation direction in coroutine 2 Answers

How to smoothly rotate an object on only two axes? 2 Answers

Rotating a direction Vector3 by Quaternion 2 Answers

allows users to view 360º content without a VR headset 0 Answers

3D nested Turret Prefab 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