Hello, I need help to my script. The script works but the sound is glitching when the ‘Player’ is moving
Here is the project:
https://playcanvas.com/editor/scene/1757634
and here is the script:
var StoneFootstep = pc.createScript(‘stoneFootstep’);
// initialize code called once per entity
StoneFootstep.prototype.initialize = function() {
this.playerEntity = this.entity.root.findByName(‘Player’);
this.collisionEntity = this.entity.root.findByName(‘Collision Stone’);
this.playerEntity.hasEntered = false;
this.entity.collision.on('triggerenter', this.onCollisionEnter, this);
this.entity.collision.on('triggerleave', this.onCollisionLeave, this);
};
// update code called every frame
StoneFootstep.prototype.update = function(dt) {
if (this.playerEntity.hasEntered && this.playerEntity.rigidbody && this.playerEntity.rigidbody.linearVelocity.length() > 0) {
// Player is moving, play the sound
var speedMultiplier = this.playerEntity.rigidbody.linearVelocity.length() / 2.0; // Adjust the multiplier as needed
this.collisionEntity.sound.pitch = speedMultiplier;
this.collisionEntity.sound.play(‘Footstep’);
} else {
// Player is not moving, stop the sound
this.collisionEntity.sound.stop(‘Footstep’);
}
};
StoneFootstep.prototype.onCollisionEnter = function() {
this.playerEntity.hasEntered = true;
};
StoneFootstep.prototype.onCollisionLeave = function() {
this.playerEntity.hasEntered = false;
};
Basically i was trying to make a dynamic footsteps in onCollision function and yeah i dont know why my sound is acting up like this.