Play sound with distance

In my enemy entity I want to make it so if the distance is less than 15, it will play a sound. But if the distance is more than 15 the sound will stop. Anyone hot an idea

Enemy Script:

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.detectObstacle();
    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.1) {
            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) {
            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);
};

Hi @Mason_Rocchio,

I didn’t have a chance to look through your code, but check positional (3D) sound. That may work in your case, the sound will fade out automatically, no code required, if the distance is larger than a limit.

https://developer.playcanvas.com/en/tutorials/basic-audio/

1 Like

I know about audio but I am not good with distance. Also it is not my code, albertos helped out with enemy AI.