Can someone give me an idea how to code bullets for a FPS? I have created a code:
this.fireRateTimer -= fireRate * dt; if(this.fireRateTimer <= 0){ //Enter script to shoot a bullet here. this.fireRateTimer = 1; }
This is a timer that is used to make sure that the bullet is only fired a certain amount of times in a second. The thing is, how do I get it to shoot a bullet? I am using a modified version of the first person controller from the tutorial to create the controls.
The way I did it in this (hold the mouse down to shoot) project was to have a template bullet, then every time you shoot a bullet it would do the following process:
Clone the template bullet
Position the bullet at the barrel of the gun (start position)
Calculate the end position (this is the camera world position, but with the Z axis scaled by the gun firing range)
Use ‘lookAt(endPosition)’ to face the bullet in the direction of shooting
Update the bullet in the update function every frame to move along its local Z axis
My code where I create and position the bullet is here in the shoot and fireLaser functions
The code where I move the bullet every update is in a script which is attached to the bullet object