Hey, we’re making a FPS game but we have a problem with our shooting mechanism. Everytime we shoot, no bullet comes out. We’ve tried everything and are desperate! If you help we will be eternally grateful to you! BTW, the gunshot sound works but the bullet doesn’t work.
Shooting part of FPS script:
FirstPersonMovement.prototype.shoot = function(dt) {
// Clone the bullet as specified by the attribute
var bullet = this.bullet.clone();
// Add it to the game
this.app.root.addChild(bullet);
var player = this.entity;
var gun = this.app.root.findByName('gun');
// Its force is in the direction the player is facing
this.force = new pc.Vec3();
this.force.copy(gun.forward);
this.force.scale(this.gunpower);
var pos = gun.getPosition();
var direction = gun.forward;
// Add it a little further if the player is already going fast
// pos.add(direction.scale(5 + gun.rigidbody.linearVelocity.length() * 0.01));
bullet.setPosition( pos );
// try to get bullet to face forward
bullet.setLocalEulerAngles(0, 0, 0);
// var bulletRotation = player.getRotation();
// bullet.setRotation( bulletRotation );
// bullet.rotateLocal(90,0,0);
bullet.enabled = true; //Must enable after setting position!
bullet.rigidbody.applyImpulse(this.force);
//reduce ammo by 1
this.app.ammo--;
this.entity.sound.play('gunshot');
// destroy cloned bullet after 4 seconds
setTimeout(function(){
bullet.destroy();
}, 4000);
};
Sure. Add tester29 to the project. I am quite busy this week, but I will try my best to take a look. However, unless the project is private, posting a public link, especially if it’s a smaller project only about the issue(this can be done if the main project is private as well) helps a lot, and it can also assist people who have the same problem as you in the future .
Hi @NextLevelEntertainme! I tested your project. Your bullet is simply too small to see it. Apart from that, the force you use doesn’t work. One of the reasons is probably that the bullet is a child of the player.
Hey @NextLevelEntertainme, in addition to what @Albertos said, see if you are initiating the cloned bullet at the right point. Currently it seems to be starting somewhere near the ground, so you will need to modify the shoot function accordingly. Other than that, you will need to unparent the bullet from the player, and simply put it under root, as @Albertos suggested.
So I unparented the bullet and enlarged it and moved it to the camera. I tried manipulating the code a little, but nothing worked. Now you can see the bullet glitching in the screen if you spam click, but it is not moving or working. How should I modify the shoot function? How should I fix the force?
So I tried adding this impulse bit and I think it messed up the rigidbody of the player and gun, as well as making a weird shaking effect when you launch the game. The bullet still doesn’t launch for some reason. I tried manipulating the numbers too, but that didn’t work. The bullet may be going sideways instead of straight? Could you please explain what is wrong with the force? Or maybe the bullet entity is not correct? Thanks for all the help, by the way, we really really appreciate it!
Check in the editor which axis and in which direction the bullet goes forward. Apply in your script the positive or negative impulse on that axis. Also make sure the bullet is cloned on the right position and direction. The easiest way is to do this is in the editor. The bullet has to be a child of the player in that case. You can then reparent the bullet by script before applying the impulse. If there is still a problem, try to disable for example the collider of the player to see if it solve the problem.
I just looked at your project and that’s why I recommend to use a different model or modify your model in Blender. The bullet is not straight on an axis and also the pivot is not in the middle. This makes it almost impossible to get the bullet in the right direction.
Alternatively, you can add the bullet model as a child to a bullet parent and position it to match the axis of the parent. Below you can see how I did that.
Bullet parent with collision and rigidbody component:
Hey @Albertos, so I did what you told me to regarding the bullet child and parent. I think now the only issue is a minor flaw in my code (btw, it doesn’t let me reparent the bullet because it says reparent is not defined?) because the impulse does not push the bullet. The old force will not work either. Do you think there is a bug here? And if so, could you tell me which specific lines to fix or add? Thanks a bunch!
FirstPersonMovement.prototype.shoot = function(dt) {
// Clone the bullet as specified by the attribute
var bullet = this.app.root.findByName('bullet').clone();
// Add it to the game
this.entity.addChild(bullet);
var player = this.entity;
var gun = this.app.root.findByName('gun');
// Its force is in the direction the player is facing
var pos = gun.getPosition();
var direction = gun.forward;
// bullet.rotate(0, 90, 90);
bullet.setPosition( pos );
bullet.enabled = true;
bullet.rigidbody.applyImpulse(0, 0, -500);
this.app.ammo--;
this.entity.sound.play('gunshot');
// destroy cloned bullet after 4 seconds
setTimeout(function(){
bullet.destroy();
}, 4000);
};
The impulse is not working because right now the child entity has the name bullet and this child entity has no rigidbody component anymore. The parent entity need to be renamed as bullet and you can rename the child entity for example model, like I did in my example above. Please make sure only the parent entity has a collision component and rigidbody component. The child entity only a model component.
I don’t know why you get an error on the reparent function, I don’t see it in your script. I’m also not sure if you need to reparent a clone because as far as I know it’s not a part of the hierarchy. Maybe someone can clarify this for me.
As the script is not on the root entity and you don’t want the bullet to be a child of the player entity, I don’t think this is the intention anyway. I would replace it with this.app.root.addChild(bullet);.
In the end, the whole parent and child story would prove meaningless, because your bullet becomes so small that you can barely see it. The use of, for example, a sphere or cylinder would therefore be sufficient and probably better for the performance of your game.
Ok, so what kind of shooting do you want? Raycast? (Instant hits with no drop) physics based? (It can drop and spread) and as the second part does it come from the camera entity being masked by a fake bullet or does it actually come from the game.
Right now we are using physics based shooting, but I think it it better if we turn 180 degrees and switch to hitscan (raycast) for the actual shooting of the current gun, making this forum worthless right now. We will add effects later, like particles. So we would love it if you can change our code to raycast shooting! (We will help in whatever way you ask) A raycasting resource
FpsController.prototype.processRaycast = function() {
var centerScreenX = this.app.graphicsDevice.width / 2;
var centerScreenY = this.app.graphicsDevice.height / 2;
var start = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.nearClip);
var end = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.farClip);
// raycast between the two points and return the closest hit result
var result = this.app.systems.rigidbody.raycastFirst(start, end);
if(result) {
console.log("You Hit: " + result.entity.name);
result.entity.fire("activate");
} else {
console.log("Nothing Hit");
}
};
Attach this function to your left mouse button down event. If you open up the debugging in your browser you will see what you have hit when you click the left mouse button.
It is important that your objects that you are firing at have collision and rigid body.