# Collision InFront?

Hey guys

I have the following Situation:

Iâ€™m working on a topdown 2D-Game with an 8-Way Movementsystem. So I wanted to write a script that checks, if the collision which my raycast notifies is in the Front of my GO. (Thats for things like stop moving when your walking against a wall and so on)

The â€śCheck in Frontâ€ť Script consists of 3 parts.

At first my â€śGetCollisionDirectionâ€ť void in my raycast script.
(The problem here is that I cant make any get-functions from the other scripts because I use the same raycastscript for all my GOs)
The raycast script works and set the bool from the direction, which the collision comes from, to true.

``````    	void GetCollisionDirection()
{
if (collisionAny == true) {
if (collisionUpLeft == true) {
collisionDirection = 8;
}
if (collisionDownLeft == true) {
collisionDirection = 7;
}
if (collisionDownRight == true) {
collisionDirection = 6;
}
if (collisionUpRight == true) {
collisionDirection = 5;
}
if (collisionRight == true) {
collisionDirection = 4;
}
if (collisionLeft == true) {
collisionDirection = 3;
}
if (collisionUp == true) {
collisionDirection = 2;
}
if (collisionDown == true) {
collisionDirection = 1;
}

//Check if more than 1 true
//The next part is my main problem I think :(
if (collisionDown == true && collisionDownRight == true && collisionDownLeft == true)
{
collisionDirection = 1;
}
if (collisionUp == true && collisionUpRight == true && collisionUpLeft == true)
{
collisionDirection = 2;
}
if (collisionLeft == true && collisionDownLeft == true && collisionUpLeft == true)
{
collisionDirection = 3;
}
if (collisionRight == true && collisionDownRight == true && collisionUpRight == true)
{
collisionDirection = 4;
}
if((collisionUp == true && collisionUpRight == true && collisionRight == true) ||
(collisionUp == true && collisionUpRight == true && collisionUpLeft == false) ||
(collisionRight == true && collisionUpRight == true && collisionDownRight == false))
{
collisionDirection = 5;
}
if((collisionDown == true && collisionDownRight == true && collisionRight == true) ||
(collisionDownRight == true && collisionRight == true && collisionUpRight == false) ||
(collisionDown == true && collisionDownRight == true && collisionDownLeft == false))
{
collisionDirection = 6;
}
if((collisionDown == true && collisionDownLeft == true && collisionLeft == true)	||
(collisionDown == true && collisionDownLeft == true && collisionDownRight == false) ||
(collisionLeft == true && collisionDownLeft == true && collisionUpLeft == false))
{
collisionDirection = 7;
}
if((collisionUp == true && collisionUpLeft == true && collisionLeft == true) ||
(collisionLeft == true && collisionUpLeft == true && collisionDownLeft == false) ||
(collisionUp == true && collisionUpLeft == true && collisionUpRight == false))
{
collisionDirection = 8;
}

} else {
collisionDirection = 0;
}
}
``````

The next part is in the GO Movement-Script. This part compares the collisionDirection from my raycasting script and the direction the player walks at the moment.

``````void CompareCollisionAndWalkDirection() {

coldirection = raycasting.collisionDirection;

if (coldirection > 0) {
if (direction == coldirection) {
collisionInFront = true;
}
else {
collisionInFront = false;
}
}
else {
collisionInFront = false;
}
}
``````

And the last part is also in the Movement-Script.
This part is very small and only sets the movement to stop the In-front is true.

``````	void checkCollisions()
{

if (collisionInFront == true) {
walkSpeed = 0f;
} else {
walkSpeed = 5.0f
}

}
``````

Sooooo and now finally to my problem. The script work and the player stops at a wallâ€¦
But my problem are edges. If I collide against a without a edge all works like I wish.
But if there are an edge I cant set the collisionDirection in my raycasting script right.

So to be short: How can I write my â€śGetCollisionDirectionâ€ť void so that it works than more the 1 collision is true. It have to work with 8 directions. My solution donâ€™t work very well, as you see

Okay I know my English is not the best, so I made small picture that describes my problem^^
The green Box is the Player and the yellow arrow is his movment direction.
The red dots are his raycast colliders and the blue Line is the wall or something like that^^

I realy thank you for your time

I understood where your code went wrong.

First of all, your picture is this in the code :
collisionUp == true && collisionUpLeft == true && collisionUpRight == false

This unfortunately can occur when moving in all 2,5,8 directions. Collision is simply not limited to moving in direction 8.

However, there is an easy solution if you simply want objects to stop on collision.