Go, Go, Godot!
  • 0

Projectiles going through collision objects

October 27, 2022

Ever had the problem where you’re firing a bullet or some kind of projectile at high speeds, and it just goes right through the collision object, instead of hitting it?

Here’s a weapon that fires a bullet at random velocities, to demonstrate the issue:

The bullets impact the character in various places, rather than at the first point of contact. I haven’t found a way to enable any kind of continuous contact option, outside of playing with the safe_margin setting. I ended up adding a raycast:

The ray cast looks somewhat like this:

func _physics_process(delta):
	ttl -= delta
	match state:
		STATES.FIRED:
			var collision: KinematicCollision2D = move_and_collide(velocity * delta)
			if collision: _on_collision(collision.get_collider(), collision.get_position())
			
			var rq = PhysicsRayQueryParameters2D.create(_last_pos, position, collision_mask)
			rq.collide_with_areas = true
			var res: Dictionary = get_world_2d().direct_space_state.intersect_ray(rq)
			if res.has("collider"):
				_on_collision(res.collider, res.position)
				position = res.position
			elif ttl <= 0:
				state = STATES.EXPIRED
		# ...

It’s definitely a hack, though. The move_and_collide should be replaced by the ray query. Using both instructions together could result in an even weirder situation; move_and_collide could skip a collision object (the issue we’re trying to fix with the ray query), but then still collide with a different collision object. Then the ray query redoes the same movement and collides with the first object that was missed by move_and_collide . Depending on the game, that could mean something like randomly shooting through shields or walls under specific circumstances.

The current approach is essentially a two-pass solution, where the first pass is sloppy, and the second pass works as intended, but doesn’t move the projectile.

Posted in Godot.
Share
PreviousMaking videos for the web with Godot 4’s Movie Writer
NextAudio Manager to handle the loading of sound effects in bulk

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

  • July 17, 2023

    Terrain3D for Godot has been released

    The Terrain3D addon for Godot lets you create and manage 3D terrains within Godot. It looks quite promising for making landscapes, hills, valleys, and other natural environments. The addon provides tools for sculpting the terrain, and you can paint different textures like grass, dirt, or rock, and blend them smoothly. It also supports features like …

  • November 27, 2024

    Toggling Visibility of Nested CanvasLayers

    I had a setup with nested CanvasLayer nodes. Toggling the visibility of the root CanvasLayer doesn’t hide any nested CanvasLayer nodes. My solution was to listen to the visibility_changed signal, find any CanvasLayer child nodes, and apply the same visibility to them.

  • October 23, 2023

    Fire, Rain, and Black Hole particle effects

    This video is from December last year and uses Godot 4.0-beta7, but I thought it was interesting, because… particle effects!

  • November 17, 2022

    Making videos for the web with Godot 4’s Movie Writer

    Normally I use OBS for screen recording, but there are cases where it makes sense to use Godot’s built-in movie writer that was recently announced. For example, if you have a slow PC or really demanding game, OBS will skip frames. It makes sense since OBS is just recording what’s on the screen in real …

    © 2025 GoGoGodot.io. All rights reserved.