Advertisement
actuallykron

Godot 3.5 - Follow Player (Direct)

Feb 8th, 2023 (edited)
2,637
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 KinematicBody2D
  2.  
  3. # Movement speed
  4. export var speed = 100
  5. var player_position
  6. var target_position
  7. # Get a reference to the player. It's likely different in your project
  8. onready var player = get_parent().get_node("Player")
  9.  
  10. func _physics_process(delta):
  11.    
  12.     # Set player_position to the position of the player node
  13.     player_position = player.position
  14.     # Calculate the target position
  15.     target_position = (player_position - position).normalized()
  16.  
  17.     # Check if the enemy is in a 3px range of the player, if not move to the target position
  18.     if position.distance_to(player_position) > 3:
  19.         move_and_slide(target_position * speed)
  20.         look_at(player_position)
  21.  
Tags: Godot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement