Advertisement
actuallykron

Godot 3.5 - Player Move To Click

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