First person not working properly
Asked Answered
H

18

0

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 ๐Ÿ™‚

Hindward answered 9/11, 2023 at 0:47 Comment(0)
T
0

Hindward 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.

Tasse answered 9/11, 2023 at 1:30 Comment(0)
P
0

What version are you using?

Plan answered 9/11, 2023 at 5:27 Comment(0)
D
0

Hindward 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.

Deviation answered 9/11, 2023 at 9:46 Comment(0)
H
0

Tasse 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

Hindward answered 13/11, 2023 at 17:34 Comment(0)
T
0

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

Tasse answered 13/11, 2023 at 17:41 Comment(0)
H
0

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

Hindward answered 13/11, 2023 at 18:12 Comment(0)
T
0

Hindward 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

Tasse answered 13/11, 2023 at 18:16 Comment(0)
H
0

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

Hindward answered 13/11, 2023 at 18:19 Comment(0)
T
0

Hindward 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.

Tasse answered 13/11, 2023 at 18:21 Comment(0)
H
0

Tasse 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

Hindward answered 13/11, 2023 at 18:23 Comment(0)
T
0

Hindward Hard to tell without seeing your whole script.

Tasse answered 13/11, 2023 at 18:26 Comment(0)
H
0

Tasse 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

Hindward answered 13/11, 2023 at 18:42 Comment(0)
A
0

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))
Aggrieve answered 13/11, 2023 at 18:51 Comment(0)
T
0

Hindward 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.

Tasse answered 13/11, 2023 at 18:54 Comment(0)
A
0

Hindward

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.

Aggrieve answered 13/11, 2023 at 18:57 Comment(0)
A
0

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:

Aggrieve answered 13/11, 2023 at 19:26 Comment(0)
H
0

Aggrieve 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

Hindward answered 13/11, 2023 at 19:57 Comment(0)
A
0

Hey, good to hear it's working. ๐Ÿ™‚

Aggrieve answered 13/11, 2023 at 20:7 Comment(0)

© 2022 - 2024 โ€” McMap. All rights reserved.