0 votes

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?

in Engine by (12 points)

2 Answers

0 votes

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.

by (1,861 points)
0 votes
by (290 points)
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 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 webmaster@godotengine.org with your username.