Godot Version
4.5
Question
i need help spawning this projectile from this code and when the owner of this code moves and spawns it when it lands on the ground it spawns it at 0,0,0 instead of where it landed
here is the code
extends CharacterBody3D
@export var projectile: PackedScene
@export var speed: float = 50.0
@export var descent_speed: float = 5.0
var can_spawn: bool = true
A flag to prevent continuous spawning if the spawn process takes multiple frames
var is_spawning: bool = false
Use the exact name of your Marker3D node here.
const SPAWN_POINT_NAME = “Marker3D”
func _physics_process(delta: float) → void:
# Character movement logic
# Only apply movement if we haven't hit the floor yet OR if we're not allowed to spawn anymore.
# If 'can_spawn' is false, it means the character stays put after landing (e.g., a mine or trap).
if can_spawn:
velocity = -transform.basis.z * speed + Vector3.DOWN * descent_speed
else:
# Stop movement entirely once the character's purpose (spawning) is complete/triggered.
velocity = Vector3.ZERO
move_and_slide()
# Trigger spawn when landed AND allowed to spawn AND not currently in the process of spawning
if is_on_floor() and can_spawn and not is_spawning:
spawn_projectile()
func spawn_projectile():
is_spawning = true # Set flag to prevent re-entry
can_spawn = false # Character’s main job is done, stops movement in _physics_process
if projectile == null:
push_error("Error: Projectile scene not assigned in inspector!")
is_spawning = false
return
var spawn_marker = get_node_or_null(SPAWN_POINT_NAME)
if spawn_marker == null:
push_error("Error: Spawn point node ('%s') not found as a child of the character!" % SPAWN_POINT_NAME)
is_spawning = false
return
var parent = get_parent()
if parent == null:
# If the character is not in the scene tree yet, this will fail.
push_error("Error: CharacterBody3D has no parent node!")
is_spawning = false
return
# 1. INSTANTIATE
var new_projectile = projectile.instantiate()
if new_projectile == null:
push_error("Error: Failed to instantiate projectile!")
is_spawning = false
return
# 2. PARENT THE NODE FIRST
parent.add_child(new_projectile)
# 3. APPLY THE GLOBAL TRANSFORM (The robust fix)
# Get the desired position from the Marker3D
var target_global_transform = spawn_marker.global_transform
# Set the projectile's transform. This should correctly position the projectile
# at the exact global position and rotation of the Marker3D.
new_projectile.global_transform = target_global_transform
# --- CRITICAL FIX FOR PHYSICS BODIES (RigidBody3D) ---
if new_projectile is RigidBody3D:
# If the projectile is a RigidBody3D, force it to stop sleeping
# so physics wakes up and accepts the new position.
new_projectile.sleeping = false
# --- CLEANUP / NEXT STATE ---
# If this CharacterBody3D is meant to be immediately destroyed after spawning:
# queue_free()
is_spawning = false # Reset spawning flag
# Optional: print for confirmation
print("Spawn successful. Projectile now at: ", new_projectile.global_transform.origin)
# EnemyCloneWhatch.clone = true