Chase Music when the enemy sees you?

Hello, I was just wondering if it’s possible to make a sound play (Like chase music) when an enemy AI sees you and Chases you. Like what popular horror games like Hello Neighbor, SCP Containmentbreach, Amnesia, and ETC have.

My monster uses raycasting to see so It should be possible to play chase music when he sees you right?

It would also be cool if not only does the music play when the monster sees you, but for it to also stop after a few seconds when he loses you.

I also don’t want the song to repeat over and over every second that he sees you because then they will overlap each other and it will sound like a mess.

And one last thing, Instead of having the enemy play the sound, instead another entity will play the sound.

How would I be able to do this if it’s possible?

And thank you.

Here is the Link to my editor: PlayCanvas | HTML5 Game Engine

Hi @DinoCalebProductions! can I see your enemy script please?

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

// update code called every frame
Enemy.prototype.update = function(dt) {
    this.searchTarget();
    this.detectObstacle();
    this.moveEnemy(dt);
};

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.05) {
            this.entity.lookAt(destination);
            this.entity.translateLocal(0,0, -this.enemySpeed*dt);
        }
        else {
            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 < 1) {
                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 < 40) {
        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);
};

What you want to do is place the sound this under the points where the distance is

to start music

 // check if target is in range, so close enough to see
    if (distanceToTarget < 40) {
    this.entity.sound.play("Your sound name")
        target.sub(this.entity.getPosition()).normalize();
        var dot = target.dot(this.entity.forward);
else {
            this.randomDestination();
            this.entity.sound.stop("Your sound here")

This should help you.

Thankyou very much and wow you code very fast.

Your welcome

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 (this.app.keyboard.isPressed(pc.KEY_SPACE)){
        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);
};

this is the full script for you

If you do it this way, the sound is also playing when the enemy can’t see the target.

I would do this:

if (this.enemyCanSeeTarget) {
    // start sound if not started yet
}
else {
    // stop sound
}

Oh thank you very much.

I didn’t expect to get help so fast so thank you a lot.

Your welcome!