Hi. I’m working on a 3rd person adventure game with attack mechanics. The code I made successfully calls the bullet during a mouse click, but it does not shoot out, staying in the weapon.
This is my current code:
var PlayerMovement = pc.createScript('playerMovement');
PlayerMovement.attributes.add('speed', { type: 'number', default: 0.2 });
PlayerMovement.attributes.add('power', {type: 'number', default: 2500});
PlayerMovement.attributes.add('lookSpeed', {type: 'number', default: 0.25});
PlayerMovement.attributes.add('camera', {type: 'entity',});
// -------- ctobias section ------------------------------------------------------ //
PlayerMovement.attributes.add('gunEntity', {type: 'entity'});
PlayerMovement.attributes.add('bulletPower', {type: 'number'});
// ------------------------------------------------------------------------------- //
PlayerMovement.prototype.initialize = function () {
var app = this.app;
var camera = app.root.findByName('Camera');
this.cameraScript = camera.script.cameraMovement;
// Listen for mouse move events
app.mouse.on("mousemove", this._onMouseMove, this);
// when the mouse is clicked hide the cursor
app.mouse.on("mousedown", function () {
app.mouse.enablePointerLock();
// -------- ctobias section ------------------------------------------------------ //
this.ctobias();
// ------------------------------------------------------------------------------- //
}, this);
};
// Temp variable to avoid garbarge colleciton
PlayerMovement.worldDirection = new pc.Vec3();
PlayerMovement.tempDirection = new pc.Vec3();
PlayerMovement.prototype.update = function (dt) {
var app = this.app;
var worldDirection = PlayerMovement.worldDirection;
worldDirection.set(0, 0, 0);
var tempDirection = PlayerMovement.tempDirection;
var forward = this.entity.forward;
var right = this.entity.right;
var x = 0;
var z = 0;
if (app.keyboard.isPressed(pc.KEY_A)) {
x -= 1;
}
if (app.keyboard.isPressed(pc.KEY_D)) {
x += 1;
}
if (app.keyboard.isPressed(pc.KEY_W)) {
z += 1;
}
if (app.keyboard.isPressed(pc.KEY_S)) {
z -= 1;
}
if (x !== 0 || z !== 0) {
worldDirection.add(tempDirection.copy(forward).mulScalar(z));
worldDirection.add(tempDirection.copy(right).mulScalar(x));
worldDirection.normalize();
var pos = new pc.Vec3(worldDirection.x * dt, 0, worldDirection.z * dt);
pos.normalize().scale(this.speed);
pos.add(this.entity.getPosition());
var targetY = this.cameraScript.eulers.x + 180;
var rot = new pc.Vec3(0, targetY, 0);
this.entity.rigidbody.teleport(pos, rot);
}
this.entity.anim.setFloat('xDirection', x);
this.entity.anim.setFloat('zDirection', z);
};
// user-defined function - ctobias section ----------------------------------------//
PlayerMovement.prototype.ctobias = function(dt){
// step-1 get the center of the screen
var centerScreenX = this.app.graphicsDevice.width/2;
var centerScreenY = this.app.graphicsDevice.height/2;
// step-2 get the start and end point
var start = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.nearClip);
var end = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.farClip);
// step-3 trigger raycas between two points and return the closet hit result
var result = this.app.systems.rigidbody.raycastFirst(start, end);
// step-4 set the bullet asset, identify the gunEntity, apply force and create instance
// step 4.1 bullet asset
var bulletAsset = this.app.assets.get(173129146);
// step 4.2 create the instance of the bullet
var bulletInstance = bulletAsset.resource.instantiate();
// step 4.3 add to hierarchy
this.app.root.addChild(bulletInstance);
// step-4.4 identify the gun and set the force
var gun = this.gunEntity;
this.force = new pc.Vec3();
this.force.copy(gun.forward);
this.force.scale(this.bulletPower);
var pos = gun.getPosition();
bulletInstance.setPosition(pos);
bulletInstance.enabled = true;
bulletInstance.rigidbody.applyImpulse(this.force);
};