Advertisement
actuallykron

Godot 4 - Player Move To Click

Sep 23rd, 2023 (edited)
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 0.66 KB | Source Code | 0 0
  1. extends CharacterBody2D
  2.  
  3. var speed = 300
  4. var click_position = Vector2()
  5. var target_position = Vector2()
  6.  
  7. func _ready():
  8.  
  9.     # Set the click position to the player's current position
  10.     click_position = position
  11.  
  12. func _physics_process(delta):
  13.  
  14.     # This input will need to be created in the input map
  15.     if Input.is_action_just_pressed("left_click"):
  16.         click_position = get_global_mouse_position()
  17.        
  18.     # Check if the player is in a 3px range of the click position, if not move to the target position
  19.     if position.distance_to(click_position) > 3:
  20.         target_position = (click_position - position).normalized()
  21.         velocity = target_position * speed
  22.         move_and_slide()
Tags: Godot godot 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement