Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class pController : MonoBehaviour
- {
- //used to move the reticle
- [SerializeField]
- private GameObject _reticle;
- public float _reticleSpeed = 5.0f;
- [SerializeField]
- private Vector3 _reticlePos;
- public Renderer _reticleRenderer;
- public float _blinkRate=0.5f;
- //missle shit
- [SerializeField]
- private GameObject _explosion;
- public GameObject _missle;
- public float _missleSpeed = 0.5f;
- public GameObject[] _missleSilo;
- public Rigidbody _missleRigidbody;
- //[SerializeField]
- //private HashSet<GameObject,bool> _missleSilo;
- // Start is called before the first frame update
- void Start()
- {
- _reticlePos = transform.position;
- // _missle = GetComponent<GameObject>();
- _missleRigidbody = _missle.GetComponent<Rigidbody>();
- _reticleRenderer = _reticle.GetComponent<Renderer>();
- StartCoroutine(BlinkReticle());
- }
- // Update is called once per frame
- void Update()
- {
- MoveReticle();
- FireMissle();
- // BlinkReticle();
- }
- //reticle blink
- IEnumerator BlinkReticle()
- { while (true)
- {
- _reticleRenderer.enabled = false;
- yield return new WaitForSeconds(_blinkRate);
- _reticleRenderer.enabled = true;
- yield return new WaitForSeconds(_blinkRate);
- }
- }
- void MoveReticle()
- {
- //use WASD or mouse to move reticle
- if (Input.GetKey(KeyCode.W))
- {
- //transform.position
- _reticlePos+= new Vector3(0,1,0)* _reticleSpeed * Time.deltaTime;
- }
- if(Input.GetKey(KeyCode.S))
- {
- _reticlePos+= new Vector3(0, -1, 0) * _reticleSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.A))
- {
- _reticlePos += new Vector3(-1, 0, 0) * _reticleSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.D))
- {
- _reticlePos += new Vector3(1, 0, 0) * _reticleSpeed * Time.deltaTime;
- }
- //clamp the reticle to the screen for x it is 16 and -16
- _reticlePos.x = Mathf.Clamp(_reticlePos.x , -16.0f, 16.0f);
- //claimp the reticle to the screen for y it is 6 and -6
- _reticlePos.y = Mathf.Clamp(_reticlePos.y, -4.4f, 6.0f);
- transform.position = _reticlePos;
- }
- void FireMissle()
- {
- //use mouse or space to fire missle
- //if all three missles silos are fired, do nothing
- //starting position will be from an inactive missle silo
- if (Input.GetKeyDown(KeyCode.Space))
- {
- int i = Random.Range(0, _missleSilo.Length-1);
- Vector3 targetPos = _reticlePos;
- Vector3 currPos = _missleSilo[i].transform.position;
- Vector3 direction = targetPos -currPos ;
- Instantiate(_missle, _missleSilo[i].transform.position , Quaternion.identity);
- // _missleRigidbody.velocity = r.normalized * _missleSpeed*Time.deltaTime;
- // _missleRigidbody.velocity = direction.normalized * _missleSpeed*Time.deltaTime;
- // _missleRigidbody.AddForce(m.normalized * _missleSpeed, ForceMode.Impulse);
- //_missleRigidbody.AddForce( Vector3.up * _missleSpeed);
- _missle.transform.Translate((direction) * _missleSpeed * Time.deltaTime);
- // _missle.transform.LookAt(_reticlePos);
- // _missle.transform.position += direction * _missleSpeed * Time.deltaTime;
- }
- }
- }
Add Comment
Please, Sign In to add comment