Launching a RigidBody from a weapon
In my game, there's a launcher weapon (sort of like a grenade launcher) which throws
RigidBody bullets. The player aims the weapon, and launches the bullet from the muzzle of the weapon. My problem is that I can't get the bullet to properly fire. I use code stolen based upon the code from the third person shooter demo. It uses the crosshairs on the screen to determine where to fire the bullet. The problem is that the bullet doesn't fire in the correct direction; it'll fire off to the left, right, or wherever.
Here's my current code:
# The code for the weapon var central_point: Vector2 = $Crosshairs.rect_position + $Crosshairs.rect_size * 0.5 var fire_point: Vector3 = $muzzle.global_transform.origin var ray_from: Vector3 = camera.project_ray_origin( central_point ) var ray_direction: Vector3 = camera.project_ray_normal( central_point ) var ball: RigidBody = bullet.instance() var scene_root = get_tree().get_root().get_children() scene_root.add_child( ball ) ball.shooter = player_node ball.global_transform.origin = $muzzle.global_transform.origin var target: Vector3 = ray_direction + ray_from * 100 var direction: Vector3 = (target - fire_point).normalized() ball.direction = direction * firing_strength # The code for the bullet func _integrate_forces(_state): apply_impulse(global_transform.origin, direction)
This code is called when the player presses the fire button (i.e. in the
_input() function). I would think that's the problem, but I have another weapon that's also fires in the
_input() function, and that works correctly. If anyone has other ideas, I would like to read them.