I am quite new to Godot and now, i have a little problem. I am using a code from youtube to help me build a first person camera system that follow the mouse cursor on where it goes, so if you turn right with the mouse the camera looks well, to the right. And ok, the camera is workin as expected with the X axis, but when looking down/up? No, it just simply don't work. The Y axis for some reason, even if in the videos that i follow the code it works perfectly, with me it doesn't. Here's the code for better understanding:
...
const mous_sens = 0.3
...
@onready var neck := $Neck
@onready var camera := $Neck/Camera3D

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _unhandled_input(event):
if event is InputEventMouseMotion:
neck.rotate_y(deg_to_rad(-event.relative.x * mous_sens))
camera.rotate_x(deg_to_rad(-event.relative.y * mous_sens))
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))

Thanks to anyone that can help me with that! Any help is highly appreciated 🙂

    CaioM No, it just simply don't work

    "Simply doesn't work" is not a useful way to describe a problem. When communicating software bugs/problems you should describe the "symptoms" in as precise terms as possible. In what way it "doesn't work"? What are you seeing vs what are you expecting to see?

    That said, the problem is likely in your scene setup. Post the node hierarchy including initial transforms on Neck and Camera nodes.

      CaioM We need to see more code. Also did you test if you get good input values? Maybe your code is fine but you get no input.

      4 days later

      xyz sorry for the long wait for a reply, my pc didn't quite work in those days and i have been quite busy trying to fix it. With that being said, i will now try to answer you.

      When i said it didn't quite work, i forgot to really detail why it didn't work. Basically, when i moved the mouse to look up, what was expected was that the camera looked up as normal, however, not just it didn't look up, the camera just rotated, clockwise when moving the mouse up, and anti-clockwise when moving the mouse down.

      And for my node hierarchy, here's a print to better understand how it is all setup

      • xyz replied to this.

        CaioM Make sure that both nodes - camera and neck - have zeroed-out rotations and 1,1,1 scale.

          xyz how can i do that? sorry im still very new to godot, adjusting things out also

            CaioM With a node selected look under Node3D/Transform section in the Inspector:

            For both nodes rotation should be 0,0,0 and scale 1,1,1

              CaioM Oh nevermind, already checked that, and yes, already done some tweaking

              • xyz replied to this.

                CaioM All parent nodes of your character body also shouldn't be rotated. So check that too. Also if your script is rotating the character body that will affect all the children including neck and camera.

                xyz yeah, that checked out! basically the rotation was off, so once zeroed out, they started looking up and down as intended. Now though, when i press the WASD keys, they do not move where the camera is pointing at, and that is very strange, as they were working just fine even with the camera problem

                • xyz replied to this.

                  CaioM Hard to tell without seeing your whole script.

                    xyz var input_dir = Input.get_vector("LEFT", "RIGHT", "UP", "DOWN")
                    var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
                    if direction:
                    velocity.x = direction.x * SPEED
                    velocity.z = direction.z * SPEED
                    else:
                    velocity.x = move_toward(velocity.x, 0, SPEED)
                    velocity.z = move_toward(velocity.z, 0, SPEED)

                    move_and_slide()

                    Basically what was making my WASD keys move where my neck was pointing out was the "var direction", but now, it doesn't work. Maybe it was the rotation being zeroed out? if it's of any help i could provide with more code, but the rest don't really connect in any way with the movement code and the "camera section" of the code, applies only to the camera and neck

                      I don't think you need a neck in a fps game and can put the camera directly as a child to the characterbody.
                      Unless you want to let the player look sideways when walking forward for example.

                      Usually you would rotate the characterbody on the y axis for horizontal looking:

                      rotate_y(deg_to_rad(-event.relative.x * mouse_sensitivity))
                      camera.rotate_x(deg_to_rad(-event.relative.y * mous_sens))
                      camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))

                      CaioM Reverse the velocity vector. You're aligning the velocity with your positive basis.z while camera is looking down the negative basis.z by default.

                      CaioM

                      Okay, you are using the neck.transform.basis. So your direction should be relative to your neck rotation. 🤔

                      Also I saw in your scene tree, that the player is not saved and instanced as a scene and is also a child of some MeshInstance3D.

                      I tested it with the following setup and everything works fine.
                      Your issue might be that your Player is a child of some other object that maybe is rotated or scaled and inherits that rotation.

                      Player scene:

                      extends CharacterBody3D
                      
                      const SPEED = 5.0
                      const JUMP_VELOCITY = 4.5
                      
                      @export var mouse_sens:= 0.4
                      @onready var neck: Node3D = $Neck
                      @onready var camera_3d: Camera3D = $Neck/Camera3D
                      
                      var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
                      
                      
                      func _input(event: InputEvent) -> void:
                      	if event is InputEventMouseMotion:
                      		neck.rotate_y(deg_to_rad(-event.relative.x * mouse_sens))
                      		camera_3d.rotate_x(deg_to_rad(-event.relative.y * mouse_sens))
                      		camera_3d.rotation.x = clamp(camera_3d.rotation.x, deg_to_rad(-90), deg_to_rad(90))
                      
                      
                      func _physics_process(delta: float) -> void:
                      	if not is_on_floor():
                      		velocity.y -= gravity * delta
                      
                      	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
                      		velocity.y = JUMP_VELOCITY
                      
                      	var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
                      	var direction := (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
                      	if direction:
                      		velocity.x = direction.x * SPEED
                      		velocity.z = direction.z * SPEED
                      	else:
                      		velocity.x = move_toward(velocity.x, 0, SPEED)
                      		velocity.z = move_toward(velocity.z, 0, SPEED)
                      
                      	move_and_slide()

                      Edit:
                      My level scene setup:

                        trizZzle Hey! Just wanna thanks the help of you two, really, i'm very glad that you could help me. I guess many problems with aligning character/camera can be solved by just zeroing the rotation, it's really that easy lol....Just had to zero the rotation of my characterbody and there you go, the issue is no longer.

                        I'm still gonna test some things out and see if everything is on track really, but for now, that's it, my character is looking to all directions just fine and it's moving as intended

                        Hey, good to hear it's working. 🙂