[SOLVED] Bug with collisions

Hello, I’m pretty new to PlayCanvas and need an answer to a quick question.

There is a problem in my game that allows enemies to pick up coins on the ground that only the player is supposed to pick up. This is because my code allows anything with collision to pick up the coin.

So my question is how do I filter what can interact with certain things? Like how to fix my problem with enemies colliding with coins, or not letting the player get hit by their own bullets but them still hitting enemies.

@Crusty_Potato Is there a project you can share? Are you talking multiplayer or single FPS? You could put a tag on player that can be used to fire the addition of coins only to the entity that has a tag of player.

Yeah I can share it to you @Tirk182. This is a single-player top-down view shooter
Don’t know how to properly share it, does this link work? Or do I have to give you access to see the game first?
https://playcanvas.com/editor/code/1033821?tabs=119938945

https://playcanvas.com/project/1033821/overview/blank-project

@Tirk182 this is the project sorry. gave you access to view it too

@Crusty_Potato You can take me off of the read only. As long as you send a link and your project is set to public the forum can get to it.

I don’t see much in the way of code. Here is a trigger script you can have a look at from one of my games. It’s to pickup Health

// Enter trigger
ItemPickup.prototype.onTriggerEnter = function(object) {

    if(object.tags.has('player')) {

        // play the pickup sound
        if(this.entity.sound) this.entity.sound.play("Pickup");
              
        // increase the munitions amount
        console.log("pickup");

        // Increase item amount
        object.fire("player:addhealth",this.amount);

        // Disable after Pickup
        this.entity.enabled = false;
           
    }

};

You can see in the above that the item pickup will not register unless the player has a tag player… It’s up to you if you want to clone and destroy after pickup or in my case I just disabled the item and then start a timer to re-enable it later in the game.

image

Thanks for the fast responses @Tirk182, this helps a lot.

@Crusty_Potato Just another example I use

var ItemPickup = pc.createScript('itemPickup');

ItemPickup.attributes.add("amount", { type: 'number', default: 5 });
ItemPickup.attributes.add("itemType", { type: 'string' });

// initialize code called once per entity
ItemPickup.prototype.initialize = function() {

    // Trigger on enter collision area
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    
};

// update code called every frame
ItemPickup.prototype.update = function(dt) {
    
};

ItemPickup.prototype.onTriggerEnter = function(entity) {

    if(entity.tags.has('player')) {

        // play the pickup sound
        if(this.entity.sound) this.entity.sound.play("Pickup");
              
        // Increase item amount
        entity.fire("player:additem",this.amount);

        // Disable after Pickup
        this.entity.enabled = false;
           
    }

};

@Tirk182, sorry to bother you again, ran into an error message that says “object is not defined” now whenever anything collides with the coin.
Do you know if there is anything I need to add to the code or if I just have anything moved around in the wrong way?

@Crusty_Potato

Coin.prototype.onCollision = function(object) { //<<<<<<<<< change
    if(object.tags.has('player')) {
    this.entity.destroy();
    }
};

Have a look to the second example above. Are you trying to do a third person game or first person?

3rd person, planing on making it a top-down view

@Tirk182 And thanks again, your solution solved the problem.

1 Like