Unity MoveTowards While循环冻结程序

时间:2018-10-25 16:08:02

标签: c# unity3d

我正在尝试让玩家使用MoveTowards移至空白游戏对象的位置。但是,当程序进入我在函数中设置的while循环时,Unity将冻结。我尝试了几种不同的方法来使其正常工作,但要么玩家没有一直移动,要么游戏仍然死机。提前谢谢。

    public KeyCode moveL; //move left
public KeyCode moveR; //move right
public KeyCode fire; //for shooting the bullet

public int laneNum =2;
public string controlLocked = "n"; //locks control when moving
    //The empty game objects for the player to move to.
public Transform LaneZero;
public Transform LaneOne;
public Transform LaneTwo;
public Transform LaneThree;
public Transform LaneFour;

//move towards variables
public float step;
public float speed =5;

    Void Update()
    {
     if((Input.GetKeyDown(moveL)) && (laneNum > 0) && (controlLocked == "n")) //for moving the player left when A is pressed
        {

            laneNum -= 1; //change the lane value
            controlLocked = "y";

            LaneChange();

        }

        if((Input.GetKeyDown(moveR)) && (laneNum < 4) && (controlLocked == "n")) //for moving the player left when A is pressed
        {

            laneNum += 1; //change the lane value
            controlLocked = "y";

            LaneChange();

        }

    }

    /function for the changing of lanes
void LaneChange()
{

    if(laneNum == 0)
    {

        while(transform.position != LaneZero.position)
        {
            step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position,LaneZero.position, step);
        }
    }
    if(laneNum == 1)
    {

        while(transform.position != LaneOne.position)
        {
            step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position,LaneOne.position, step);

        }

    }
    if(laneNum == 2)
    {

        while(transform.position != LaneTwo.position)
        {
            step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position,LaneTwo.position, step);
        }
    }
    if(laneNum == 3)
    {

        while(transform.position != LaneThree.position)
        {
            step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position,LaneThree.position, step);
        }
    }
    if(laneNum == 4)
    {

        while(transform.position != LaneFour.position)
        {
            step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position,LaneFour.position, step);
        }
    }

    controlLocked = "n";


}

0 个答案:

没有答案