using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] private float speed = 1.0f; [SerializeField] private GameObject laserPrefab; [SerializeField] private float fireRate = 0.15f; private float nextFire = -1f; [SerializeField] private int lives = 3; private SpawnManager spawnManager; void Start() { transform.position = new Vector3(0, 0, 0); spawnManager = GameObject.Find("SpawnManager").GetComponent(); if(spawnManager == null ) { Debug.LogError("Spawn Manager is null"); } } void Update() { CalculateMovement(); if (Input.GetButton("Fire1") && Time.time > nextFire) { ShootLaser(); } } private void ShootLaser() { nextFire = Time.time + fireRate; Instantiate(laserPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity); } private void CalculateMovement() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * speed * Time.deltaTime); /* Set boundaries on top and bottom if (transform.position.y >= 0) { transform.position = new Vector3(transform.position.x, 0, 0); } else if (transform.position.y < -3.8f) { transform.position = new Vector3(transform.position.x, -3.8f, 0); } */ // Same as the if statement above but cleaner. transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0); // Pass through on the left and right side if (transform.position.x >= 11.3f) { transform.position = new Vector3(-11.3f, transform.position.y, 0); } else if (transform.position.x < -11.3f) { transform.position = new Vector3(11.3f, transform.position.y, 0); } } public void Damage() { lives--; if (lives < 1) { spawnManager.OnPlayerDeath(); Destroy(this.gameObject); } } }