Need help figuring out the cause of my arrow shifting it’s rotation when I face backward.
var Shoot = pc.createScript('shoot');
Shoot.attributes.add('player', { type: 'entity' });
Shoot.attributes.add('power', { type: 'number' });
Shoot.attributes.add('arrowOnHand', { type: 'entity' });
Shoot.attributes.add('threshold',{type: 'number' });
Shoot.attributes.add('ammo',{type: 'number' });
Shoot.attributes.add('powerBar', { type: 'entity' });
// initialize code called once per entity
Shoot.prototype.initialize = function() {
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.mouseDown, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.mouseUp, this);
this.setPower=false;
this.ammoText = this.app.root.findByName('ammoCount');
};
// update code called every frame
Shoot.prototype.update = function(dt) {
this.onSetPower(dt);
if(this.app.keyboard.wasPressed(pc.KEY_R)) {
console.log("Reload Hit");
// Reload Magazine
if(this.ammo <= 9){
this.entity.sound.play('arrowReload');
this.ammo = 10;
}
else if(this.ammo == 10){
}
}
this.ammoText.element.text = this.ammo;
};
Shoot.prototype.setPowerBarValue = function(val) {
this.powerBar.element.width = val * 260;
};
Shoot.prototype.onSetPower = function(dt) {
if(this.setPower){
this.power+=this.threshold*dt;
if(this.power>this.threshold){
this.power=this.threshold;
}
let powerVal = this.power / this.threshold;
this.setPowerBarValue(powerVal);
}
};
Shoot.prototype.mouseDown = function(e) {
if(this.entity.enabled) {
this.setPower=true;
if(this.ammo != 0){
this.entity.sound.play('arrowLoad');
}else if(this.ammo == 0){
this.setPower=false;
}
}
};
Shoot.prototype.mouseUp = function(e) {
if(this.entity.enabled) {
this.setPower=false;
if(this.ammo != 0){
this.shootArrow();
}
}else{
}
};
Shoot.prototype.shootArrow = function(dt){
this.entity.sound.stop('arrowLoad');
this.entity.sound.play('arrowRelease');
// Clone arrow
var arrow = this.arrowOnHand.clone();
// Add it to the game
this.app.root.addChild(arrow);
var player = this.entity;
// Its force is in the direction the player is facing
this.force = new pc.Vec3();
this.force.copy(player.forward);
this.force.scale(this.power);
var pos = player.getPosition();
arrow.setPosition(pos);
var arrowRotation = player.getRotation();
arrow.setRotation(arrowRotation);
arrow.rotateLocal(90,0,0);
arrow.enabled = true; //Must enable after setting position!
//arrow.rigidbody.applyImpulse(this.yForce, this.force);
arrow.rigidbody.applyImpulse(this.force);
this.power = 30;
this.ammo--;
this.setPowerBarValue(0);
};