GodotComponentTest/entities/GoldBar.cs

62 lines
1.6 KiB
C#

using Godot;
using System;
public class GoldBar : KinematicBody
{
private Vector3 targetPosition;
private bool hasTarget = false;
public Vector3 velocity;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
targetPosition = GlobalTransform.origin;
}
public void SetTarget(Vector3 target)
{
targetPosition = target;
hasTarget = true;
}
public void UnsetTarget()
{
hasTarget = false;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _PhysicsProcess(float delta)
{
if (hasTarget)
{
if (targetPosition.IsEqualApprox(GlobalTransform.origin))
{
targetPosition = GlobalTransform.origin;
velocity = Vector3.Zero;
}
Vector3 targetDirection = (targetPosition - GlobalTransform.origin).Normalized();
velocity = targetDirection * (velocity.Length() + 10 * delta);
Transform = new Transform(this.Transform.basis.Rotated(Vector3.Up, delta * 2.0f), Transform.origin);
}
else
{
if (Mathf.Abs(Transform.origin.y) < 0.01)
{
// apply damping when on ground
velocity = velocity - velocity.Normalized() * 0.9f * delta;
}
velocity.y = velocity.y - 9.81f * delta;
}
velocity = MoveAndSlide(velocity);
if (velocity.LengthSquared() < 0.01)
{
velocity = Vector3.Zero;
}
}
}