首页 > 解决方案 > 如何在棋盘上创建曲线(统一的立方体),角色在哪里可以选择走或不走另一条路?基于骰子的运动

问题描述

我正在制作一个棋盘游戏,我创建了 2 个脚本,它运行良好且没有错误,当我按下空格键时,角色会根据 1 到 6 的随机数移动,但我不能让他走在弧形板,我希望角色进行调解,并用箭头或钥匙决定他是否要转身。还想将骰子的精灵与正确的随机数相关联,这是“石头”(角色)的脚本:

public Route currentRoute;

int routePosition;

public int steps;

bool isMoving;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space) && !isMoving)
    {
        steps = Random.Range(1, 7);
        Debug.Log("Dice Rolled " + steps);

        StartCoroutine(Move());

        //           if(routePosition+steps < currentRoute.childNodeList.Count)
        //            {
        //               StartCoroutine(Move());
        //          }    
        //           else
        //           {
        //               Debug.Log("Rolled Number is to high");
        //           }
    }
}


IEnumerator Move()
{
    if (isMoving)
    {
        yield break;
    }
    isMoving = true;

    while (steps > 0)
    {

        routePosition++;
        routePosition %= currentRoute.childNodeList.Count;

        Vector3 nextPos = currentRoute.childNodeList[routePosition].position;
        while (MoveToNextNode(nextPos)) { yield return null; }

        yield return new WaitForSeconds(0.1f);
        steps--;
        //routePosition++;
    }

    isMoving = false;
}

bool MoveToNextNode(Vector3 goal)
{
    return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 4f * Time.deltaTime));
}

这里是“节点”(“节点”是棋盘上的空间,角色将根据骰子数字继续移动。):

Transform[] childObjects;
public List<Transform> childNodeList = new List<Transform>();

void Start()
{
    FillNodes();
}

void OnDrawGizmos()
{
    Gizmos.color = Color.green;

    FillNodes();
    
    for (int i = 0; i < childNodeList.Count; i++)
        
    {
        Vector3 currentPos = childNodeList[i].position;
        if (i > 0)
        {
            Vector3 prevPos = childNodeList[i - 1].position;
            Gizmos.DrawLine(prevPos, currentPos);
        }
    }
}

void FillNodes()
{
    childNodeList.Clear();

    childObjects = GetComponentsInChildren<Transform>();

    foreach (Transform child in childObjects)
    {
        if (child != this.transform)
        {
            childNodeList.Add(child);
        }
    }
}

标签: c#

解决方案


推荐阅读