Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | AuraBunny |
Hello, I’m a beginner in godot.
I just tried the Navigation Polygon demo project.
Since I prefer c #, convert navigation.gd to NavigationMap.cs.
Everything works fine, but the character movement speed is abnormally fast and no speed value is applied.
what am i wrong?
source code is below.
using Godot;
using System;
public class NavigationMap : Navigation2D
{
[Export]
public float speed = 400.0f;
public Sprite player;
private Vector2[] path = null;
private int pathIdx = 0;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
GD.Print("NavigationMap is ready.");
player = GetNode<Sprite>("Character");
SetProcess(false);
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("click") == false)
return;
GD.Print("NavigationMap is click.");
UpdateNavigationPath(GetLocalMousePosition());
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
MoveAlongPath(speed * delta);
}
public void UpdateNavigationPath(Vector2 destination)
{
path = GetSimplePath(player.GetPosition(), destination, true);
pathIdx = 0;
SetProcess(true);
}
public void MoveAlongPath(float distance)
{
// set start position.
var lastPoint = path[pathIdx];
// start 1 index because 0 index is current position.
pathIdx++;
while (pathIdx < path.Length)
{
var distanceBetweenPoint = lastPoint.DistanceTo(path[pathIdx]);
if (distance <= distanceBetweenPoint)
{
player.SetPosition(lastPoint.LinearInterpolate(path[pathIdx], distance / distanceBetweenPoint));
return;
}
else
{
lastPoint = path[pathIdx];
pathIdx++;
distance -= distanceBetweenPoint;
}
}
player.SetPosition(lastPoint);
SetProcess(false);
}
}