• 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 siddharth3322 · Sep 05, 2019 at 04:01 PM · rotationtransformlerpslerplookrotation

Smoothly Rotate Object Towards Target Object

I want to rotate my player vehicle into the target object direction/side. Though the following image, I have tried to explain my point in a better way:

alt text

I want to rotate my below tank object towards another tank object so that it can point into that direction.

I have written this code for this purpose but it is not working:

 IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
 {
     Quaternion targetRotation = Quaternion.identity;
     do
     {
         Debug.Log("do rotation");
         Vector3 targetDirection = opponentPlayer.position - transform.position;
         targetRotation = Quaternion.LookRotation(targetDirection);
         Quaternion nextRotation = Quaternion.Lerp(transform.localRotation, targetRotation, Time.deltaTime);
         transform.localRotation = nextRotation;
         yield return null;
 
     } while (Quaternion.Angle(transform.localRotation, targetRotation) < 0.01f);
 }

I just want to smoothly rotate and stop towards a target object. Please share your suggestion regarding this.

vehicle-target-object-rotation-edited.png (304.4 kB)
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
2
Best Answer

Answer by Hellium · Sep 05, 2019 at 04:20 PM

Quaternion.LookRotation(targetDirection) defines a rotation in world space, but you are trying to change the local rotation of the object. Try:

 Quaternion nextRotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime);
 transform.rotation = nextRotation;

But I would use RotateTowards instead

  Vector3 targetDirection = (opponentPlayer.position - transform.position).normalized;
  targetRotation = Quaternion.LookRotation(targetDirection);
  transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation , Time.deltaTime);
  yield return null;


Also, don't forget to change your condition to

 while (Quaternion.Angle(transform.rotation, targetRotation) > 0.01f);
Comment
Add comment · Show 6 · 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 siddharth3322 · Sep 05, 2019 at 04:25 PM 0
Share

Still, it's not doing a complete rotation or turning the face towards another target object!!.

avatar image siddharth3322 · Sep 05, 2019 at 04:27 PM 0
Share

This is the updated code that is executing:

  IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
     {
         Quaternion targetRotation = Quaternion.identity;
         do
         {
             Debug.Log("do rotation");
 
             Vector3 targetDirection = (opponentPlayer.position - transform.position).normalized;
             targetRotation = Quaternion.LookRotation(targetDirection);
             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime);
 
             yield return null;
 
         } while (Quaternion.Angle(transform.rotation, targetRotation) < 0.01f);
     }
avatar image Hellium siddharth3322 · Sep 05, 2019 at 04:34 PM 1
Share

Didn't see the condition.

Change it to

 while (Quaternion.Angle(transform.rotation, targetRotation) > 0.01f);

You want the object to continue while the object is not aligned with the target rotation.

avatar image siddharth3322 Hellium · Sep 05, 2019 at 04:39 PM 0
Share

You are a genius man :) Thank you so much - can you post updated code as an answer for both questions?

so I can mark this as correct.

Show more comments

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

165 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Quaternion Slerp Sanity Check 1 Answer

Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer

How to ROTATE an object without slowing the ends (lerp) 2 Answers

Slerp to make the right/left side face another object 2 Answers

What am I doing wrong with these rotations? 0 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