在 Unity C# 中为棋盘游戏制作一系列程序跳转

问题描述 投票:0回答:1
   I need to make a procedural jump in a board game which is called every time when I need to do so in the script. This allows player to move between waypoints. So for example the player gets the number 6 from a game dice and the script has to make 6 jumps through waypoints.

我尝试过使用 **Vector3.MoveTowards ** 在航路点之间移动,但它并不像我想象的那样工作。我也尝试过使用跳跃动画,但它与某个位置相关,因此如果动画在 1 到 2 点之间,我无法在 2 到 3 点之间移动。 请说我。我可以使用动画吗?如何使用它?或者我如何修改我的脚本,使其变为:

  1. 继续下一点
  2. 移动到它
  3. 将此点作为当前点
  4. 采取下一点 ...
c# unity-game-engine animation unityscript procedural-programming
1个回答
0
投票

为此,需要将 Transform 字段放入数组中。然后实现类似于我下面编写的代码。借助DoTweenPro插件并使用Jump命令,您的问题将得到解决。

public int goTo = 0;
public Transform[] points;
private int currentIndex = 0;
public float jumpPower = 1f;
public float jumpDuration = 1f;
void JumpTo(int index) // This is a recursive function that moves the character closer to the desired point with each jump.
{
    if (currentIndex != index)
    {
        currentIndex += index > currentIndex ? 1 : -1;
        transform.DOJump(points[currentIndex].position, jumpPower, 1, jumpDuration).OnComplete(() =>
        {
            if (index != currentIndex)
            {
                JumpTo(index);
            }
        });
    }
}

指定场景中的变换后,只需在上面的脚本中添加一个如下所示的简短测试即可!

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        JumpTo(goTo);
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.