Hi there,
I keep receiving this error while following a tutorial, but I can’t for the life of me figure out where I’ve gone wrong.
Here’s my code! I’d really appreciate any help.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
// Takes and handles input and movement for a player character
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 1f;
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
Vector2 movementInput;
Rigidbody2D rb;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate() {
// If movement input is not 0, try to move
if(movementInput != Vector2.zero){
bool success = TryMove(movementInput);
if(!success) {
success TryMove(new Vector2(movementInput.x, 0)); // If you hit a collision, see if you can slide on the x axis
if (!success) {
success = TryMove(new Vector2(0, movementInput.y)); // If x axis not possible, try on the y axis
}
}
}
}
private bool TryMove(Vector2 direction) {
// Check for potential collisions
int count = rb.Cast(
direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
castCollisions, // List of collisions to store the found collisions into after the Cast is finished
moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
if(count == 0){ // Movement calculation
rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
return true;
} else {
return false;
}
}
void OnMove(InputValue movementValue) {
movementInput = movementValue.Get<Vector2>();
}
}
Here’s the tutorial I’m following if that’s helpful at all (I’m stuck at the section at 44:42)!