Godot Version
4.4.1
Question
So, in a previous question, I asked how to direct a muzzle raycast to the middle of the player’s screen.
I figured out how to implement that literally 15 minutes ago from typing this.
Now, I need to figure out how to shoot the projectile along the muzzle’s raycast.
The code for projectile shooting is the following:
func primary_action() -> void:
# Update the camera raycast
Camera_Raycast.force_raycast_update()
# Create collision variables
var Camera_Raycast_Collision_Point : Vector3 = Camera_Raycast.get_collision_point()
var Target_Raycast_Location : Vector3 = Muzzle_Raycast.to_local(Camera_Raycast_Collision_Point)
# Update muzzle raycast to go toward camera raycast hit location
Muzzle_Raycast.target_position = Target_Raycast_Location
Muzzle_Raycast.force_raycast_update()
# Instantiate projectile
var Projectile_Instance : Node = Test_Projectile.instantiate()
# Set projectile spawn position at the weapon muzzle
Projectile_Instance.Projectile_Spawn_Position = Temp_Gun_Position.global_position
# Set projectile spawn rotation
Projectile_Instance.Projectile_Spawn_Rotation_X = Camera_Reference.global_rotation.x * -1
Projectile_Instance.Projectile_Spawn_Rotation_Y = Camera_Pitch.global_rotation.y
Projectile_Instance.Projectile_Spawn_Rotation_Z = Camera_Reference.global_rotation.z * -1
# Spawn projectile in scene
add_sibling(Projectile_Instance)
I would send a video, but I’m currently live as I write this, and didn’t test how a screen recording would react.
Let me know if you need any more information. Thanks in advance.