37 lines
940 B
C#
37 lines
940 B
C#
using Godot;
|
|
|
|
public partial class NPCFollow : CharacterBody3D
|
|
{
|
|
[Export]
|
|
private float _speed = 5f;
|
|
[Export]
|
|
private double _followDistance = 2;
|
|
|
|
private Node3D _player;
|
|
public const float JumpVelocity = 4.5f;
|
|
|
|
public float _gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
|
|
|
|
public override void _Ready()
|
|
{
|
|
_player = GetNode<Node3D>("/root/Main/Player");
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
var playerPosition = _player.Position;
|
|
var targetPosition = (playerPosition - Position).Normalized();
|
|
|
|
if (Position.DistanceTo(playerPosition) >= _followDistance)
|
|
{
|
|
LookAt(playerPosition - Velocity, Vector3.Up);
|
|
Velocity = targetPosition * _speed;
|
|
MoveAndSlide();
|
|
}
|
|
else if (Position.DistanceTo(playerPosition) > _followDistance - 0.4)
|
|
{
|
|
LookAt(playerPosition - Velocity, Vector3.Up);
|
|
MoveAndSlide();
|
|
}
|
|
}
|
|
} |