using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { //movement and physics variables private float horizontal; public float speed = 8f; public float normalSpeed = 8f; private float boostedSpeed = 16f; private float boostDuration = 2f; private float jumpingPower = 13f; private float normalJumpingPower = 13f; private float superJump = 20f; private float superJumpDuration = 0.1f; private bool isFacingRight = true; //physics and groundcheck variables [SerializeField] private Rigidbody2D rb; [SerializeField] private Transform groundCheck; [SerializeField] private LayerMask groundLayer; //setting and checking values public float normalGravity = 5f; public float GravitychangeCooldown = 0.5f; public bool isUpsideDown = false; private bool canFlipGravity = true; void Update() { horizontal = Input.GetAxisRaw("Horizontal"); //getting horizontal movement if (Input.GetButtonDown("Jump") && IsGrounded()) //jump logic { if (isUpsideDown) { rb.velocity = new Vector2(rb.velocity.x, -jumpingPower); //reverse jump direction if the player is upside down } else { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); //normal jump direction } } //decrease jump height if the jump is released early if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); } Flip(); //changing the way the player is looking } private void FixedUpdate() //applying movement to players velocity { rb.velocity = new Vector2(horizontal * speed, rb.velocity.y); } private bool IsGrounded() //check if the player is touching the ground using a circle { return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); } private void Flip() //flip the player sprite based on input directions { if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f) { isFacingRight = !isFacingRight; Vector3 localScale = transform.localScale; localScale.x *= -1f; transform.localScale = localScale; } } private void OnTriggerEnter2D(Collider2D collision) { //handling interactions with different paint types if (collision.gameObject.CompareTag("Blue Paint")) { if (canFlipGravity) { FlipGravity(); AudioManager.Instance.PlaySFX("BluePaint"); } } if (collision.gameObject.CompareTag("Orange Paint")) { StartCoroutine(SpeedBoost()); AudioManager.Instance.PlaySFX("Orange Paint"); } if (collision.gameObject.CompareTag("Green Paint")) { AudioManager.Instance.PlaySFX("Green Paint"); } } private void OnTriggerStay2D(Collider2D collision) { //jump boost when the player stays on the green paint if (collision.gameObject.CompareTag("Green Paint")) { StartCoroutine(JumpBoost()); } } private IEnumerator SpeedBoost() //increasing the players speed for a short duration { speed = boostedSpeed; float elapsedTime = 0f; while (elapsedTime < boostDuration) { elapsedTime += Time.deltaTime; speed = Mathf.Lerp(boostedSpeed, normalSpeed, elapsedTime / boostDuration); yield return null; } speed = normalSpeed; } private IEnumerator JumpBoost() //increasing the players jump height for a short duration { jumpingPower = superJump; float elapsedTime = 0f; while (elapsedTime < superJumpDuration) { elapsedTime += Time.deltaTime; jumpingPower = Mathf.Lerp(superJump, jumpingPower, elapsedTime / superJumpDuration); yield return null; } jumpingPower = normalJumpingPower; } private void FlipGravity() //flip the players gravity and rotation { if (rb.gravityScale == normalGravity) { rb.gravityScale *= -1; RotatePlayer(true); } else { rb.gravityScale *= -1; RotatePlayer(false); } } public void RotatePlayer(bool upsideDown) //rotate the player sprite based on gravity direction { if (upsideDown && !isUpsideDown) { transform.Rotate(180f, 0f, 0f); isUpsideDown = true; } else if (!upsideDown && isUpsideDown) { transform.Rotate(180f, 0f, 0f); isUpsideDown = false; } } public void DeathGravityReturn() //resetting the players gravity and rotation on death { if (rb.gravityScale != normalGravity) { transform.rotation = Quaternion.identity; isUpsideDown = false; rb.gravityScale *= -1; } } }