PlayerController.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class PlayerController : MonoBehaviour
  4. {
  5. public GameManager gameManager;
  6. public InputActionReference screenTabAction;
  7. public GameObject projectilePrefab;
  8. public float projectileSpeed = 1;
  9. // Start is called once before the first execution of Update after the MonoBehaviour is created
  10. void Start()
  11. {
  12. if (gameManager == null)
  13. {
  14. gameManager = FindFirstObjectByType<GameManager>();
  15. }
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. if (screenTabAction.action.WasPressedThisFrame()) {
  21. ShootProjectile();
  22. }
  23. }
  24. private void ShootProjectile()
  25. {
  26. if (gameManager != null)
  27. {
  28. if (gameManager.GetGameState() != GameState.GAME_RUNNING)
  29. {
  30. return;
  31. }
  32. }
  33. Debug.Log("Projectile Shot!");
  34. Vector3 spawnPosition = Camera.main.transform.position;
  35. Vector3 direction = Camera.main.transform.forward;
  36. GameObject spawnedProjectile = Instantiate(projectilePrefab);
  37. spawnedProjectile.transform.position = spawnPosition;
  38. Projectile projectileScript = spawnedProjectile.GetComponent<Projectile>();
  39. if (projectileScript != null) {
  40. projectileScript.SetValues(projectileSpeed, direction);
  41. }
  42. GameObject dynamicObject = GameObject.Find("_Dynamic");
  43. if (dynamicObject != null) {
  44. spawnedProjectile.transform.SetParent(dynamicObject.transform);
  45. }
  46. }
  47. }