我需要一个将等距方向(从统一等距瓷砖地图)转换为正常方向的函数。例如,在这张图片中,我沿着轨道(红线)创建了航路点 它们不附加到图块地图,而是使用linerenderer漂浮在其上方
所以用这个
Vector3 targetPosition = trackPositions[currentWaypointIndex];
// Move the locomotive towards the target waypoint
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Mathf.Abs(speed) * Time.deltaTime);
// Calculate the direction to the next waypoint
Vector3 direction = (targetPosition - transform.position).normalized;
我找到方向了
然后我想用它来将火车精灵放在轨道上(实际上是 36 个图像),并且我需要一个以度/10 为单位的法线方向来设置正确的图像..
我认为你应该在世界坐标中移动并计算角度,所以你需要这样一个辅助类。
public static class IsometricConverter
{
// I assume your tilemap angle is 45 degrees.
public static readonly YFactor = Mathf.Cos(Mathf.Deg2Rad * 45);
public static Vector2 Isometric2World (Vector2 p) => new Vector2(p.x, p.y / YFactor);
public static Vector2 World2Isometric (Vector2 p) => new Vector2(p.x, p.y * YFactor);
}
现在让我们使用该类。
var targetPosition = IsometricConverter.ToWorld(trackPositions[currentWaypointIndex]);
var currentPosition = IsometricConverter.ToWorld(transform.position);
// Move the locomotive towards the target waypoint
currentPosition = Vector2.MoveTowards(currentPosition, targetPosition, Mathf.Abs(speed) * Time.deltaTime);
transform.position = IsometricConverter.FromWorld(currentPosition)
// Calculate the angle and convert it to 0 ~ 360 degrees
var angle = Vector2.SignedAngle(Vector2.right, (targetPosition - currentPosition).normalized);
angle += 180;
请注意,0 度轴现在面向菱形的右侧。根据您的火车精灵,您可能需要调整角度或反转它。