#How to detect the right collider? Top-Down 2D

1 messages · Page 1 of 1 (latest)

gray flax
#

I have a box collider on my enemy gameObject, used for colliding with terrain. Then I have a empty child object under the enemy called "Hitbox" with a box collider I want to use to detect bullets from the player. The "hitbox" collider has IsTrigger = enabled, whilst the terrain collider doesn't.

Using this code, the bullet only detects the terrain collider, whilst the bullets go through on the larger collider. For info, this is a top-down 2D game.

How do I make it so the bullet detects the child gameObject called "hitbox" instead of the box collider on the enemy parent object?

Thanks!sadok

#

I was also wondering why OnTriggerEnter2D gets triggered when the terrain collider has isTrigger = false

#

whilst the hitbox has it set to true

hallow rivet
#

You need a phyics layer for the hitboxes and another one for the terrain

#

Set the hitbox layer to only collide with terrain and other hitboxes

hallow rivet
#

Your scenario (assuming the bullets have rigidbodies on them) would fall into Static + Rigidbody Trigger which will send trigger messages

gray flax
# hallow rivet OnTriggerEnter gets called even if the other collider is not a trigger. You can ...

I now added two layers, one called Hitboxes and one called TerrainCollision.
Only the empty gameObject called Hitbox is set to Hitboxes, as well as the bullet prefab. The bullet still does not register the impact on the Hitbox gameobject.

Note: The empty hitbox gameObject with the box collider does not have a Rigidbody2D. However, its parent gameObject (the enemy object) does.
The bullet prefab has a dynamic Rigidbody, and so does the enemy.

hallow rivet
#

Did you configure the collision matrix?

gray flax
#

Yes, this is how I set it up

hallow rivet
#

So the hitboxes should only collide with each other and the terrain

gray flax
#

Both the bullet and the empty gameobject with the enemy hitbox is set to Hitboxes layer

hallow rivet
#

Yeah, it's weird you aren't hitting it at all

#

Maybe your bullet is getting blocked by the normal enemy collider?

gray flax
#

I am sure it's something I've forgot to clarify but not sure what

#

No, now it goes right through. It destroys the bullet gameobject on collision, which it currently doesnt do

hallow rivet
#

But they still collide with the terrain?

gray flax
#

No, it does not collide with the terrain regardless of which layer the terrain is in

#

uh for some reason

hallow rivet
#

Can you show the inspector for the bullet?

#

Also I noticed that your bullet class inherits from PlayerStats, is that intended?

gray flax
hallow rivet
#

It looks fine, and I'm guessing the circle collider just has isTrigger enabled

gray flax
hallow rivet
#

Maybe the size of the collider is messed up?

hallow rivet
#

Wait, I think I found it

#

The EnemyBehaviour script is on the parent of the hitbox, right?

gray flax
#

Correct

#

its on the enemy object and not the empty box collider object

hallow rivet
#

You are only checking for EnemyBehaviour components on the other gameObject, not on it's parent

gray flax
#

The bullet still has isTrigger = True. Also, the script does not have a reference to the Enemybehavior script!

#

It's not assigned in the inspector because it depends on which enemy ur shooting

#

How do I get that?

hallow rivet
#
var enemyBehaviour = col.gameObject.GetComponentInParent<EnemyBehaviour>();
if (enemyBehaviour != null) {
  enemyBehaviour.TakeDamage(attack);
  Destroy(gameObject);
}
#

You should probably move the Destroy outside the if statement, so the bullet gets destroyed when it hits something, even it if wasn't an enemy

gray flax
#

The type or namespace name 'EnemyBehaviour' could not be found (are you missing a using directive or an assembly reference?)

hallow rivet
#

I spelled Behaviour differently :D

#

Remove the u

gray flax
#

I did, and no errors!

#

The bullet doesnt collide with anything though

#

what on earth, it should work

hallow rivet
#

Put a Debug.Log at the top of OnTriggerEnter2D and see if it logs anything

gray flax
hallow rivet
#

idk, everything looks fine

#

How are you moving your bullets?

gray flax
hallow rivet
#

You can try enabling continous collision detection on your bullet's rigidbody

gray flax
#

Changed it from discrete to continuous, still no interaction. Should the bullet also have the IsTrigger enabled?

hallow rivet
#

Yeah, otherwise you dont get OnTriggerEnter2D messages

#

But you already have it judging by the video

gray flax
#

Yeah so both colliders need the trigger?

hallow rivet
#

No, not exactly

gray flax
#

So the dynamic bullet hits the none/static hitbox, so the IsTrigger square is green. Green for which object? Sorry but that link was confusing to me, which is why I didn't really understand

hallow rivet
#

It's green for the object with the trigger

#

or both if both of them are triggers

#

Your bullets and enemies are both rigidbody triggers, so it should work

gray flax
#

The enemy has a rigidbody yes, but the empty gameobject with the box collider that I want the bullet to interact with, does not

hallow rivet
#

The parent does though

#

If you open the "Info" section on the collider you will see the connected rigidbody

gray flax
hallow rivet
#

Ohh, that's a 3d collider

gray flax
#

oh my god

hallow rivet
#

that is brutal

gray flax
#

thank you so much for the help! Though now I still shoot over terrain

hallow rivet
#

Also with the code I sent you the bullets only disappear if they hit an enemy

gray flax
#

yeah I changed it

hallow rivet
#

👍

gray flax
#

so it destroys on collision

#

But thanks! Ill add another layer for the terrain so it passes through the tag on the player but not on the terrain

hallow rivet
#

Alright

#

Sorry for missing the 3d collider in the video

gray flax
#

no, its my fault lol