2d collision with "unwalkable" objects

Hello everyone.

I’m working on a 2D collision script inspired from this tutorial.

I have a player controlled with the key up, down, right and left, and a gameObject with the layer “unwalkable” (layer number 8). What I want is simple: when the player box collider hit the unwalkable box collider, the player is stopped.

I have two problems:
Currently when the player hit the unwalkable object, he’s stopping… but not always. For example when the player’s bottom hit the unwalkable’s top, the player run through it:

alt text

Both box collider should not superimposed.

The second problem:
for example the player is moving to the top and hit and object, so the player is stopped. But when the player is hitting the object while moving top-right or top-left, he run through it.

Here’s an extract of my code:

private RaycastHit hitInfo;
private float halfMyX = 18f;
private float halfMyY = 24f;

private int unwalkable = 1 << 8;

private float absVel2X;
private float absVel2Y;

public virtual void characterMove()
	{
		speed.x = 0;
		speed.y = 0;
		
		if (up)
		{
			speed.y = charSpeed;	
		}
		if (down)
		{
			speed.y = -charSpeed;	
		}
		if (right)
		{
			speed.x = charSpeed;
		}
		if (left)
		{
			speed.x = -charSpeed;	
		}
		
		checkBlocked();
		
		speed2 = speed * Time.deltaTime;
		transf.position += new Vector3(speed2.x,speed2.y,0f);
	}

public void checkBlocked()
{
	absVel2X = Mathf.Abs(speed2.x);
	absVel2Y = Mathf.Abs(speed2.y);
	
	// blocked right?
	if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), Vector3.right, out hitInfo, halfMyX+absVel2X, unwalkable))
	{
		if (currentFacing == direction.right || currentDirection == direction.right)
		{
			speed.x = 0;
		}
	}
	//blocked left?
	if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), -Vector3.right, out hitInfo, halfMyX+absVel2X, unwalkable))
	{
		if (currentFacing == direction.left || currentDirection == direction.left)
		{
			speed.x = 0;
		}
	}
	//blocked up
	if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), Vector3.up, out hitInfo, halfMyY+absVel2Y, unwalkable))
	{
		if (currentFacing == direction.up || currentDirection == direction.up)
		{
			speed.y = 0;
		}
	}
	//blocked down
	if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), -Vector3.up, out hitInfo, halfMyY+absVel2Y, unwalkable))
	{
		if (currentFacing == direction.down || currentDirection == direction.down)
		{
			speed.y = 0;
		}
	}
}

Here’s how the script works:

  • If the button “up” is pushed, the variable “up” is true. Then the speed is increased.
  • The checkBlocked() method is called. It set the speed to 0 if the player collide with an unwalkable object.

I could not found a solution for this two problems, maybe I don’t know where to search (I tried to find an example of script doing what I want, witch is, I guess, a basic thing…).

Does someone have a solution?

Thank you for your time!

I’m working on a 2D game myself.
It might be just super easier, if you used character controller.
It will simply take care of your character movement.

The problem in this tute tho, add

Debug.Log(“your message here”);

to any suspicious place and see which function is not working for you.
Hope this helps.

Nevermind, I checked Is Trigger for the box collider of the object, and my player can walk though!