[SOLVED] Impact Problems

I’m trying to get my bullets to delete when they hit a tree, but the tutorials don’t give me a good answer. What is the current way to create a collision event?

https://playcanvas.com/editor/code/539704?tabs=11418589&line=27&col=19&

Look at ‘contact events’ at the bottom of this page: https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

I did work on that, but I have a problem with this code:

if (heet.tree) {
    var burf = this.entity.findByName("Bullets");
    burf.destroy();
}

There are several parts to address here:

  1. bullet.name = "Bullets"; You are giving every bullet entity the same name which makes it impossible to know which bullet has collided with the tree with the name alone.
  2. if (heet.tree) { will always be null as it never gets assigned a value.
  3. this.entity.findByName("Tree"); won’t ever find the tree as it will only search the children of this.entity. You probably meant this.app.root.findByName("Tree") which searches the children of this.app.root which is the root node of the application.

What I recommend you do is to create a new entity in the editor for the bullet, create separate script for it that handles the collision callbacks, movement etc and clone it every time the player fires.

There’s an example of cloning in this project sample: https://developer.playcanvas.com/en/tutorials/place-an-entity-with-physics/

Or you can set an anonymous function as the contact callback so you can keep a reference to the bullet entity like so:

        bullet.collision.on('contact', function(result) {
            this.destroy();
        }, bullet);

PS You may encounter a bug where the setTimeout function that you set tries to destroy an entity that has already been destroyed from a collision.

PPS You may encounter a bug where the bullets go through the tree due to low frame rates. Refer to this tutorial on enabling the CCD. https://developer.playcanvas.com/en/tutorials/physics-with-ccd/

Tried all that, but some stuff made it crash, some stuff just didn’t do anything.

https://playcanvas.com/project/546445/overview/fire-test

I left some comments, so now it should be crystal clear.

Problem: the ENTIRE script refuses to load. What did I do?

Show me the errors log please.

Cant you run my project?

Your thing works, but mine says
Error loading scripts. Open the browser console for details.
Movement works fine, but the bullets don’t appear or move.

Compare it exactly with mine.

I also removed bullet entity from scene hierarchy.

Okay, I see you haven’t changed script name.

image

Script loaded, but still doesn’t do anything.

You still have “fire” script on your gun.

image

You have to reparse your script file and then attach new script.
Select script file and in right panel should be button for this purpose.

And please, be little more attentively - this problem you could solve easily just by comparing with my project.

I did that, still nothing.

You gonna be kidding me.
Your gun still has “fire” script. Not “shootstuff” one.

image

Remove old script and add new as I told you before.

That fire script got deleted! I only see shootstuff!

And now it works pretty great, how I can see.