32 lines
801 B
C#
32 lines
801 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 = Velocity.Slerp(targetPosition * _speed, 0.5f);
|
|
MoveAndSlide();
|
|
}
|
|
}
|
|
} |