How do I stop my camera from flicking/turning around to its default placement? (3D)

Godot Version

4.4

Question

Hello,
I followed Brackeys 3D godot tutorial and am trying to edit the character controller so that it just lets me be in free camera automatically. My problem is that when I place the cameraman in the scene and specify its starting rotation in the inspector it flips back to its “default” position once the mouse moves. Bellow is what I’ve edited the code down to and I would love any information on what’s happening and why its doing this and how I can fix it/ resources I should be looking at. I think it has something to do with the basis and transform but I’m unsure how I would fix it. Thank you!

Here’s the node hierarchy as well:

var mouse_captured : bool = false
var look_rotation : Vector2
var move_speed : float = 10.0

var look_speed : float = .002

var cameraInputDirection:= Vector2.ZERO

func _ready() -> void:
	velocity = Vector3.ZERO
	

func _physics_process(delta: float) -> void:
	var input_dir := Input.get_vector("left","right","up","down")
	var input2 := Input.get_axis("ctrlDOWN","spaceUP")
	var motion := (head.global_basis * Vector3(input_dir.x , 0 + input2, input_dir.y)).normalized()
	motion *= move_speed * delta
	move_and_collide(motion)
	#print(transform)

func _unhandled_input(event: InputEvent) -> void:
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		capture_mouse()
	if Input.is_key_pressed(KEY_ESCAPE):
		release_mouse()
	
	# Look around
	if mouse_captured and event is InputEventMouseMotion:
		
		
		rotate_look(event.relative)


# Rotate us to look around.
# Base of controller rotates around y (left/right). Head rotates around x (up/down).
# Modifies look_rotation based on rot_input, then resets basis and rotates by look_rotation.
func rotate_look(rot_input : Vector2):
	look_rotation.x -= rot_input.y * look_speed
	look_rotation.x = clamp(look_rotation.x, deg_to_rad(-85), deg_to_rad(85))
	look_rotation.y -= rot_input.x * look_speed
	
	rotate_y(look_rotation.y)
	head.transform.basis = Basis()
	head.rotate_x(look_rotation.x)
	print(Me.global_rotation)


func capture_mouse():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	mouse_captured = true


func release_mouse():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	mouse_captured = false

Yes it does have to do with the Identity Basis assignment on this line

It seems like your code would also quickly speed up rotating if you keep turning left or kept right. If true, both issues are because you are storing a look_rotation variable where your rotations are not applied as accumulations instead per-movement with rotate_x/y.

I recommend cutting that look_rotation variable out entirely. And I recommend using event.screen_relative as it’s more consistent on different sized windows (i.e. a user resizes or uses fullscreen)

func rotate_look(rot_input : Vector2):
	rotate_y(rot_input.x * look_speed)
	head.rotate_x(rot_input.y * look_speed)

	const max_tilt: float = deg_to_rad(85)
	head.rotation.x = clampf(head.rotation.x, -max_tilt, max_tilt)
1 Like

This worked! Thank you so much, and also thank you for informing me about rotate_y and rotate_x as I was not aware of them before now.

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