#3D Camera following mouse

20 messages · Page 1 of 1 (latest)

obsidian sphinx
#

Hi !
I'm starting to learn GDScript, and today i wanna make a fixed camera 3D, that move just a bit to follow the mouse movements.
I'll use that for a Tower Defense Game
If anyone could help me pls ! I've searched for a ytb tuto for a while but couldn't find any in 3D :/
Thanks !!

lucid acorn
#

Hello! what do you mean exactly by "move just a bit to follow the mouse movements?"

obsidian sphinx
#

Something similar to this! But in 3D 🙏
So it has a camera limit, and smooth movements

lucid acorn
#

i can try to help you if you want tho

#

what code do you use to get the position of the mouse?

obsidian sphinx
#

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

lucid acorn
obsidian sphinx
#

omg i would love it

lucid acorn
#
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

obsidian sphinx
#

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"

lucid acorn
obsidian sphinx
#

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

lucid acorn
#

@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)
obsidian sphinx
#

omg it works so well! (Better than i expected gdclap )

  • I got it now, not sure I'll be able to remake it but your comments made it really clear
#

thanks !