why are my bullets not coliding with enemies

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

sometimes the bullets colide but most of the time they just go right through each other.

the bullet is a rigid body2d and is only a sprite and a collision shape

the enemy is a kinematic2d with a sprite, collision shape, area2d, and the area 2d has another collision shape.

the code for the bullet is,

if Input.is_action_just_pressed("LMB"):
		fire()

func fire():
	var bullet_instance = bullet.instance()
	bullet_instance.position = get_global_position()
	bullet_instance.rotation_degrees = rotation_degrees
	bullet_instance.apply_impulse(Vector2(), Vector2(bullet_speed,0).rotated(rotation))
	get_tree().get_root().call_deferred("add_child",bullet_instance)

and it is inside the player node,
the code for the enemy is,

    extends KinematicBody2D
    
    var motion = Vector2()
   
    func _physics_process(_delta): 
var player = get_parent(). get_node("Player") 	 	
position += (player.position - position)/50 	
look_at(player.position) 	
    
    move_and_collide(motion)

so does anyone know a fix for this?

:bust_in_silhouette: Reply From: johnygames

This usually happens when a projectile moves too fast. The way collision works is by checking where the projectile is every frame and whether a collision shape has been entered. The engine calculates where the object is going to be in the next frame and if there is no object in the calculated position, then the object keeps moving along its tranjectory. If an object moves too fast and the collision shape is too thin, then collision is not detected. The thickness of the collision shape has to be greater than the distance traveled in a frame, or else the object passes right THROUGH the obstacle because it never finds itself IN the obstacle.

A way to combat this would be to use thicker collision shapes, but I understand that this is impractical since we are talking about tiny bullets.

Another thing you can try is to tweak the Continuous Cd setting from the inspector menu of the rigidbody. This leads to more accurate collisions at the cost of performance (at least for 3d physics).

I haven’t used this setting, but there is a “Cast Ray” option under Continuous Cd which I guess is what you want. Generally speaking, you cast a ray to see what it hits. Check this out for more info. Raycasting works better for bullets because rays detect obstacles along the bullet’s path more quickly and accurately.

Another piece of advice would be to do all updating in the _physics_process(delta) function instead of the _process(delta)since you are dealing with physics and you want actions to be synced with the Physics Engine.

:bust_in_silhouette: Reply From: mdubaisi

what about turning the bullet into area2D, you can watch these videos:

and https://www.youtube.com/watch?v=UpTlc51dGhQ&list=PLyckz_-Rzq6ClGevL2fneJ5YJnMPKWa4M&index=6
by UmaiPixel