[SOLVED] Translate forward on trigger

How would I make it so if the player touches another entity, it triggers a script in another entity that translates the entity forward.

Hi @GraphicBuildGames,

Start with the trigger functionality, check this tutorial on how to implement it:

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

I already have a trigger script

var TriggerVolume = pc.createScript('triggerVolume');

// initialize code called once per entity
TriggerVolume.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', function (entity) {
        console.log(entity.name + ' has entered trigger volume.');
    });
    this.entity.collision.on('triggerleave', function (entity) {
        console.log(entity.name + ' has left trigger volume.');
    });
};

TriggerVolume.attributes.add('Enemy Entity',{
     type: 'entity'
});

will this work for it?

Hi @GraphicBuildGames! The entity you want to move has no rigidbody? What about the duration of the movement?

I dont know how to do it

I now just want to be able to activate a script using another script. Is it possible to do that without triggers?

If you already know which entity it is, you can do it without a trigger. You can enable a specific script or the script component with all scripts.

// enable script component 
entity.script.enabled = true;

what do I do with the entity part

this

'name of entity', script.enabled = true;

If I understand you correctly, you can use this method:

var entity = this.app.root.findByName('Name');
entity.script.enabled = true;

That’s basically the same as this:

this.app.root.findByName('Name').script.enabled = true;
2 Likes

where it says script would I put in the name of the script?

If the script component is already enabled and you want enable a specific script only, it wil look like this:

var entity = this.app.root.findByName('Name');
entity.script.scriptName.enabled = true;

well in the function code of the script I am trying to activate, there is a translate local function that is in the update code. So when I start it it will go immediately. How do I fix that?

What do you mean with this?

Well when the thing to move it is in the function code meaning it does it forever and starts immediately.

Yes, as long as the script is enabled. What do you want instead?

so do I have to disable the script in the beginning?

Of course, otherwise enabling by script is useless.

entity.script.scriptName.enabled = false;

is that right?

var Start = pc.createScript('start');

var entity;
// initialize code called once per entity
Start.prototype.initialize = function() {
};

// update code called every frame
Start.prototype.update = function(dt) {
    if(this.app.keyboard.isPressed(pc.KEY_W)){
        var entity = this.app.root.findByName('Box');
entity.script.drive.enabled = true;
    }
};

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

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

script name is drive

If the script has the name scriptName, but you can disable the script also in the editor.

1 Like