Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class EnemyMove01 : MonoBehaviour
- {
- public Transform target; //set target from inspector instead of looking in Update
- public float speed = 3f;
- void Start()
- {
- }
- void FixedUpdate()
- {
- target = GameObject.FindWithTag("Player").transform;
- }
- void Update()
- {
- if (target == null)
- {
- Destroy(gameObject);
- }
- else
- {
- //rotate to look at the player
- transform.LookAt(target.position);
- transform.Rotate(new Vector3(0, -90, 0), Space.Self); //correcting the original rotation
- //move towards the player
- if (Vector3.Distance(transform.position, target.position) > 5f)
- { //move if distance from target is greater than 1
- transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement