0 votes

I have a projectile weapon that works great but if I back up at a certain distance, the bullet.instance seems to stop registering collisions with everything.

here's the code for the bullet. If you think the problem is in the weapon itself I can share the code for it too.

extends RigidBody

var shoot = false

const DAMAGE = 50
const HEADSHOT_DAMAGE = DAMAGE * 2.5
const SPEED = 10

func ready():
set
as_toplevel(true)

func physicsprocess(delta):
if shoot:
apply_impulse(transform.basis.z, -transform.basis.z * SPEED)

func onAreabodyentered(body):
if body.isingroup("Enemy"):
body.health -= DAMAGE
queuefree()
else:
queue
free()

in Engine by (27 points)

1 Answer

0 votes
Best answer

I believe the problem lies in how you speed up the bullet. If the weapon script sets shoot = true, then each physics frame impulse is added to the bullet. Soon the bullet goes at such speed that it clips right through everything.

A solution is to apply the impulse just once. For example, instead of setting a shoot variable, make a shoot() function:

func shoot():
    apply_impulse(transform.basis.z, -transform.basis.z * SPEED)

If you feel that the bullet moves too slowly, and adjusting speed higher causes again the clipping problem, you may need to look into raycasting to check collision.

by (1,100 points)
selected by

tryed to use a function called shoot that is called by the gun every time it spawns a new bullet. Now all my bullets point in the same direction? did I do something wrong?

also, can you tell me more about the ray cast option? is it too hard to learn? I'm still a newbie

I can't be sure without the code you have for instancing bullets, but I'd imagine there's a mistake in the order of commands. If you call shoot() before setting the rotation of the new bullet, it will go in the default direction. The right order is:

  • Create a bullet
  • Rotate and place
  • shoot()

As for raycasting, it's not too hard, but requires a bit more code. Instead of creating an actual bullet object, you draw an imaginary line from the weapon forwards. Check what this line hits, and act accordingly. Check what the docs say: link. There's also a lot of video tutorials on this topic: search from Youtube

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.