GodotComponentTest/components/GroundMotionComponent.cs

28 lines
756 B
C#

using Godot;
using System;
/// <summary>
/// </summary>
public class GroundMotionComponent
{
public float MaxSpeed = 3;
public void Process(float delta, Entity entity, Vector3 target_position)
{
Vector3 direction_xy = new Vector3(target_position.x - entity.GlobalTranslation.x,
0,
target_position.z - entity.GlobalTranslation.z);
Vector3 new_velocity = direction_xy.Normalized() * MaxSpeed;
new_velocity.y = entity.Velocity.y - 9.81f * delta;
if (direction_xy.LengthSquared() > 0.01)
{
entity.Velocity = entity.MoveAndSlide(new_velocity);
}
else
{
entity.Velocity = Vector3.Up * -9.81f * delta;
}
}
}