32 lines
678 B
C#
32 lines
678 B
C#
|
using System;
|
||
|
using Godot;
|
||
|
|
||
|
public partial class MoveUpDown : RigidBody2D
|
||
|
{
|
||
|
[Export] public Node2D Up;
|
||
|
[Export] public Node2D Down;
|
||
|
|
||
|
[Export] private double speed = 1f;
|
||
|
|
||
|
private double t = 0f;
|
||
|
|
||
|
// Called when the node enters the scene tree for the first time.
|
||
|
public override void _Ready()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||
|
public override void _Process(double delta)
|
||
|
{
|
||
|
t += delta * speed;
|
||
|
t = t % 2f;
|
||
|
|
||
|
double posT = Math.Abs(t - 1f);
|
||
|
|
||
|
MoveAndCollide(new Vector2(
|
||
|
(float)Mathf.Lerp(Up.Position.X, Down.Position.X, posT),
|
||
|
(float)Mathf.Lerp(Up.Position.Y, Down.Position.Y, posT)
|
||
|
) - Position);
|
||
|
}
|
||
|
}
|