| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class PlayerController : MonoBehaviour
- {
- public GameManager gameManager;
- public InputActionReference screenTabAction;
- public GameObject projectilePrefab;
- public float projectileSpeed = 1;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- if (gameManager == null)
- {
- gameManager = FindFirstObjectByType<GameManager>();
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (screenTabAction.action.WasPressedThisFrame()) {
- ShootProjectile();
- }
- }
- private void ShootProjectile()
- {
- if (gameManager != null)
- {
- if (gameManager.GetGameState() != GameState.GAME_RUNNING)
- {
- return;
- }
- }
- Debug.Log("Projectile Shot!");
- Vector3 spawnPosition = Camera.main.transform.position;
- Vector3 direction = Camera.main.transform.forward;
- GameObject spawnedProjectile = Instantiate(projectilePrefab);
- spawnedProjectile.transform.position = spawnPosition;
- Projectile projectileScript = spawnedProjectile.GetComponent<Projectile>();
- if (projectileScript != null) {
- projectileScript.SetValues(projectileSpeed, direction);
- }
- GameObject dynamicObject = GameObject.Find("_Dynamic");
- if (dynamicObject != null) {
- spawnedProjectile.transform.SetParent(dynamicObject.transform);
- }
- }
- }
|