Raycast Help

This is sample of my Pac-Man movement code with a shell for the raycast. Every time I try to make some type of raycast restriction he ends up being stuck at the end of the first lane.

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour 
{
	public Rigidbody2D rb2D;
	public float speed = 0.3f;

	private Vector2 velocity;
	private Animator anim;
	private Vector2 newPos; 

	void Start() 
	{
		newPos = transform.position;
		rb2D = GetComponent<Rigidbody2D>();
		anim = GetComponent <Animator> ();
		velocity = Vector2.left;
	}
	void FixedUpdate() 
	{
		rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);

		if (Input.GetAxisRaw ("Vertical") == (1)) {
			if(valid(Vector2.up))
			{ 
				velocity = Vector2.up;
				anim.SetFloat ("dirY", 1);
				anim.SetFloat ("dirX", 0);
				rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
			}
		}

		if (Input.GetAxisRaw ("Horizontal") == (-1)) {
			if(valid(Vector2.left))
			{ 
				velocity = Vector2.left;
				anim.SetFloat ("dirX", -1);
				anim.SetFloat ("dirY", 0);
				rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
			}
		}

		if (Input.GetAxisRaw ("Vertical") == (-1)) {
			if(valid(Vector2.down))
			{ 
				velocity = Vector2.down;
				anim.SetFloat ("dirY", -1);
				anim.SetFloat ("dirX", 0);
				rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
			}
		}

		if (Input.GetAxisRaw ("Horizontal") == (1)) {
			if(valid(Vector2.right))
			{ 
				velocity = Vector2.right;
				anim.SetFloat ("dirX", 1);
				anim.SetFloat ("dirY", 0);
				rb2D.MovePosition (rb2D.position + velocity * speed * Time.fixedDeltaTime);
			}
		}
	}

	bool valid (Vector2 velocity) 
	{
	
	}
}

This is a photo of where he gets stuck. My colliders are lined up perfectly, and without the raycast he can move around the map perfectly.

I would appreciate some pseudo code, but not the actual code to make it work.

Are you using Physics2D.Raycast() ?

Small side note… GetAxisRaw should return 0 or 1 but in dealing with floats its always good practice to just go ahead and assume you’ll never get an equal number due to floating point precision. I would set this up with <= / >= operators, but I’m OCD. :stuck_out_tongue:

Physics2D.Raycast(stuff)

if (hit) return false;
else return true;