Godot Version
4.5.1 Stable
Question
I am new to Godot and GDScript. I have a little bit of programming experience but not much. I am trying to understand why this piece of code I wrote works…
I have a first-person character controller script that I wrote. The horizontal look direction should wrap between 0 and TAU, and the vertical look direction should clamp between -90 and 90 degrees.
I had this successfully working in my “look_around()” function, but then I read in the Godot manual about setters and getters and figured I might be able to apply this so that it always works regardless where I set the look direction.
This is how I set it up.
var look_rotation: Vector2 = Vector2(0.0, 0.0):
set(value):
look_rotation = Vector2(clampf(value.x, deg_to_rad(look_down_limit),
deg_to_rad(look_up_limit)), fposmod(value.y, TAU))
@onready var head: Node3D = $Head
func look_around(look_input: Vector2) -> void:
look_rotation.y -= look_input.x * look_sensitivity
look_rotation.x -= look_input.y * look_sensitivity
# Rotate the whole player left and right
transform.basis = Basis() # Reset rotation
rotate_y(look_rotation.y)
# Rotate the player's head up and down
head.transform.basis = Basis() # Reset rotation
head.rotate_x(look_rotation.x)
So this code works as expected, however I don’t understand exactly how. Since the set function takes a Vector2 argument, how is the set function working when I set the x and y vectors individually?