Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyMove01 : MonoBehaviour
  5. {
  6.  
  7. public Transform target; //set target from inspector instead of looking in Update
  8. public float speed = 3f;
  9.  
  10.  
  11.  
  12. void Start()
  13. {
  14.  
  15. }
  16.  
  17. void FixedUpdate()
  18. {
  19.  
  20. target = GameObject.FindWithTag("Player").transform;
  21. }
  22.  
  23. void Update()
  24. {
  25.  
  26. if (target == null)
  27. {
  28. Destroy(gameObject);
  29. }
  30. else
  31. {
  32. //rotate to look at the player
  33. transform.LookAt(target.position);
  34. transform.Rotate(new Vector3(0, -90, 0), Space.Self); //correcting the original rotation
  35.  
  36.  
  37. //move towards the player
  38. if (Vector3.Distance(transform.position, target.position) > 5f)
  39. { //move if distance from target is greater than 1
  40. transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
  41. }
  42. }
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement