Hi,
I was just working on a project using a rigidbody, collisions and now audiosources.
I never worked with audio in playcanvas before, so I just followed the tutorial until something went weird:
I am bouncing a ball off a surface using velocity, then I check for a collision with a boundaries box to reset the ball to its original position and to reset its physical values.
When the ball hits the surface I want to play a sound, now all that is working fine, but I found a weird quirk in the sound-components ‘positional’ attribute:
When positional is checked, the ball disappears into nowhere rather then resetting its own position, it end up behind the collider that is supposed to reset it, and before it finally “falls out of the scene” it triggers the collision event about 20 times.
This is the script attached to the ball:
pc.script.attribute('linear','vec3',[0,0,0]);
// linear velocity for the ball
pc.script.attribute('angular','vec3',[0,0,0]);
// angular velocity for the ball
pc.script.create('startVelocity', function (app) {
// Creates a new StartVelocity instance
var StartVelocity = function (entity) {
this.entity = entity;
};
StartVelocity.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function ()
{
// store the original position of the ball to restore it later
this.orgPos = new pc.Vec3();
this.orgPos = this.entity.getPosition();
// set up collision trigger
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
// set velocity to the default and sync
this.resetRigidbody();
},
resetRigidbody: function()
{
this.entity.rigidbody.linearVelocity = this.linear;
this.entity.rigidbody.angularVelocity = this.angular;
this.entity.rigidbody.syncEntityToBody();
},
onCollisionStart: function(result)
{
// if we hit one of the boundary objects (all named Respawn)
if(result.other.name == "Respawn")
{
// restore the original position,
this.entity.setPosition(this.orgPos.x,this.orgPos.y,this.orgPos.z);
// then restore the original velocity and resync the rigidbody
this.resetRigidbody();
console.log("respawn "+this.entity.getPosition());
}
else
{
// if we hit anything else play a sound
this.entity.sound.play('hit');
}
}
};
return StartVelocity;
Now my guess is that the positional attribute of the sound component is somehow messing up the rigidbody- or the entity-position, thus resulting in the ball respawning at the same position it collided and then slowly falling out of the collider while still respawning
Is this a bug, or am I missing a function to sync the audio source with the entity or something like this?
Thanks in advance for any help