Advertisement
jkuehlin

Ai Code problem

Nov 14th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.AI;
  6. using Panda;
  7.  
  8. public class AI : MonoBehaviour
  9. {
  10. public Transform player;
  11. public Transform bulletSpawn;
  12. public Slider healthBar;
  13. public GameObject bulletPrefab;
  14.  
  15. NavMeshAgent agent;
  16. public Vector3 destination; // The movement destination.
  17. public Vector3 target; // The position to aim to.
  18. float health = 100.0f;
  19. float rotSpeed = 5.0f;
  20.  
  21. float visibleRange = 80.0f;
  22. float shotRange = 40.0f;
  23.  
  24. void Start()
  25. {
  26. agent = this.GetComponent<NavMeshAgent>();
  27. agent.stoppingDistance = shotRange - 5; //for a little buffer
  28. InvokeRepeating("UpdateHealth",5,0.5f);
  29. }
  30.  
  31. void Update()
  32. {
  33. Vector3 healthBarPos = Camera.main.WorldToScreenPoint(this.transform.position);
  34. healthBar.value = (int)health;
  35. healthBar.transform.position = healthBarPos + new Vector3(0,60,0);
  36. }
  37.  
  38. void UpdateHealth()
  39. {
  40. if(health < 100)
  41. health ++;
  42. }
  43.  
  44. void OnCollisionEnter(Collision col)
  45. {
  46. if(col.gameObject.tag == "bullet")
  47. {
  48. health -= 10;
  49. }
  50. }
  51.  
  52. //telling Panda to pick up as task which links to leaf node
  53. [Task]
  54. public void TargetPlayer()
  55. {
  56. target = player.transform.position;
  57. Task.current.Succeed();
  58. }
  59.  
  60. [Task]
  61. public void LookAtTarget()
  62. {
  63. Vector3 direction = target - this.transform.position;
  64. this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
  65. Quaternion.LookRotation(direction),
  66. Time.deltaTime * rotSpeed);
  67.  
  68. if (Task.isInspected)
  69. Task.current.debugInfo = string.Format("angle = {0}",
  70. Vector3.Angle(this.transform.forward, direction));
  71.  
  72. if(Vector3.Angle(this.transform.forward,direction) < 5.0f)
  73. {
  74. Task.current.Succeed();
  75. }
  76. }
  77.  
  78. [Task]
  79. public bool Fire()
  80. {
  81. GameObject bullet = GameObject.Instantiate(bulletPrefab, bulletSpawn.transform.position,
  82. bulletSpawn.transform.rotation);
  83. bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * 2000);
  84. return true;
  85. }
  86.  
  87.  
  88. [Task]
  89. public void PickDestination(float x, float z)
  90. {
  91. Vector3 dest = new Vector3(x, 0, z);
  92. agent.SetDestination(dest);
  93. Task.current.Succeed();
  94. }
  95.  
  96. [Task]
  97. public void PickRandomDestination()
  98. {
  99. Vector3 dest = new Vector3(Random.Range(-100, 100), 0, Random.Range(-100, 100));
  100. agent.SetDestination(dest);
  101. Task.current.Succeed();
  102. }
  103. [Task]
  104. public void MoveToDestination()
  105. {
  106. if (Task.isInspected)
  107. Task.current.debugInfo = string.Format("t={0:0.00}", Time.time);
  108.  
  109. if(agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending)
  110. {
  111. Task.current.Succeed();
  112. }
  113. }
  114.  
  115. [Task]
  116. bool SeePlayer()
  117. {
  118. Vector3 distance = player.transform.position - this.transform.position;
  119.  
  120. RaycastHit hit;
  121. bool seeWall = false;
  122.  
  123. Debug.DrawRay(this.transform.position, distance, Color.red);
  124.  
  125. if (Physics.Raycast(this.transform.position, distance, out hit))
  126. {
  127. if(hit.collider.gameObject.tag == "wall")
  128. {
  129. seeWall = true;
  130. }
  131. }
  132.  
  133. if (Task.isInspected)
  134. Task.current.debugInfo = string.Format("wall={0}", seeWall);
  135. if (distance.magnitude < visibleRange && !seeWall)
  136. return true;
  137. else
  138. return false;
  139.  
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement