Collision destroying itself instead of destroying another Collision

I have a code that is supposed to destroy an “enemy” on collision, but instead is destroying itself. Can you help?

var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemyNumber = 0;
};
Collision.prototype.onCollisionStart = function(result) {
    if (result.other.name = 'Enemy') {
        result.other.destroy();
        this.enemyNumber += 1;
    }
};
Collision.prototype.update = function() {
    this.textBox.element.text = "Enemys Killed : " + this.enemyNumber;
    if (this.entity.enabled = false){
        this.enemyNumber -= 1;
    }
};

Thank you in advanced :grin:

And the game PlayCanvas | HTML5 Game Engine.

For starters

if (result.other.name = 'Enemy') {
use == not = for evaluations. = is for setting, == is for evaluating equality

Its hard to know exactly whats going on without the hierarchy, are there other components on this entity?

Just 2 enemies (enemy 1, enemy 2) that need to be destroyed, and the bullet entity.

When you shoot (key E) the gun (key 1-4) against a wall, the bullet is supposed to show, then destroy after ‘x’ seconds, but the bullet is not showing at all, unless I turn off the ‘Collision’ script.

I think the bullet is hitting shooter entity or gun entity right away, and destroying it (you or your gun) before it can even hit the enemy.

The main problem is your if statements:

Collision.prototype.onCollisionStart = function(result) {
    if (result.other.name = 'Enemy') {
        result.other.destroy();
        this.enemyNumber += 1;
    }
};

The equals sign on line 2 should be 2 equals signs ==. Otherwise it will evaluate true and destroy anything regardlesss of its name (including the shooter and gun if they have colliders)

1 Like

I have it like this now

var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemyNumber = 0;
};
Collision.prototype.onCollisionStart = function() {
    if (onCollisionStart == this.app.root.findByName('Enemy1')) {
        destroy(this.app.root.findByName('Enemy1'));
        this.enemyNumber += 1;
    }
    if (onCollisionStart == this.app.root.findByName('Enemy2')) {
        destroy(this.app.root.findByName('Enemy2'));
        this.enemyNumber += 1;
    }
};
Collision.prototype.update = function() {
    this.enemyNumber = this.enemyNumber;
    this.textBox.element.text = "Enemys Killed : " + this.enemyNumber;
};

and the bullet shows now, but it still doesn’t destroy the enemy.

Yeah im not sure off the top of my head why it wouldnt work, since its javascript you can do logs, i would add these logs to see what happens:

var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemiesKilled = 0;
};
Collision.prototype.onCollisionStart = function(result) {
   console.log(`COLLISION OCCURED: ${ result.other.name}`)
    if (result.other.name == 'Enemy1' || result.other.name == 'Enemy2') {
        destroy(result.other);
        this.enemiesKilled += 1;
    }
};
Collision.prototype.update = function() {
    this.textBox.element.text = "Enemys Killed : " + this.enemiesKilled;
};
1 Like

I would also reccomend using “tags” instead of name. like this:

var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemiesKilled = 0;
};
Collision.prototype.onCollisionStart = function(result) {
   console.log(`COLLISION OCCURRED: ${ result.other.name}`)
    if (result.other.tags.has('enemy')) {
        destroy(result.other);
        this.enemiesKilled += 1;
    }
};
Collision.prototype.update = function() {
    this.textBox.element.text = "Enemys Killed : " + this.enemiesKilled;
};

You can add tags to an entity in the heirarchy

It is still not working, but maybe it disables the entity instead?

Hi @Kyle_3_1415,

I’ve noticed that a lot of the code you post has some odd characteristics to it that I can’t explain as examples from the API reference or from the answers in your threads. Are you using a generator like ChatGPT by chance?

Specifically, I’m having trouble determining where destroy(result.other); comes from along with your original comparisons like if (onCollisionStart == this.app.root.findByName('Enemy1')).

In general you would approach this with something like what @MitchMeyer1 suggested, though instead of using the undefined destroy() call destroy from the actual ‘other’ entity like this:

Collision.prototype.onCollisionStart = function(result) {
   console.log(`COLLISION OCCURRED: ${ result.other.name}`)
    if (result.other.tags.has('enemy')) {
//        destroy(result.other);
        result.other.destroy();
        this.enemiesKilled += 1;
    }
};

I would recommend taking a look here for collision logic examples:

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

and here for the functions that already attached to all entities that you can call:

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

If you are using a language model to generate your code, I would definitely recommend that you follow some of the tutorials available in the docs section of playcanvas.com, especially the basics tutorials here:

https://developer.playcanvas.com/en/tutorials/?tags=basics

This will allow you to better familiarize yourself with how things are structured both inside and outside of code.

I hope this is helpful.

1 Like

It still isn’t working, but those docs. are helpful. And I am generating the code in my brain.

I think I have it figured out:

  1. Make one script that when hits certain entity with tag, it subtracts a point of health
  2. Another script that when the health reaches 0< it destroys the entity
  3. Cross your fingers it works

I discovered the problem:

var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.attributes.add('textBox2', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemiesKilled = 0;
    this.bulletsMissed = 0;
};
Collision.prototype.onCollisionStart = function(result) {
    this.bulletsMissed += 1;
    if (result.other.tags.has('enemy')) {
        console.log(`COLLISION HIT`);
    } else {
        console.log(`COLLISION NONE`);
    }
};
Collision.prototype.update = function() {
    this.textBox.element.text = "Enemys Killed : " + this.enemiesKilled;
    this.textBox2.element.text = "Bullets Used : " + this.bulletsMissed;
    if (console.log == ('COLLISION TERMINATED')) {
        this.enemiesKilled += 1;
    }
};
var HitCollision = pc.createScript('hitCollision');
HitCollision.prototype.update = function() {
    if (console.log == (`COLLISION HIT`)) {
        this.entity.destroy();
        console.log('COLLISION TERMINATED')
    }
};

It is not registering when a collision happens.

Even with the old script, it will not register the collision.

var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.attributes.add('textBox2', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemiesKilled = 0;
    this.bulletsMissed = 0;
};
Collision.prototype.onCollisionStart = function(result) {
    this.bulletsMissed += 1;
    if (result.other.tags.has('enemy')) {
        console.log(`COLLISION HIT`);
        this.result.other.destroy();
        this.enemiesKilled += 1;
    } else {
        console.log(`COLLISION NONE`);
    }
};
Collision.prototype.update = function() {
    this.textBox.element.text = "Enemys Killed : " + this.enemiesKilled;
    this.textBox2.element.text = "Bullets Used : " + this.bulletsMissed;
};

the bulletsMissed should go Up by 1 when colliding, but nothing happens.

Hi @Kyle_3_1415! Make sure both entities have the correct rigidbody and collision component.

1 Like

Hate to sound feminine, but OH MY GOD. You saved me ripping my scalp out trying to figure it out. I made it kinematic (so the bullets don’t move once shot) and it works perfectly. Thanks!!!

//Le perfecto code:
var Collision = pc.createScript('collision');
Collision.attributes.add('textBox', {type: 'entity'});
Collision.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.enemiesKilled = 0;
};
Collision.prototype.onCollisionStart = function(result) {
   console.log(`COLLISION OCCURED: ${ result.other.name}`)
    if (result.other.name == 'Enemy1' || result.other.name == 'Enemy2') {
        result.other.destroy();
        this.enemiesKilled += 1;
    }
};
Collision.prototype.update = function() {
    this.textBox.element.text = "Enemies Killed : " + this.enemiesKilled;
};
1 Like