TeHArGiS10

Untitled

Aug 13th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AI : MonoBehaviour {
  5.  
  6. private Transform mainUFO;
  7. public GameObject laserBeam;
  8. public float turnSpeed = 5f;
  9. public float shootingForce = 20000f;
  10. public float hookDistance = 5f;
  11. public bool inDelay;
  12. public float speed = 10f;
  13. RaycastHit hit;
  14. Rigidbody rb;
  15. AudioSource audioSource;
  16.  
  17. void Start()
  18. {
  19. mainUFO = GameObject.FindGameObjectWithTag("mainUFO").transform;
  20. audioSource = GetComponent<AudioSource>();
  21. inDelay = false;
  22. }
  23.  
  24. void FixedUpdate ()
  25. {
  26. float maxDistance = Vector3.Distance(mainUFO.transform.position, transform.position);
  27.  
  28. if (!inDelay)
  29. {
  30. Quaternion targetRotation = Quaternion.LookRotation(mainUFO.transform.position - transform.position);
  31. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.fixedDeltaTime);
  32.  
  33. if (maxDistance >= 30)
  34. {
  35. transform.position = Vector3.MoveTowards(transform.position, mainUFO.transform.position, Time.fixedDeltaTime * speed);
  36. }
  37. }
  38.  
  39. if (Vector3.Angle(transform.forward, mainUFO.transform.position - transform.position) < hookDistance)
  40. {
  41. if (!inDelay)
  42. {
  43. StartCoroutine(shootingDelay());
  44. }
  45. }
  46. }
  47.  
  48. IEnumerator shootingDelay ()
  49. {
  50. inDelay = true;
  51. yield return new WaitForSeconds(1);
  52.  
  53. Transform shootingStart = gameObject.transform.GetChild(1);
  54. GameObject newLaserBeam = Instantiate(laserBeam, new Vector3(shootingStart.transform.position.x, shootingStart.transform.position.y, shootingStart.transform.position.z), shootingStart.transform.rotation) as GameObject;
  55.  
  56. rb = newLaserBeam.GetComponent<Rigidbody>();
  57. rb.AddForce(transform.forward * shootingForce);
  58. audioSource.Play();
  59.  
  60. if (Vector3.Angle(transform.forward, mainUFO.transform.position - transform.position) < hookDistance)
  61. {
  62. print("hit!");
  63. }
  64.  
  65. yield return new WaitForSeconds(1);
  66. inDelay = false;
  67. Object.Destroy(newLaserBeam);
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment