使用切换语句来移动一个字符

问题描述 投票:1回答:1

我想通过使用switch语句让游戏中的NPC移动,然而没有用。

首先,我使用一个函数从0到3中选择一个数字,然后我使用switch语句。

void Start()
    {

       int randomNumber = Random.Range(0, 3);

        switch (randomNumber)
        {
            case 0:// the npc moves towards
                targetPosition.y = 1.4f;
                targetPosition.x = this.transform.position.x;
                targetPosition.z = this.transform.position.z - 3;
                this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
                break;

            case 1://the npc moves back
                targetPosition.y = 1.4f;
                targetPosition.x = this.transform.position.x;
                targetPosition.z = this.transform.position.z + 3;
                this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
                break;

            case 2://the npc moves right
                targetPosition.y = 1.4f;
                targetPosition.x = this.transform.position.x - 3;
                targetPosition.z = this.transform.position.z;
                this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
                break;

            case 3://the npc moves left
                targetPosition.y = 1.4f;
                targetPosition.x = this.transform.position.x + 3;
                targetPosition.z = this.transform.position.z;
                this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
                break;

            default: break;
        }
    }

数字生成了,但NPC没有移动。我在这里做错了什么?

unity3d
1个回答
1
投票

如果你想在第一次移动的时候有一个开始的位置,使用startCoroutine...

void Start()
{
   StartCoroutine(FirstMove(Random.range(0,4));
}

public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
{
    var currentPos = transform.position;
    var t = 0f;
    while (t < 1f)
    {
        t += Time.deltaTime / timeToMove;
        transform.position = Vector3.Lerp(currentPos, position, t);
        yield return null;
    }
    transform.position = position;
}


private void Firstmove(int randomNumber)
{

    //Vector3 targetPosition = new Vector3();
    //float velocity = 10.0f;
    switch (randomNumber)
    {
        case 0: // the npc moves towards
            targetPosition.y = 1.4f;
            targetPosition.x = this.transform.position.x;
            targetPosition.z = this.transform.position.z - 3;
            break;

        case 1: //the npc moves back
            targetPosition.y = 1.4f;
            targetPosition.x = this.transform.position.x;
            targetPosition.z = this.transform.position.z + 3;
            break;

        case 2: //the npc moves right
            targetPosition.y = 1.4f;
            targetPosition.x = this.transform.position.x - 3;
            targetPosition.z = this.transform.position.z;
            break;

        case 3: //the npc moves left
            targetPosition.y = 1.4f;
            targetPosition.x = this.transform.position.x + 3;
            targetPosition.z = this.transform.position.z;
            break;

        default: break;
    }

    StartCoroutine(MoveToPosition(transform, targetPosition, 2f));
}
© www.soinside.com 2019 - 2024. All rights reserved.