#3D Camera following mouse
20 messages · Page 1 of 1 (latest)
Hello! what do you mean exactly by "move just a bit to follow the mouse movements?"
Something similar to this! But in 3D 🙏
So it has a camera limit, and smooth movements
ah i see, you're probably gonna have to do a tad of math to achieve that behavior
i can try to help you if you want tho
what code do you use to get the position of the mouse?
i got nothing for the moment ><" lmao well it's sure a great feature but if it's that hard i can give up on it
it's not that hard, i can try to write a script for you
omg i would love it
extends Camera3D
# control how much the camera should follow the mouse
@export var follow_intensity: float = 2.0
func _process(delta):
# get the position of the mouse
var mouse_pos: Vector2 = get_viewport().get_mouse_position()
# get the size of the game window/screen
var window_size = get_viewport().get_visible_rect().size
# make sure the mouse position we get is independent from the size of the window/screen
mouse_pos /= window_size.x
# make it so (0, 0) is the center of the screen
mouse_pos.x -= 0.5
mouse_pos.y -= window_size.y / window_size.x / 2.0
# apply the intensity setting
var position_2d: Vector2 = mouse_pos * follow_intensity
# set the position of the camera
position = Vector3(position_2d.x, -position_2d.y, 0.0)
to use it, make the camera a child of a Node3D and make use all the code then would move the camera moves that parent node instead
also, do you understand what all of the code i sent does?
it's better if you do, that way you learn
the movement is currently not smoothed tho, do you want me to smooth it? it will make the code more complicted
It works ! Thanks
I understood most parts, I just don't get how you make this " # make it so (0, 0) is the center of the screen"
try commenting it out, see how it changes things
oh okay, now my mouse base position is set where i've put my mouse when i launch the game
thank you soooo much, you're a genius
@obsidian sphinx here it is with smoothing
extends Camera3D
# controll how much the camera should follow the mouse
@export var follow_intensity: float = 2.0
# control how "sitff" the moevent is. the lower this number, the smoother the motion will be
@export var stiffness: float = 2.0
# store our current position so we can remeber it between frames
var current_pos := Vector2(0, 0)
func _process(delta):
# get the position of the mouse
var mouse_pos: Vector2 = get_viewport().get_mouse_position()
# get the size of the game window/screen
var window_size = get_viewport().get_visible_rect().size
# make sure the mouse position we get is independent from the size of the window/screen
mouse_pos /= window_size.x
# make it so (0, 0) is the center of the screen
mouse_pos.x -= 0.5
mouse_pos.y -= window_size.y / window_size.x / 2.0
# get the position the camera wants to be in
var target_pos: Vector2 = mouse_pos * follow_intensity
# calculate movevent we need to get to said position
var needed_movement = target_pos - current_pos
# move in the movenet we just calculeted, but less, so the camera moves smoothly
current_pos += needed_movement * stiffness * delta
# set the position of the camera
position = Vector3(current_pos.x, -current_pos.y, 0.0)
)