Here, should you need the code
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D myRb2d;
Animator myAnim;
CapsuleCollider2D myCapsuleCollider;
/* Remember to come back to this! If need be, check 92. Prevent Wall Jump video to
be able to figure out how to not jump more than once of a wall. Commented lines are
the code that Rick used on his end. */
// BoxCollider2D myBoxCollider;
[SerializeField] float runSpeed = 2.5f;
[SerializeField] float jumpSpeed = 3f;
[SerializeField] float climbSpeed = 3f;
float gravityScaleAtStart;
// Start is called before the first frame update
void Start()
{
myRb2d = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
myCapsuleCollider = GetComponent<CapsuleCollider2D>();
//myBoxCollider = GetComponent<BoxCollider2D>();
gravityScaleAtStart = myRb2d.gravityScale;
}
// Update is called once per frame
void Update()
{
Run();
FlipPlayer();
ClimbLadder();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void OnJump(InputValue value)
{
if(!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
if (value.isPressed)
{
myRb2d.velocity += new Vector2(0f, jumpSpeed);
}
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRb2d.velocity.y);
myRb2d.velocity = playerVelocity;
//Flip the state to running
bool playerHasHorizontalSpeed = Mathf.Abs(myRb2d.velocity.x) > Mathf.Epsilon;
myAnim.SetBool("isRunning", playerHasHorizontalSpeed);
}
void FlipPlayer()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRb2d.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRb2d.velocity.x), 1f);
}
}
void ClimbLadder()
{
if (!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
{
myRb2d.gravityScale = gravityScaleAtStart;
myAnim.SetBool("isClimbing", false);
return;
}
Vector2 climbVelocity = new Vector2(myRb2d.velocity.x,moveInput.y * climbSpeed);
myRb2d.velocity = climbVelocity;
myRb2d.gravityScale = 0f;
bool playerHasVerticalSpeed = Mathf.Abs(myRb2d.velocity.y) > Mathf.Epsilon;
myAnim.SetBool("isClimbing", playerHasVerticalSpeed);
}
}