Shooter.cs 886 B

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. public class Shooter : MonoBehaviour
  3. {
  4. public AudioSource hitEnemy;
  5. public GameManager gameManager;
  6. void Update()
  7. {
  8. if
  9. (
  10. (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  11. ||
  12. (Input.GetMouseButtonDown(0))
  13. )
  14. FireRaycast();
  15. }
  16. void FireRaycast()// Shoot line from the center of screen
  17. {
  18. Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  19. RaycastHit hit;
  20. if (Physics.Raycast(ray, out hit))
  21. {
  22. // Has hit an Enemy?
  23. if (hit.transform.tag == "Enemy" && gameManager.gameRunning)
  24. {
  25. hitEnemy.Play();
  26. Destroy(hit.transform.gameObject);
  27. GameManager.gameManager.AddPoints();
  28. }
  29. }
  30. }
  31. }