How to activate enemy with trigger

How would I make it so when the player crosses over a certian area it makes an entity come out of that area

Could you please elaborate via an example?

Player -------------------------------> player walks over trigger ------------------------> SCRIPT
Trigger is here Player triggers script

When the player walks over the trigger it will activate another script

Hi @Mason_Rocchio,

Check this tutorial on triggers:

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

I have already tried that

What does your current setup look like?

download (13)
when the player walkes over the trigger it turnes on the enemy.js script

Create a trigger entity first. This is an entity with a collision component. Add a script like the script on the tutorial page below. You can add a attribute to this script for your enemy entity. On trigger enter you can enable the enemy script.

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

When the player walks over a trigger I want to activate another script how would I do that?

What I have so far:
triggerVolume script with enemy attribute for the enemy I want to activate. listed below

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'
}); 

enemy script: listed below

var Enemy = pc.createScript('enemy');

Enemy.attributes.add('enemyCamera', {type: 'entity', title: 'Camera Entity'});
Enemy.attributes.add('enemyTarget', {type: 'entity', title: 'Target Entity'});
Enemy.attributes.add('enemyGround', {type: 'entity', title: 'Ground Entity'});
Enemy.attributes.add('enemySpeed', {type: 'number', default: 0.1, title: 'Enemy Speed'});

// initialize code called once per entity
Enemy.prototype.initialize = function() {
    this.enemyCanSeeTarget = false;
    this.enemyDestination = new pc.Entity();
    this.activated = false;
};

// update code called every frame
Enemy.prototype.update = function(dt) {
    if (this.activated) {
        this.searchTarget();
        this.detectObstacle();
        this.moveEnemy(dt);
    }
    else if (console.log(entity.name + ' has left trigger volume.')){
        this.activated = true;
    }
};

Enemy.prototype.moveEnemy = function (dt) {
    var destination = new pc.Vec3(this.enemyDestination.getPosition().x, 1, this.enemyDestination.getPosition().z);
    var distanceToDestination = this.entity.getPosition().distance(destination);

    // move the enemy based on the current state
    if (!this.enemyCanSeeTarget) {
        if (distanceToDestination > 0.1) {
            this.entity.sound.play("Chase");
            this.entity.lookAt(destination);
            this.entity.translateLocal(0,0, -this.enemySpeed*dt);
        }
        else {
            this.entity.sound.stop("Chase");
            this.randomDestination();
        }
    }
    else {
        this.entity.lookAt(this.enemyDestination.getPosition());
        
        if (distanceToDestination > 2) {
            this.entity.translateLocal(0,0, -this.enemySpeed*dt*2);
        }
    }
};

Enemy.prototype.detectObstacle = function () {
    this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.enemyDestination.getPosition(), function (result) {
        
        // check if there is an obstacle in the way
        // prevent the enemy is moving inside an obstacle
        if (result.entity.name == 'Obstacle') {
            var distanceToObstacle = this.entity.getPosition().distance(result.point);
            
            if (distanceToObstacle < 2) {
                this.randomDestination();
            }
        }
    }.bind(this));
};

Enemy.prototype.searchTarget = function () {
    var target = this.enemyTarget.getPosition();
    var distanceToTarget = this.entity.getPosition().distance(target);
    
    // check if target is in range, so close enough to see
    if (distanceToTarget < 10) {
        target.sub(this.entity.getPosition()).normalize();
        var dot = target.dot(this.entity.forward);
        
        // check if target is front, so spottable or already spotted
        if (dot > 0.5 || this.enemyCanSeeTarget) {
            this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.enemyTarget.getPosition(), function (result) {
                
                // check if target is in view, so no obstacles in the way
                if (result.entity.name == 'Player') {
                    this.enemyCanSeeTarget = true;
                    this.enemyDestination.setPosition(this.enemyTarget.getPosition().x, 1, this.enemyTarget.getPosition().z);
                }
                else {
                    this.enemyCanSeeTarget = false;
                }
            }.bind(this));
        }
        else {
            this.enemyCanSeeTarget = false;
        }
    }
    else {
        this.enemyCanSeeTarget = false;
    }
};

Enemy.prototype.randomDestination = function () {
    var xPosition = pc.math.random(-this.enemyGround.getLocalScale().x/2, this.enemyGround.getLocalScale().x/2);
    var zPosition = pc.math.random(-this.enemyGround.getLocalScale().z/2, this.enemyGround.getLocalScale().z/2);
    this.enemyDestination.setPosition(xPosition, 1, zPosition);
};

Below how you can activate and deactive the enemy using your trigger.

// initialize code called once per entity
TriggerVolume.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', function (entity) {
        this.enemyEntity.script.enemy.activated = true;
    }.bind(this));
    this.entity.collision.on('triggerleave', function (entity) {
        this.enemyEntity.script.enemy.activated = false;
    }.bind(this));
};

You need to rename your attribute to enemyEntity (instead of Enemy Entity). You also have to remove the else if statement from the code below.