I have a problem with ammo in my game. I've already programmed to shoot bullets right in front of my character and forward, but when I turn the camera, the bullets kind of change direction. Here's the bullet code:

`extends CharacterBody3D

const SPEED = 20.0
var DIRECCION = self.basis.z

func _process(delta: float) -> void:
position += DIRECCION * SPEED * delta`

And here's what I got in player's code:
`var Bullet = preload("res://scenes/bullet.tscn")
@onready var pivote = $visuals/Player/Gun/Spatial

func _input(event):
if event.is_action_pressed("shoot"):
var bullet = Bullet.instantiate()
add_child(bullet)
bullet.global_transform = pivote.global_transform`

This happens when turning the camera counterclockwise:

Let me know if you want to know more of my code.

  • xyz replied to this.

    Where is the camera in the scene tree? How is it positioned there relative to the character and the bullets?

    This is what I got in my tree:

    ORposes2024c is the Player. I got the Bullet in a separate scene.

    orart Bullets shouldn't be parented to the player. Also DIRECCION is initialized from the local basis value before the bullet is added to the scene tree so doing bullet.global_transform = pivote.global_transform after adding the bullet will have no effect on the movement/velocity direction.

    Ok. I've just added get_tree().get_root().add_child(bullet) and it seems the bullets don't turn around a lot like before. Just a little bit.
    Any better suggestions?

    Assign pivote.global_basis.z to bullet.DIRRECTION upon adding it to the tree.