Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. public class EnemyTerritory : MonoBehaviour
  2. {
  3. public BoxCollider territory;
  4. GameObject player;
  5. bool playerInTerritory;
  6.  
  7. public GameObject enemy;
  8. BasicEnemy basicenemy;
  9.  
  10. // Use this for initialization
  11. void Start ()
  12. {
  13. player = GameObject.FindGameObjectWithTag ("Player");
  14. basicenemy = enemy.GetComponent <BasicEnemy> ();
  15. playerInTerritory = false;
  16. }
  17.  
  18. // Update is called once per frame
  19. void Update ()
  20. {
  21. if (playerInTerritory = true)
  22. {
  23. basicenemy.MoveToPlayer ();
  24. }
  25.  
  26. if (playerInTerritory = false)
  27. {
  28. basicenemy.Rest ();
  29. }
  30. }
  31.  
  32. void OnTriggerEnter (Collider other)
  33. {
  34. if (other.gameObject == player)
  35. {
  36. playerInTerritory = true;
  37. }
  38. }
  39.  
  40. void OnTriggerExit (Collider other)
  41. {
  42. if (other.gameObject == player)
  43. {
  44. playerInTerritory = false;
  45. }
  46. }
  47. }
  48.  
  49. public class BasicEnemy : MonoBehaviour
  50. {
  51. public Transform target;
  52. public float speed = 3f;
  53. public float attack1Range = 1f;
  54. public int attack1Damage = 1;
  55. public float timeBetweenAttacks;
  56.  
  57.  
  58. // Use this for initialization
  59. void Start ()
  60. {
  61. Rest ();
  62. }
  63.  
  64. // Update is called once per frame
  65. void Update ()
  66. {
  67.  
  68. }
  69.  
  70. public void MoveToPlayer ()
  71. {
  72. //rotate to look at player
  73. transform.LookAt (target.position);
  74. transform.Rotate (new Vector3 (0, -90, 0), Space.Self);
  75.  
  76. //move towards player
  77. if (Vector3.Distance (transform.position, target.position) > attack1Range)
  78. {
  79. transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
  80. }
  81. }
  82.  
  83. public void Rest ()
  84. {
  85.  
  86. }
  87. }
  88.  
  89. //Monster moves towards player if inside territory, and attacks the player.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement