- Home /
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 :)
$$anonymous$$ay I ask why you wish to create your own collision system? Unity has 2D colliders that helps out a lot..
Ahhh sorry I forget to say that I use a Unity 2DBoxcollider for all my objects
Answer by revolute · Oct 02, 2014 at 07:44 AM
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.
Yeah thats right, you got my code^^ But a small question which solution do you mean?^^ The link is to the 2D Physics mainpage^^ What parts of the physics do I need? (I already use 2d-Boxcolliders and a Ridigidbody2D)
When your objects with 2d colliders and 2d rigidbody actually collides, OnCollisionEnter2D is called and tells you what has collided. Parameter is the object that this object collided into.
Yeah I think OnCollisionEnter2D would be very helpful. But I have to compare my movement direction with my colliding Object, so that I can get, if the object is in front or not. I mean: If I only query the enter of a collision, my movement script will be always off from the first hit. And I cant move to the left or to the right to avoid my obstacle. So i have to compare it with my movement direction in someway^^
Do you have any ideas how to realize this with my 8-way?^^ (And thank you so much for your help :) )
You can get colliding points. Collision2D contains ContactPoints2D which contains Vector2 of collision point. Collision2D and ContactPoints2D and Vector2
Your answer

Follow this Question
Related Questions
Custom Collision Detection 4 Answers
Collision Detection If Raycast Source Is inside A Collider? 4 Answers
Need help with third-person camera stuttering involving raycasts and clamp 0 Answers
AI create path, help. 1 Answer
Help with a field of sight 3 Answers