• 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
Question by toishaanpatel · Jul 02, 2020 at 02:02 PM · rotationboneforwardinterpolate

How do I make my gameobject rotate smoothly towards target?

I have a transform which is the player's head bone, and I want to rotate it towards the opponent smoothly. Right now, with the following code, the player's head rotates towards the opponent as expected, but it is not smooth. As soon as the condition is met, the player's bone rotation just jumps to the expected rotation. I want it to look smooth:

 Vector3 look = GameObject.FindGameObjectWithTag("Opponent").transform.position-bone.position;
  bone.forward = look;
  bone.localEulerAngles = new Vector3(bone.localEulerAngles.x, 8, 0); //to maintain the other axis from changing //x is apparently the local axis in this case that makes the player's head rotate left to right
Comment

People who like this

0 Show 3
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 toishaanpatel · Jul 02, 2020 at 02:03 PM 0
Share

I even tried lerping the forward but that did not work properly.

avatar image toishaanpatel · Jul 02, 2020 at 02:12 PM 0
Share

I did lerp the forward, but that was very jittery and it was glitchy. It would sometimes reach the desired rotation, but it it keeps moving back and forth

avatar image toishaanpatel · Jul 02, 2020 at 02:13 PM 0
Share

This is in lateUpdate as well

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by HappyPixel27 · Jul 03, 2020 at 01:43 AM

Not sure if this is what you wanted but i think you should have it so the head will rotate slowly each frame until the desired target is reached.

Comment
Rs

People who like this

-1 Show 9 · 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 toishaanpatel · Jul 03, 2020 at 03:12 AM 0
Share

Ok.. doesnt lerp or slerp do that? I tried doing those and it wouldn't rotate past a certain point depending on what the t parameter was. How would I implement your solution in this case?

avatar image HappyPixel27 toishaanpatel · Jul 03, 2020 at 08:03 PM 0
Share

Does the enemy ever change where it is coming from? If not you can avoid scripting all together and just create an animation. What i was thinking is to find the rotation to stop at, put that into a variable and then create a speed variable. The object will rotate according to that speed until it is facing the enemy.

avatar image toishaanpatel HappyPixel27 · Jul 04, 2020 at 03:10 AM 0
Share

Yeah, the opponent is a driver and will be coming from any direction or might drive beside me altogether. The player's head is in the middle of an animation so I was using LateUpdate. I am thinking that this head follow thing is not such a good idea cuz even if I manage to get the head rotating smoothly, I have no way to return the head movements back in the animation. Thanks for the help though. Is there any way to manipulate an animation? Probably not, right? Maybe...

Show more comments
avatar image

Answer by Beylan0 · Jul 03, 2020 at 08:32 PM

I could suggest you to try this way:

 Quaternion lookRotation = Quaternion.LookRotation((target.position - transform.position).normalized);
 transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, 5f * Time.deltaTime);

"transform.rotation" and "transform.position" is the object you're trying to rotate, and lookRotation's target.position is the "Opponent" i suppose.

@toishaanpatel

Comment
Rs
Robster95

People who like this

2 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 toishaanpatel · Jul 04, 2020 at 03:07 AM 0
Share

Yeah, I tried that, it wouldn't rotate smoothly at all, and it wouldn't even rotate all the way. Thanks though.

avatar image Beylan0 toishaanpatel · Jul 04, 2020 at 06:36 AM 0
Share

I also tried the code before I wrote it down, and it was working fine as smooth as you want. Maybe you did something wrong, could you send the code part you added this part? @toishaanpatel

avatar image toishaanpatel Beylan0 · Jul 04, 2020 at 02:16 PM 0
Share

I have a question, If I manage to get the rotation working, it it possible that under some condition, I do not want the player to look at the opponent anymore, is it possible to return the bone movements back to the animation? Because I am using LateUpdate to override the animation playing on the player's body. Kind of like an idle animation. When I was experimenting with some code, when I stop looking at the opponent, the player's head would just stay at rotation (0,0,0) or whatever, even when there are specific animations playing that need the head to move.

Show more comments
avatar image Robster95 · Nov 07, 2020 at 08:30 AM 0
Share

Hey I just wanted to say I was having trouble with this too and glad I found your question on here before I had to ask.

@Beylan0 you helped fix my problem! thank you so much seeing this work made my night and made my game look smoother!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

How to rotate bone relative to camera using LookAt 3 Answers

Vector3.forward only moves me in 2 directions when rotating 1 Answer

Rigidbody slowing right down when colliding only when I'm rotating the velocity to always be going forward 1 Answer

How do i rotate an object to face a direction (with specific forward/up) 0 Answers

Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle. 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