Reputation: 151
I am making a third person game in godot4, and my third person camera has been flipping around when I drag my mouse up. How do I make it so that it sticks there and not allow it to flip over like other third person games?
My code right now (for player movement and camera):
extends RigidBody3D
@onready var horizontal_pivit = $HorizontalPivit
@onready var vertical_pivit = $HorizontalPivit/VerticalPivit
var mouse_sensitivity := 0.001
var horizontal_input := 0.0
var vertical_input := 0.0
func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _process(delta) -> void:
var input := Vector3.ZERO
input.x = Input.get_axis("left", "right")
input.z = Input.get_axis("forward", "backward")
apply_central_force(input * 1200.0 * delta)
horizontal_pivit.rotate_y(horizontal_input)
vertical_pivit.rotate_x(vertical_input)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
horizontal_input = -event.relative.x * mouse_sensitivity
vertical_input = -event.relative.y * mouse_sensitivity
my player scene:
Upvotes: 1
Views: 135
Reputation: 40295
As I mentioned in the prior answer, you want to store the angle (a new script level variable), you want to wrap it so it always represents a rotation the sorter way (e.g. it will be a negative quarter turn instead of positive three quarter turns), and for the vertical you want to clamp it to a minimum and maximum angle. Now you have discovered why.
Since you would be storing the angle, it makes sense to compute the rotation and set it, instead of doing relative rotations. Which I was doing by setting the basis of the transform of the pivots.
Upvotes: 1