[SOLVED] Trigger Link / Collision

Hello Guys.

I am currently work on a FPS virtual Gallery, and want to be able to trigger/open an external link when a key is pressed in a certain area of the game. Is this possible?
My coding skills are limited, so wonder if anybody could point me in a direction how to solve this?

Kind regards

Hey, you can simply check the key press and on that you can write the logic for the new link.

myScriptName.prototype.update = function (dt) {
    var app = this.app;

    if (app.keyboard.isPressed(pc.KEY_A)) {
        window.open("https://www.w3schools.com"); 
   // window.top.location.href = "https://www.google.com"; // This link will useful if you want to open the link on the same window tab
    }
};

You can read more about it here : Window open() Method

2 Likes

Thanks. But what Script would i add this to?
https://playcanvas.com/project/850394/overview/sota

Hi @Anders_Juul_Jorgense and welcome! You can create an entity and add a collision component to it. Then you can give this component the same size as your room. The position of this entity should be somewhere in the middle of the room. This entity will be your trigger entity. You need to create also a script for this entity. The pages below will help you with this script. In this script you can add the code of @saif.

https://developer.playcanvas.com/en/user-manual/physics/trigger-volumes/

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

1 Like

Hi Albertos. Thanks for Reply.

I have used this triggertext before, How would you add it to this script?

var Triggertext = pc.createScript('triggertext');
Triggertext.attributes.add('artistname',{
    type:'string'
});

// initialize code called once per entity
Triggertext.prototype.initialize = function() {
    var artistname = this.artistname;
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
     this.entity.collision.on('collisionend', this.onCollisionEnd, this);
};


Triggertext.prototype.onCollisionStart = function (result){
    if (result.other.rigidbody){
        this.app.root.findByName('artworkUI').element.text = this.artistname;
    }
};

Triggertext.prototype.onCollisionEnd = function (result){
   
         this.app.root.findByName('artworkUI').element.text = '';
    
};


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

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

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

I got it working :slight_smile:
Thanks for the pointers guys!

1 Like