Hey guys, I'm very new to Unity so I don't know what I would use to do this, but I would like to have an enemy rotate towards a player if it is within a certain radius. I've already managed to use some code but it doesn't give me the range effect that I want. Any help? Here's the code I currently have.
private GameObject target;
private Vector3 targetPoint;
private Quaternion targetRotation;
void Start () {
rigidbody.velocity = transform.up * speed;
target = GameObject.FindWithTag("Player");
}
void Update()
{
targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
targetRotation = Quaternion.LookRotation (-targetPoint, Vector3.right);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
}`
Any and all help is appreciated. I'm coding in C# but JavaScript answers are helpful as well.
↧