| 123456789101112131415161718192021222324252627282930313233343536 |
- using UnityEngine;
- public class Shooter : MonoBehaviour
- {
- public AudioSource hitEnemy;
- public GameManager gameManager;
- void Update()
- {
-
- if
- (
- (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
- ||
- (Input.GetMouseButtonDown(0))
- )
- FireRaycast();
- }
- void FireRaycast()// Shoot line from the center of screen
- {
- Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- // Has hit an Enemy?
- if (hit.transform.tag == "Enemy" && gameManager.gameRunning)
- {
- hitEnemy.Play();
- Destroy(hit.transform.gameObject);
- GameManager.gameManager.AddPoints();
- }
- }
- }
- }
|