Advertisement
actuallykron

Godot 4 - Follow Mouse

Sep 23rd, 2023 (edited)
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 0.59 KB | Source Code | 0 0
  1. extends CharacterBody2D
  2.  
  3. var speed = 300
  4. var mouse_position = Vector2()
  5. var target_position = Vector2()
  6.  
  7. func _physics_process(delta):
  8.    
  9.     mouse_position = get_global_mouse_position()
  10.     target_position = (mouse_position - position).normalized()
  11.    
  12.     # This input will need to be created in the input map
  13.     if Input.is_action_pressed("up"):
  14.             velocity = target_position * speed
  15.             move_and_slide()
  16.    
  17.     # This input will need to be created in the input map
  18.     if Input.is_action_pressed("down"):
  19.             velocity = -target_position * speed
  20.             move_and_slide()
  21.    
  22.     look_at(mouse_position)
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement