- Home /
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
I even tried lerping the forward but that did not work properly.
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
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.
Yeah, I tried that, it wouldn't rotate smoothly at all, and it wouldn't even rotate all the way. Thanks though.
I also tried the code before I wrote it down, and it was working fine as smooth as you want. $$anonymous$$aybe you did something wrong, could you send the code part you added this part? @toishaanpatel
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.
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!
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.
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?
Does the enemy ever change where it is co$$anonymous$$g 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.
Yeah, the opponent is a driver and will be co$$anonymous$$g 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? $$anonymous$$aybe...
Your answer
![](https://koobas.hobune.stream/wayback/20220613002320im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to rotate bone relative to camera using LookAt 3 Answers
look at movedirection 1 Answer
2D rotation and moving forward 2 Answers
Quaternion.Lerp not working 1 Answer
Getting the forward position of an object in a vertex shader 0 Answers