# Rotate to another objects rotation

Hi guys!
I’ve been trying to figure this out for a few hours now (over days).

I have a head that is essentially a camera that rotates to look where the player moves their mouse (like any FPS). What I want to do with that, is slowly rotate the characters body to match the Y rotation of the head.

I’ve tried all sorts of things like disconnecting the head from the body (as far as parenting goes) which works better, but with this code, what happens is if I move the mouse just a little to the left or right, the body continuously rotates at a speed based on the difference of rotation, rather than rotating towards the rotation of the head.

``````void RotateTowardsMouse(){
float rotX = this.gameObject.transform.rotation.eulerAngles.x;
float rotY = this.gameObject.transform.rotation.eulerAngles.y;
float rotZ = this.gameObject.transform.rotation.eulerAngles.z;

float newRotY = Mathf.Lerp (rotY, camrotY, 1f);
Vector3 newRot = new Vector3(rotX, newRotY, rotZ);
this.gameObject.transform.Rotate(newRot);
{
``````

It’s probably obvious, but I don’t know what I’m doing wrong. Anyone have any solutions?

``````float speed = 10f;
float newRotY = Mathf.MoveTowardsAngle(rotY, camrotY, speed * Time.deltatime);
Vector3 newRot = new Vector3((rotX, newRotY, rotZ);
this.gameObject.transform.rotation = Quaternion.Euler(newRot);
``````

1 Like

That works pretty well! Thank you.
I don’t suppose there is a way to ease the rotation instead of having it turn linearly, is there?

There are several… An easy way is multiplying with the distance

``````float delta = Mathf.DeltaAngle(rotY, camrotY);
float newRotY = Mathf.MoveTowardsAngle(rotY, camrotY, delta * speed * Time.deltatime);
``````

Or you can use Mathf.SmoothDampAngle for example.

1 Like