Object spawned by a spawner that has a basis on a non-flat surface goes in wrong direction when applied relatively.

Godot Version

Godot 4.2

Question

Hello. I’m trying to wrap my head around this issue im having with setting up these spawners that are set up all around a planet. i can provide some more examples of my code in a few. But Im basically running into this issue where what im spawning which has to move in these given direction vectors i make from the spawner code arent going in the right directions when i put the spawners in places that are upside down or on the sides of the planet. i feel like im close but something with my basis’ are wrong.

this is when it all works correctly, they all are spawning around the circle. its at the top of the planet so nothing has really been rotated so its all acting fine. the projectiles move how they should.

this is under the planet, so theres some rotation/basis changes for the spawner, now the projectiles are going in wild directions all of a sudden

func build_spawn_points():

var gravity_dir = Vector3.DOWN

# Orient the spawner to the planet
if planet != null:
    gravity_dir = planet.get_gravity_dir(global_position)
else:
    gravity_dir = Vector3.DOWN

var basis_target = global_transform.basis
basis_target.y = -gravity_dir
basis_target.z = basis_target.z.slide(basis_target.y).normalized()
basis_target.x = basis_target.y.cross(basis_target.z)
global_transform.basis = basis_target

var angleStep = (2 * PI) / numProjectiles

var start_pos = self.global_position
var angle = Vector3.ZERO
var direction = Vector3.ZERO

for i in range(numProjectiles):

    angle = i * angleStep
    direction = (global_basis * Vector3(cos(angle), 0, sin(angle))).normalized() # basis * 
    start_pos =  self.global_position + (direction * spawnPointOffset)

    positionsDirections.append([start_pos, direction])

this is what's running on the spawners. it sets up the basis to face the right gravity direction. then im making these spawn points in a circle with n number of points. im multiplying my direction by the global_basis so that this allegedly will still be the right direction even if the spawner were to potentially be upside-down.
so im passing this basis relative to the spawner direction then later to the projectile.

```func _physics_process(delta):
    
    if planet != null:
        gravity_dir = planet.get_gravity_dir(global_position)
    else:
        gravity_dir = Vector3.DOWN
        
    up_direction = -gravity_dir
    var basis_target = global_transform.basis
    basis_target.y = -gravity_dir

    
    basis_target.z = basis_target.z.slide(basis_target.y).normalized()
    basis_target.x = basis_target.y.cross(basis_target.z)
    global_transform.basis = global_transform.basis.slerp(basis_target, 0.5)

    # Add the gravity.
    if is_on_floor():
        velocity += gravity_dir * 0.1
    else:
        velocity += gravity_dir * Gravity * delta


    # Apply acceleration based on set direction
    var direction = (global_basis * dir).normalized() #((self.basis.x * dir.x) + (self.basis.z * dir.z)).normalized()
    print("Projectile dir = ", direction)
    var h_velocity = velocity.slide(gravity_dir)
    var v_velocity = velocity.project(gravity_dir)
    
    DebugDraw.draw_line(global_position, global_position + (direction.normalized() * 5))
    
    h_velocity = (direction * max_speed)```

1. i also have debug lines in the pics above. Those seem correct even in the picture where the spawned projectiles are in the wrong directions. So i think im on the right track. i think the issue is since Im dealing with a direction thats made with the basis of the spawner, that isnt translating correctly to the projectile when i then multiply that by the projectiles basis even though thats what i need to do to keep the projectile moving correctly all the way around the planet or it would eventually struggle to get all the way down.