[SOLVED] 2D sprites collision not working

I have two 2D sprites that both have collision, one is a car sprite, another one is just a coin with sprite, and I added collision to both of them and added collision triggerenter to the coin to detect when the coin collides with the car, if the coin collides, it should just disappear but its not, no collision is getting detected, the coin passes through the car with no collision. The code has nothing special, its just 2 collision boxes with no rigidbody has one of them has collision trigger enter code

Hi @sir_akc,

Can you try adding the trigger enter on your car and try again? If the coin is entering the car the event will only fire on the collision component acting as the receiving trigger.

I slightly remember there was a feature request to have it fire on both entities, but I am unsure if it was implemented.

man that needs to change

1 Like

ok I just added the same script to the car and the car did nothing, both scripts are initiated tho, I can see that in the console, but neither is doing anything

Are you sure there is no rigidbody needed to activate a trigger?

Yeah, for trigger events collision components should be enough.

@sir_akc try posting a sample project so we can take a look.

I think the car need a rigidbody.

https://playcanvas.com/project/779656/overview/cleo

I tried Albertos method and nothing happened

Thanks, I’ll take a look tomorrow morning, unless another forum member steps in, in the meantime.

in Thanos’s immortal words: fine, I will do it myself.
lol

:grinning:

1 Like

User manual: ‘Trigger volumes are static collision shapes which can fire events whenever a rigidbody enters or leaves their volume’.

https://developer.playcanvas.com/en/user-manual/physics/trigger-volumes/

So as long as your car has no rigid body it’s not working. If it’s also not working with a rigid body it means there is a second problem.

2 Likes

Okay I finally got it to work, the collision component seem to only work against a dynamic rigid body, from keeping my car from following through the floor, I changed the mass to 0 and it worked.

Conclusion: collision works only with rigid body that is set to dynamic, this is limiting

So you tried to set the rigid body type to kinematic and that’s not working?

How do you move the car?

let me try that
I move my car through setLocalPosition

okay so I finally fixed all the problems or I hope so.

I added a new entity under the car which has a collision box and a rigidbody set to kinematic, adding a rigidbody directly into the car sprite is making my controller code not work but adding the collision to the car as an entity did the work. so basically what you are colliding against needs to have a rigidbody only then the collision component will feel like its being collided against something, the rigidbody on which the collision is happening needs collision component too, but the one with the trigger code only needs collision, no rigidbody.

so in summary:

car - rigidbody(kinematic), collision, model(box or whatever)[disabled] idk why but I am too afraid to remove it lol.
coin - collision.

I believe that adding rigidbody to the coin that moves based on some code will disable all collision triggers. so coin shouldn’t have a rigidbody if it is going to use the triggerenter function.

but the wall/car its colliding against should have a collision and a rigidbody.

I don’t understand why you use setLocalPosition to move you car. If you use translateLocal to move your car, your car need a kinematic rigid body. And indeed, don’t add a rigid body to the trigger because then it’s not a trigger anymore. Only the entities that have to activate the trigger need a rigid body.

1 Like

the car has 3 lanes, I only move lanes

Ah okay. If you are happy with the result I will mark the topic as solved. :slightly_smiling_face:

1 Like

If you add a rigidbody, the entity is no longer a trigger.