Collision not working at a distance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pit

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 _physics_process(delta):
if shoot:
apply_impulse(transform.basis.z, -transform.basis.z * SPEED)

func _on_Area_body_entered(body):
if body.is_in_group(“Enemy”):
body.health -= DAMAGE
queue_free()
else:
queue_free()

:bust_in_silhouette: Reply From: aXu_AP

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.

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

pit | 2022-12-14 19:33

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

aXu_AP | 2022-12-14 20:15