This script has an issue where the check for slowAngle on line 57 is randomly failing (sometimes it works, sometimes it doesn’t). The only reason I can think of for this is that the number slowAngle is set to is never matched by the z rotation. Is there a way I can improve the check so that no matter what number slowAngle comes out to be, I can be guaranteed the z rotation value will be checked at the same number?
var SpinWheel = pc.createScript('spinWheel');
SpinWheel.attributes.add('manager', {type: 'entity'});
// initialize code called once per entity
SpinWheel.prototype.initialize = function()
{
this.paused = true;
this.slowDown = false;
this.colliders = this.app.root.findByTag('Collider');
//Vars to control slow/stop
this.rotCount = 0;
this.slowCount = 0;
//Set start position
startAngles = [-15, -45, -75, -105, -135, -165, 15, 45, 75, 105, 135, 165];
//startAngle = 15;
this.startAngle = startAngles[Math.floor(Math.random()*startAngles.length)];
this.entity.setEulerAngles(90,0,this.startAngle);
console.log("Start Angle: " + this.startAngle);
};
SpinWheel.prototype.spin = function()
{
this.paused = false;
this.slowAngle = this.manager.script.spinManager.stopAngle;
//this.slowAngle = 15;
this.entity.rigidbody.angularVelocity = new pc.Vec3(0,0,100);
};
SpinWheel.prototype.update = function(dt)
{
if(this.paused)
{
return;
}
this.rotation = this.entity.getEulerAngles();
if(this.entity.rigidbody.angularVelocity.z === 0)
{
for(i=0;i<this.colliders.length;i++)
{
this.colliders[i].rigidbody.enabled = false;
}
}
else
{
for(i=0;i<this.colliders.length;i++)
{
this.colliders[i].rigidbody.enabled = true;
}
}
if(this.rotCount === 3 && Math.round(this.rotation.z) === this.slowAngle)
{
this.slowDown = true; //this needs to always happen, but isn't
}
else if(this.rotCount != 3)
{
if(Math.round(this.rotation.z) === this.startAngle)
{
this.rotCount += 1;
}
console.log("Rotations: " + this.rotCount);
}
if(this.slowDown)
{
var v = this.entity.rigidbody.angularVelocity;
v.z *= 0.995;
this.entity.rigidbody.angularVelocity = v;
}
};