Enemy follow
This commit is contained in:
20
Scripts/MeleeEnemy.cs
Normal file
20
Scripts/MeleeEnemy.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Godot;
|
||||
using System.Linq;
|
||||
|
||||
public partial class MeleeEnemy : CharacterBody3D
|
||||
{
|
||||
private float _speed = 1.0f;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var players = GetTree().GetNodesInGroup("Player");
|
||||
if (players.Any())
|
||||
{
|
||||
var convertedPlayers = players.Select(x => (Node3D)x);
|
||||
var target = convertedPlayers.OrderBy(x => Position.DistanceTo(x.Position)).FirstOrDefault();
|
||||
GD.Print(Position.DirectionTo(target.Position));
|
||||
Position = Position.MoveToward(target.Position, _speed * (float)delta);
|
||||
MoveAndSlide();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user