Advertisement
actuallykron

Godot 3.5 - Follow Mouse Position

Feb 15th, 2023 (edited)
1,859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 0.48 KB | Source Code | 0 0
  1. extends KinematicBody2D
  2.  
  3. export var speed = 200
  4. var velocity = Vector2()
  5. var mouse_position = null
  6.  
  7. func _physics_process(delta):
  8.    
  9.     # Reset the player's velocity
  10.     velocity = Vector2(0, 0)
  11.     mouse_position = get_global_mouse_position()
  12.  
  13.     # This input will need to be created in the input map
  14.     if Input.is_action_pressed("forward"):
  15.         var direction = (mouse_position - position).normalized()
  16.         velocity = (direction * speed)
  17.    
  18.     move_and_slide(velocity)
  19.     look_at(mouse_position)
Tags: Godot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement