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

  • April 21, 2024

    Inventory System v1.13 available

    It’s been a while, but it was worth the wait. The latest version of the Inventory System is now available, with a long list of features, improvements, fixes, and documentation. This time the focus is on multiplayer. Let’s cover the features first: Bug fixes:

  • April 1, 2023

    Introducing GodotBuilder: Custom Export Templates built on demand

    Need optimized export templates with PCK encryption support but don’t want to have to set up a build pipeline or download the entire compilation toolchain on your computer? Well, now it is. Fill out the form, checkout, and we’ll email you the download link after the compilation completes. Compilation may take 30 minutes to 3 …

  • 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!

  • 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 …

    © 2025 GoGoGodot.io. All rights reserved.