[SOLVED] Collision event for specific entity only

I have been working on this script and have done many experiments for it to work, but I can’t get it to do what I want it to do so I decided to ask for help.

So this script is a trigger script and what it does is it teleport’s the player to a new scene whenever it collides with a rigidbody.

The trigger and teleport function work perfectly fine, so the only issue I have is the fact that it teleport’s the player whenever it touches any rigidbody.

So what’s wrong with it? you may ask.

Well that is just it. I don’t want it to teleport the player whenever it touches any rigidbody. I only want it to teleport the player when it touches the player rigidbody.

So I tried experimenting with many different methods such as find by tag, find by name, I tried again and again, but it still teleport’s the player regardless of what rigidbody touches it.

So I was wondering if you could help me fix this problem please.

And I thank you ahead of time.

Also here is my code.

var DeathTele = pc.createScript('deathTele');

// scene name is the scene ID
DeathTele.attributes.add("sceneName", {type: "string", default: "", title: "Scene Name to Load"});

//player to teleport
DeathTele.attributes.add("player", {type: "string", default: "", title: "entity to trigger"});



// initialize code called once per entity
DeathTele.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.app.root.findByTag('player', this.player, this);

    
    







};









DeathTele.prototype.onTriggerEnter = function(player) {
    player.rigidbody.linearVelocity = pc.Vec3.ZERO;
    player.rigidbody.angularVelocity = pc.Vec3.ZERO;
    // change the scene
    this.loadScene(this.sceneName); 
};


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

DeathTele.prototype.loadScene = function (sceneName) {
    // Get a reference to the scene's root object
    var oldHierarchy = this.app.root.findByName ('Root');
    
    // Get the path to the scene
    var scene = this.app.scenes.find(sceneName);
    
    // Load the scenes entity hierarchy
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (!err) {
            oldHierarchy.destroy();
        } else {
            console.error(err);
        }
    });
};

// swap method called for script hot-reloading
// inherit your script state here
// StartGame.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/type or paste code here

Hi @DinoCalebProductions,

The most common way to do this is by checking a property of the other entity that is passed as an argument to the on triggerenter callback.

For example:

this.entity.collision.on('triggerenter', function(other){

   // using the entity name
   if(other.name === 'Player'){
      // it's the player, do the things you normally do
   }

   // using the entity tag
   if(other.tags.has('Player') === true){
      // it's the player, do the things you normally do
   }

}, this);
2 Likes

Thank you @Leonidas I have not tried this method so I will impement it into my script.

Thankyou very much.

1 Like