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

  • February 4, 2024

    Inventory System v1.3 available

    Another Inventory System release, and this time we’ve added support for persistence through serialization of inventory data. Features: Bug fixes:

  • February 29, 2024

    Inventory System v1.12 available

    Crafting is here! The latest inventory system version is now available with the following features: The bug fixes:

  • September 29, 2022

    Audio Manager to handle the loading of sound effects in bulk

    Years ago I purchased a game dev bundle on HumbleBundle. Part of that was a sound library called Pro Sound Collection. It’s pretty comprehensive, whether RPG or FPS, there are sounds for a ton of use cases. I might as well use them for something. Luckily for me, the sound collection is pretty well organized. …

  • Godot Game Engine Logo
    September 14, 2023

    Building UIs in Godot 4

    Here’s a collection of tutorials that are helpful if you’re new to using Control and Container nodes to create UIs in Godot 4. The Game Dev Artisan video covers creating a simple UI with a reload indicator for a simple 2D tank game: Clear Code’s 11+ hour Ultimate Introduction to Godot 4 has a chapter …

    © 2025 GoGoGodot.io. All rights reserved.