# How can I make the objToRotate speed to rotate depending on the target moving speed?

The player has these components: Animator, Rigidbody, Capsule Collider, Third Person Character (Script), Third Person User Control (Script), and the MyLookAT script.

The problem is if I drag the cube target too fast in the editor changing its position it will take some time for the objToRotate to rotate facing the target again. I want somehow to make that the objToRotate will know to set his own speed and to rotate fast/slow enough to face the target when moving the target.

I want the objToRotate to face the target in real-time when the target is moving not with delay.

using UnityEngine;
using System;
using System.Collections;

public class RotatingTest : MonoBehaviour
{
public Transform target;
public Transform objToRotate;
public float speed = 1.0f;

void LateUpdate()
{
Vector3 targetDirection = target.position - objToRotate.position;
float singleStep = speed * Time.deltaTime;

Vector3 newDirection = Vector3.RotateTowards(objToRotate.forward, targetDirection, singleStep, 0.0f);

Debug.DrawRay(objToRotate.position, newDirection, Color.red);

objToRotate.rotation = Quaternion.LookRotation(newDirection);
}
}

• Do you still want a limited rotation speed at all? Or do you want to just delete the RotateTowards line and use LookRotation(targetDirection) instead? Jul 24, 2021 at 12:43
• @DMGregory If I delete the RotateTowrds line and using only the LookRotation like this objToRotate.rotation = Quaternion.LookRotation(target.position); it's not rotating at all. For now without limiting the speed just to rotate depending on the target speed movement. Jul 24, 2021 at 13:02
• Compare what you wrote in your comment to what I wrote in mine. I did not suggest passing target.position as the argument for Quaternion.LookRotation, because this function asks for a direction, not a position. Jul 24, 2021 at 13:04

using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))]
public class MyLookAt : MonoBehaviour
{
public Transform target;
public Transform objToRotate;

void LateUpdate()
{
Vector3 relativePos = target.position - objToRotate.position;
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
objToRotate.rotation = rotation;
}
}