[SOLVED] Use collision events with entity clone

I am creating an FPS game and I have added an event listener so that when you click, an entity (placed outside of the current visible map) will be cloned, placed directly in front of you, and translated locally during the prototype.update function. I am currently having an issue with trying to get it to understand when it comes into contact with a surface (a wall, floor, etc.) but collision events only seem to work when using this.entity, which I can’t do because the script is attached to the original entity that gets cloned. Is there another way to do this?

Hi @cyron and welcome! Can you share a link of your project please? I hope that makes it a little easier to understand what you’re trying to achieve. :upside_down_face:

Yeah, absolutely!
https://playcanvas.com/editor/scene/1377599
It’s a bit messy, I’m still kind of early into it.

I guess you want to detect collision on your bullet? Then you can just add the event to your bullet script and use this.entity. I suggest to disable the bullet in the editor, because you enable the clone by script.

Yeah, I did do that, but I wasn’t able to figure out how to get the script to recognize the clone as this.entity. It keeps checking the original (I think) and since the original never touches anything, I guess the event never gets triggered?
EDIT: So I added a ‘triggerenter’ function to the script, but it doesn’t seem to have any effect. I have also disabled the original entity, and the script still seems to be able to clone it, which is good, but I guess it still recognizes it as this.entity.

No, that’s not the case. If you clone an entity with a script, the clone has also that script, so for the cloned entity this.entity will be the clone.

I don’t see it in your bullet script. Be ware that a trigger is an entity without a rigidbody. If you use a rigidbody you need to use collisionstart. Please check the page below for some examples.

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

Oh, you’re right. I misread the ‘triggerenter’ and thought it said it only works if you have a rigidbody. I’ll try a collision.on and see what happens.

1 Like

Ok, so I’ve added the function, and the listener. For some reason it just goes straight through whatever it touches. I am completely lost at this point. When it collides with something, it should translate 100 units on the X, Y, and Z axis, completely out of the player’s view (this is just a temporary thing, I’m probably going to change it once everything gets sorted out).

I see you move your bullet with the translate method. This doesn’t move a dynamic rigidbody. I suggest to set the rigidbody type to kinematic.

You also could use this.entity.enabled = false; or this.entity.destroy();.

1 Like

Copy that. Changing to kinematic.

OK, so it still is doing the same thing.
It’s moving, but it just travels through stuff. I was wondering: I have a collision component for the entity, but it is disabled. Would that have anything to do with it? (Also, when I do enable it, the clone sits in front of me for a few seconds before moving, and it just gets really weird.)

I see an enabled collision component right now and that is good. It seems not to be possible to use this.entity.collision.on('collisionstart', this.onCollisionStart, this); in a custom function. Please add it to the initialize function.

IT WORKS!! Thanks so much!! :grinning:

1 Like