Need Urgent Help With Shooting Script!

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); 
     
     
};

Ask any questions and we will answer promptly!

Hey @NextLevelEntertainme and welcome,

Could you please send a link to your project? That’ll help us look into the issue better.

1 Like

Can I add you to the game? I will remove you after the problem is solved if you wish. Thanks for the quick response!

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 :wink:.

https://playcanvas.com/project/815788/overview/firestorm-new-version

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.

2 Likes

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.

1 Like

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?

Are you sure this code is correct? Because that is the force that you apply. If your are not sure, try if the rule below moves the bullet.

bullet.rigidbody.applyImpulse(0, 0, 500);
2 Likes

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.

image

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:

Bullet child with model component:

1 Like

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);.

I’m curious what this rule does.

2 Likes

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

1 Like