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); } }
target.positionas the argument forQuaternion.LookRotation, because this function asks for a direction, not a position. \$\endgroup\$