I’m trying to add a gun to my FPS Game but whenever I move my looking direction(where I am looking) to the back, it doesn’t show the gun(only the back of it). The code I am using for my first person movement is :
var FirstPersonMovement = pc.createScript('firstPersonMovement');`
FirstPersonMovement.attributes.add('camera', {
type: 'entity',
description: 'Optional, assign a camera entity, otherwise one is created'
});
FirstPersonMovement.attributes.add('power', {
type: 'number',
default: 2500,
description: 'Adjusts the speed of player movement'
});
FirstPersonMovement.attributes.add('jumpImpulse', {
type: 'number',
default: 3,
description: 'Adjusts the impulse of player Jump'
});
FirstPersonMovement.attributes.add('gravity', {
type: 'number',
default: 90,
description: 'Adjusts the gravity'
});
FirstPersonMovement.attributes.add('lookSpeed', {
type: 'number',
default: 0.25,
description: 'Adjusts the sensitivity of looking'
});
FirstPersonMovement.prototype.initialize = function () {
this.force = new pc.Vec3();
this.eulers = new pc.Vec3();
this.canJump = 1; // Use this or I suggest this.canJump = true;
var app = this.app;
app.mouse.on("mousemove", this._onMouseMove, this);
app.mouse.on("mousedown", function () {
app.mouse.enablePointerLock();
}, this);
if (!this.entity.collision) {
console.error("First Person Movement script needs to have a 'collision' component");
}
if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
}
};
FirstPersonMovement.prototype.update = function (dt) {
// If a camera isn't assigned from the Editor, create one
if (!this.camera) {
this._createCamera();
}
var force = this.force;
var app = this.app;
var forward = this.camera.forward;
var right = this.camera.right;
var x = 0;
var z = 0;
if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_Q)) {
x -= right.x;
z -= right.z;
}
if (app.keyboard.isPressed(pc.KEY_D)) {
x += right.x;
z += right.z;
}
if (app.keyboard.isPressed(pc.KEY_W)) {
x += forward.x;
z += forward.z;
}
if (app.keyboard.isPressed(pc.KEY_S)) {
x -= forward.x;
z -= forward.z;
}
if (app.keyboard.isPressed(pc.KEY_SPACE) && this.canJump == 1) {
this.entity.rigidbody.applyImpulse(0, this.jumpImpulse, 0)
}
if (x !== 0 || z !== 0) {
force.set(x, 0, z).normalize().scale(this.power);
this.entity.rigidbody.applyForce(force);
}
if (this.entity.getPosition().y > 3) {
this.canJump = 0
}
if (this.entity.getPosition().y <= 3) {
this.canJump = 1
//this.canJump = true
}
if (this.canJump == 0) {
this.entity.rigidbody.applyForce(new pc.Vec3(0, -this.gravity, 0));
}
// update camera angle from mouse events
this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
};
FirstPersonMovement.prototype._onMouseMove = function (e) {
if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
this.eulers.x -= this.lookSpeed * e.dx;
this.eulers.y -= this.lookSpeed * e.dy;
}
};
FirstPersonMovement.prototype._createCamera = function () {
this.camera = new pc.Entity();
this.camera.setName("First Person Camera");
this.camera.addComponent("camera");
this.entity.addChild(this.camera);
this.camera.translateLocal(0, 0.5, 0);
};