- Home /
Get information on ignored collision
I'm trying to program a low wall in a 2D game. The walls I got so far consist of a sprite and a polygon collider. This works fine, the player can't walk through walls. And shots are stopped by the wall, too.
Here's a screenshot:
It's really difficult to get a screenshot fast enough to see the shot before the collision. This is the best I managed. You view the scene from the top down, the objects are drawn in some kind of pseudo 3d. The player can move up/down, right/left. Collisions are registered by a box collider at his "feet". The walls have colliders, too. It gets complicated with the shot. The actual position of the shot is the small grey circle below. That's supposed to be its "shadow". The shadow has a circle collider which is used for collisions. The white v shaped sprite is the shot itself, it's just a simple sprite, child to the shadow gameobject. No collision detection. That all happens on the shadow. The shadow is responsible for movement, too. As time passes the local y coordinate of the shot-sprite child is reduced. It looks as though the shot is falling down.
But now I need to create a low wall. This is actually rather difficult. The player must not walk through this wall, but shots can fly over it. I've set up a special layer for this type of wall and am ignoring collisions with the shots. But I would like to check if the shot is actually high enough to fly over this wall. The shots are falling down really fast. The movement of the shot is implemented with a script that controls speed, orientation, collisions and height. When a collision happens, the height of the shot should be compared to the height of the collider. That means that I need to register collisions between the low wall and the shot.
I didn't find a way to ignore the collision after it happened. Optimally I would like to do something like this:
void OnCollisionEnter2D(Collision2D collision){
InfoScript info = collision.gameobjet.getComponent<Infoscript>();
if(info.height < this.height){
// ignore collision
}
else{
Destroy (gameObject, 0.5f);
}
}
But that doesn't seem to be possible. My next idea was to create a trigger and use OnTrigger to check if a collision happened. But it seems like I can only add one PolygonCollider2D. I would need two: One real collider to stop the player from walking through walls. And one trigger to check for collisions with shots
How can I solve this ?
Answer by lhk · Apr 11, 2014 at 11:33 AM
Works now. I just added a child GameObject with the new polygon collider and the info script.
Your answer
Follow this Question
Related Questions
Jitter/vibration on an object with constant velocity when using 2D Pixel Perfect Camera 1 Answer
Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 1 Answer
How do you detect if two specific objects are touching each other without collision. 3 Answers