Code:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private LayerMask groundLayer;
private Rigidbody2D body;
private Animator anim;
private BoxCollider2D boxCollider;
private void Awake()
{
//Grab references for RigidBody and animator from object
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
//Flip player when moving left-right
if (horizontalInput > 0.02f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
if (Input.GetKey(KeyCode.Space) && isGrounded())
Jumping();
//Set animator parameters
anim.SetBool("Run", horizontalInput != 0);
anim.SetBool("Grounded", isGrounded);
}
private void Jumping()
{
body.velocity = new Vector2(body.velocity.x, speed);
anim.SetTrigger("jump");
}
private void OnCollisionEnter2D(Collision2D collision)
{
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return false;
}
}
I’m new to Unity and coding so help would be much appreciated. I was following a tutorial but I don’t know how to fix this error.