Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Test : MonoBehaviour
- {
- public GameObject[] target;
- public float objectDistance;
- public float rotationDamping;
- void Start()
- {
- target = GameObject.FindGameObjectsWithTag("Enemy"); //It will look for all objects tagged as "Enemy"
- }
- void Update()
- {
- for (var i = 0; i < target.Length; i++)
- {
- objectDistance = Vector3.Distance(target[i].transform.position, transform.position);
- if (objectDistance < 20f)
- {
- lookAtPlayer();
- }
- }
- }
- void lookAtPlayer()
- {
- for (var i = 0; i < target.Length; i++)
- {
- Quaternion rotation = Quaternion.LookRotation(target[i].transform.position - transform.position);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement