Advertisement
Guest User

enemyAIFAIL

a guest
Aug 23rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemySold : MonoBehaviour {
  5.  
  6. CharacterController cc;
  7. public float gravity;
  8. private Vector3 moveDirection = Vector3.zero;
  9.  
  10. public float moveSpeed;
  11. public float jumpForce;
  12.  
  13. public float shootRate;
  14. float rate;
  15.  
  16. public GameObject actor;
  17. public CapsuleCollider collisionCapsule;
  18. public GameObject bullet;
  19. public Transform bulletSpawnPoint;
  20.  
  21. public Transform rayPoint;
  22.  
  23. int dir;
  24. public bool playerInSight;
  25.  
  26. //
  27.  
  28. void Start ()
  29. {
  30. if(cc == null)
  31. {
  32. cc = this.gameObject.AddComponent<CharacterController>();
  33. cc.height = 1;
  34. cc.center = new Vector3(0, 0.5f, 0);
  35.  
  36. dir = 0;
  37. actor.transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
  38. playerInSight = false;
  39. }
  40. }
  41.  
  42. void FixedUpdate()
  43. {
  44. RaycastHit hit;
  45. Debug.DrawRay(rayPoint.position, rayPoint.right, Color.green);
  46.  
  47. if (Physics.Raycast(rayPoint.position, rayPoint.right, out hit, 10.0f)){
  48.  
  49. if(hit.transform.tag == "wall" && hit.distance < 1)
  50. {
  51.  
  52. // Looking left turn to Right
  53.  
  54. if(dir == 1)
  55. {
  56. dir = 0;
  57. actor.transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
  58.  
  59. }
  60.  
  61. // Looking right turn to Left
  62.  
  63. if(dir == 0)
  64. {
  65. dir = 1;
  66. actor.transform.rotation = Quaternion.Euler(new Vector3(0,180,0));
  67.  
  68. }
  69. }
  70.  
  71.  
  72. if(hit.transform.tag == "Player")
  73. {
  74. playerInSight = true;
  75. }
  76. }
  77.  
  78. else
  79. {
  80. playerInSight = false;
  81. }
  82. }
  83.  
  84. void Update ()
  85. {
  86. // shoot!
  87. if(playerInSight)
  88. {
  89. rate -= 1 * Time.deltaTime;
  90. }
  91.  
  92. // move!
  93. if(!playerInSight && cc.isGrounded)
  94. {
  95. if(dir == 0)
  96. {
  97. moveDirection = new Vector3(moveSpeed, 0, 0);
  98. }
  99.  
  100. if(dir == 1)
  101. {
  102. moveDirection = new Vector3(-moveSpeed, 0, 0);
  103. }
  104. }
  105.  
  106.  
  107. if(rate <= 0)
  108. {
  109. Instantiate(bullet, bulletSpawnPoint.transform.position , bulletSpawnPoint.transform.rotation);
  110. rate = shootRate;
  111. }
  112.  
  113. // gravity!
  114. moveDirection.y -= gravity * Time.deltaTime;
  115. cc.Move(moveDirection * Time.deltaTime);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement