/* * ------------------------------------------------------------------------ * Description: This class handles tplayer movement * Author: Eli Simpkins-Simmonds * ------------------------------------------------------------------------ */ using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; public class NewBehaviourScript : MonoBehaviour { [SerializeField] float runSpeed = 10f; // Speed at which the player runs [SerializeField] float jumpSpeed = 10f; // Speed at which the player jumps Vector2 moveInput; // Stores the player's movement input Rigidbody2D myRigidbody; // Reference to the player's Rigidbody2D component Animator myAnimator; // Reference to the player's Animator component Shooter shooter; // Reference to the Shooter component void Awake() { shooter = GetComponent(); // Get the Shooter component attached to the player } void Start() { myRigidbody = GetComponent(); // Get the Rigidbody2D component attached to the player myAnimator = GetComponent(); // Get the Animator component attached to the player } void Update() { Run(); // Call the Run method to handle running FlipSprite(); // Call the FlipSprite method to handle sprite flipping } void OnMove(InputValue value) { moveInput = value.Get(); // Get the movement input from the player Debug.Log(moveInput); // Log the movement input for debugging } void OnJump(InputValue value) { if (value.isPressed) // Check if the jump input is pressed { myRigidbody.velocity += new Vector2(0f, jumpSpeed); // Apply a vertical force to the player's Rigidbody2D } } void Run() { // Calculate the player's velocity based on the movement input and run speed Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y); myRigidbody.velocity = playerVelocity; // Set the player's Rigidbody2D velocity // Check if the player has horizontal speed bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; // Set the "isRunning" parameter in the Animator based on the player's horizontal speed myAnimator.SetBool("isRunning", playerHasHorizontalSpeed); } void FlipSprite() { // Check if the player has horizontal speed bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; if (playerHasHorizontalSpeed) // If the player has horizontal speed, flip the sprite { // Set the local scale of the player to flip the sprite based on the movement direction transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f); } } void OnFire(InputValue value) { if (shooter != null) // Check if the Shooter component is attached to the player { shooter.isFiring = value.isPressed; // Set the isFiring property in the Shooter component based on the fire input } } }