Using -global_basis.z not spawning instance in front of node

Godot Version

4.3

Question

Okay, so I’m new to using transforms, and they’ve been working great for me so far, but I can’t figure out what’s going on.

I’m building a projectile system for my game, and I’m Instantiating a bullet scene that I made, and giving it a direction using -root_node.basis.z(which works), but I’m having trouble setting the bullets location to be in front of the root_node.

I used

instance.global_transform.origin = root_node.global_transform.origin.

Which is able to spawn the bullet at the location I want it to, but I want to spawn it in front of where the root_node is facing, so I used

Instance.global_transform.origin =  -root_node.global_basis.z

which seems to be spawning it using a local vector of some sort, since it is always spawning close to the world origin.

perhaps I should put all my code in there lmao

extends BaseWeapon

class_name ProjectileWeapon

@export var projectile_type : PackedScene = preload("res://projectiles/projectile.tscn")
@export var speed : float
@export var team : float

func activate_action(root_node : Node3D, collider : Node3D, collider_position : Vector3)->void:
	
	if can_activate_action():
		var instance = projectile_type.instantiate()
		
		
		get_tree().root.add_child(instance)
		instance.init(-(root_node.global_basis.z).normalized(),speed, damage,team)
		instance.global_transform.origin = -root_node.global_basis.z
		print(instance.global_transform.origin)
		audio_stream_player.play()

global_transform.origin is global_position so you are setting the instance’s position to a unit vector. You could add the node’s position to the unit vector to get an offset and even multiply it to get further the offset.

const FRONT_OFFSET = 1.0
instance.position = -root_node.global_basis.z * FRONT_OFFSET + root_node.global_position

You’re a beast. Thanks for the help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.