Guest User

Untitled

a guest
Aug 23rd, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1.  
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class pController : MonoBehaviour
  8.  
  9. {
  10.  
  11. //used to move the reticle
  12. [SerializeField]
  13. private GameObject _reticle;
  14. public float _reticleSpeed = 5.0f;
  15. [SerializeField]
  16. private Vector3 _reticlePos;
  17. public Renderer _reticleRenderer;
  18. public float _blinkRate=0.5f;
  19.  
  20. //missle shit
  21. [SerializeField]
  22. private GameObject _explosion;
  23. public GameObject _missle;
  24. public float _missleSpeed = 0.5f;
  25. public GameObject[] _missleSilo;
  26. public Rigidbody _missleRigidbody;
  27. //[SerializeField]
  28. //private HashSet<GameObject,bool> _missleSilo;
  29.  
  30. // Start is called before the first frame update
  31. void Start()
  32. {
  33. _reticlePos = transform.position;
  34. // _missle = GetComponent<GameObject>();
  35. _missleRigidbody = _missle.GetComponent<Rigidbody>();
  36. _reticleRenderer = _reticle.GetComponent<Renderer>();
  37. StartCoroutine(BlinkReticle());
  38.  
  39. }
  40.  
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. MoveReticle();
  45. FireMissle();
  46. // BlinkReticle();
  47. }
  48. //reticle blink
  49. IEnumerator BlinkReticle()
  50. { while (true)
  51. {
  52. _reticleRenderer.enabled = false;
  53. yield return new WaitForSeconds(_blinkRate);
  54. _reticleRenderer.enabled = true;
  55. yield return new WaitForSeconds(_blinkRate);
  56. }
  57. }
  58. void MoveReticle()
  59. {
  60.  
  61.  
  62. //use WASD or mouse to move reticle
  63. if (Input.GetKey(KeyCode.W))
  64. {
  65. //transform.position
  66. _reticlePos+= new Vector3(0,1,0)* _reticleSpeed * Time.deltaTime;
  67. }
  68. if(Input.GetKey(KeyCode.S))
  69. {
  70. _reticlePos+= new Vector3(0, -1, 0) * _reticleSpeed * Time.deltaTime;
  71. }
  72. if (Input.GetKey(KeyCode.A))
  73. {
  74. _reticlePos += new Vector3(-1, 0, 0) * _reticleSpeed * Time.deltaTime;
  75. }
  76. if (Input.GetKey(KeyCode.D))
  77. {
  78. _reticlePos += new Vector3(1, 0, 0) * _reticleSpeed * Time.deltaTime;
  79. }
  80. //clamp the reticle to the screen for x it is 16 and -16
  81. _reticlePos.x = Mathf.Clamp(_reticlePos.x , -16.0f, 16.0f);
  82. //claimp the reticle to the screen for y it is 6 and -6
  83. _reticlePos.y = Mathf.Clamp(_reticlePos.y, -4.4f, 6.0f);
  84.  
  85. transform.position = _reticlePos;
  86. }
  87.  
  88. void FireMissle()
  89. {
  90. //use mouse or space to fire missle
  91. //if all three missles silos are fired, do nothing
  92. //starting position will be from an inactive missle silo
  93.  
  94. if (Input.GetKeyDown(KeyCode.Space))
  95. {
  96. int i = Random.Range(0, _missleSilo.Length-1);
  97. Vector3 targetPos = _reticlePos;
  98.  
  99. Vector3 currPos = _missleSilo[i].transform.position;
  100. Vector3 direction = targetPos -currPos ;
  101.  
  102. Instantiate(_missle, _missleSilo[i].transform.position , Quaternion.identity);
  103. // _missleRigidbody.velocity = r.normalized * _missleSpeed*Time.deltaTime;
  104. // _missleRigidbody.velocity = direction.normalized * _missleSpeed*Time.deltaTime;
  105.  
  106. // _missleRigidbody.AddForce(m.normalized * _missleSpeed, ForceMode.Impulse);
  107. //_missleRigidbody.AddForce( Vector3.up * _missleSpeed);
  108. _missle.transform.Translate((direction) * _missleSpeed * Time.deltaTime);
  109.  
  110. // _missle.transform.LookAt(_reticlePos);
  111.  
  112. // _missle.transform.position += direction * _missleSpeed * Time.deltaTime;
  113. }
  114. }
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
Add Comment
Please, Sign In to add comment