Bullets not traveling in straight line

Godot Version

4.6 - stable

Question

Hello I’m trying to implement shooting into a test project but the bullets are moving with the camera not on a straight line and I really can figure out why. They travel along a raycast3d towards the target position. The direction and starting position are set when the object is created so I really am not sure why they are constantly updating. Thank you for any help.

Shooting Script

extends Marker3D
class_name Bullet_Path

@onready var bullet_pivot:Marker3D = $"."
@onready var bullet_path:RayCast3D = $Bullet_Path
@onready var cooldown:Timer = $Shot_Cooldown
var can_shoot:bool = true
@export var sensitivity:float = 0.5
@export var bullet:PackedScene
var bullet_dir:Vector3
var bullet_object:Bullet

func _input(event):
	if event is InputEventMouseMotion:
		bullet_pivot.rotate_x(deg_to_rad(-event.relative.y *sensitivity))
		bullet_pivot.rotation.x = clamp(bullet_pivot.rotation.x,deg_to_rad(-45),deg_to_rad(45))
		
func _physics_process(_delta):
	if Input.is_action_pressed("shoot") and can_shoot:
		primary_fire()

func primary_fire():
	cooldown.wait_time = 0.5
	create_bullet()
	begin_cooldown()

func create_bullet():
	bullet_dir = (bullet_path.to_global(bullet_path.target_position) - bullet_path.to_global(Vector3.ZERO)).normalized()
	bullet_object = bullet.instantiate().duplicate()
	$"../Projectiles".add_child(bullet_object)
	bullet_object.pass_direction(bullet_path.global_position,bullet_dir)


func _on_shot_cooldown_timeout():
	can_shoot = true

func begin_cooldown():
	can_shoot = false
	cooldown.start()

Bullet Script

extends CharacterBody3D
class_name Bullet

var starting_position:Vector3
var bullet_range:float = 5
var direction:Vector3

func move(delta):
	update_postion()
	move_and_collide(direction * 2 * delta)
	
func update_postion():
	var range_difference = (position-starting_position).length()
	if range_difference >= bullet_range:
		queue_free()

func _physics_process(delta):
	move(delta)
	
func pass_direction(start_pos,dir):
	starting_position = start_pos
	direction = dir

Video of problem- https://imgur.com/a/ZGWZWfW

Thanks again for any assistance

I think this is because Projectiles is under Player in the scene tree so when your rotate Player it also rotates all of the projectiles. If you move projectiles outside of player it might work.

2 Likes

You’re awesome thank you