2D Player sticking to 2D Box Colliders and being unresponsive
I'm attempting to re-create Pac-man. I've given my maze several Box Collider 2Ds to identify the boundaries, my Pac-man has a Rigidbody 2D and a Circle Collider 2D.
The issue I've come across is whenever Pac-man collides with any of the box colliders, he sticks to them and is unable to move; he cannot switch direction or change animations, he just sits unresponsive to any control, not even a flicker.
I've tried changing Body Type and Collision Detection, creating a Physics Material 2D and setting its values to 0 does nothing when added to Pac-man (or the maze when adding a Rigidbody 2D). I cannot seem to find "Min Penetration" in my Project Settings. Changing the colliders on either Game Object, to different types also yields no result.
Thus, I am officially at a loss, so any advice or alternative would be kindly appreciated. Thanks in advance~
My code is as follows (borrowed from this Link):
using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float movementSpeed = 0.4f;
Vector2 destination = Vector2.zero;
void Start() {
destination = transform.position;
}
void FixedUpdate() { // Move closer towards destination
Vector2 p = Vector2.MoveTowards(transform.position, destination, movementSpeed);
GetComponent<Rigidbody2D>().MovePosition(p);
// Check for Input while not moving
if ((Vector2)transform.position == destination) {
if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
destination = (Vector2)transform.position - Vector2.right;
if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
destination = (Vector2)transform.position + Vector2.right;
if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
destination = (Vector2)transform.position + Vector2.up;
if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
destination = (Vector2)transform.position - Vector2.up;
}
// Animation parameters
Vector2 dir = destination - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("Horizontal", dir.x);
GetComponent<Animator>().SetFloat("Vertical", dir.y);
}
bool valid(Vector2 dir) { // Cast Line from Pacman to next to pacman
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
}