-1

我有一个基于网格的级别,每个网格大小为 1x1 单位(默认)。如何让我的游戏对象在特定方向(吃豆人)加速穿过车道,直到输入改变其方向,或者如果它与死角墙碰撞并转向其右侧?运动需要与瓷砖的中间对齐。下面给出了一个带有玩家对象的关卡示例。绿色的物体是玩家,红色的圆柱体是目标,图中的场景是没有按下任何输入按钮,从而允许玩家对象自动左转。

截屏

4

1 回答 1

0

@萨夫隆。是的,我做到了。目前,我只能在按下一个键时移动,然后检查该图块是否可行走。如果是,则进行移动。这只发生在我按住钥匙的时候。下面是相同的代码。

public GameObject levelCreator_;
levelCreator temp;
Vector3 playerPos;

int[,] mapArray = new int[13,17];
public bool inhibitPlayerInput;

void Start () {
    DOTween.Init(true, true, LogBehaviour.Verbose).SetCapacity(6000, 6000);

    levelCreator_ = GameObject.Find ("LevelCreatorGameObject");
    temp = levelCreator_.gameObject.GetComponent<levelCreator>();
    mapArray = temp.mapArray;
    for(int i = 1; i<12; i++)
    {
        for(int ii = 1; ii < 16; ii++)
        {
            if( mapArray[i,ii] == 2)     // tile with value 2 is the starting position of the player
            {
                playerPos = new Vector3(i,ii,0);
            }
        }
    }

    transform.position = new Vector3(playerPos.x,playerPos.y,0);
}

void Update () {
    if (inhibitPlayerInput) return;
    getInput();
}

void getInput()
{
    bool inputPressed = false;
    Vector3 newPlayerPos = playerPos;

    if(Input.GetKey(KeyCode.W))
    {
        inputPressed = true;
        newPlayerPos += new Vector3(-1,0,0);
    } 
    else if (Input.GetKey(KeyCode.S))
    {
        inputPressed = true;
        newPlayerPos += new Vector3(1,0,0);
    } 
    else if(Input.GetKey(KeyCode.A))
    {
        inputPressed = true;
        newPlayerPos += new Vector3(0,-1,0);
    }
    else if(Input.GetKey(KeyCode.D))
    {
        inputPressed = true;
        newPlayerPos += new Vector3(0,1,0);
    } 

    if (!inputPressed) return;

    if (mapArray[(int)newPlayerPos.x,(int)newPlayerPos.y] == 1)      // Unwalkable tile check
    {
        return;
    }
    playerPos = newPlayerPos;

    if(mapArray[(int)playerPos.x,(int)playerPos.y] == 0 || mapArray[(int)playerPos.x,(int)playerPos.y] >= 2)
    {
        inhibitPlayerInput = true;
        transform.DOMove(playerPos, TweenWpDuration).OnComplete(() => inhibitPlayerInput = false).SetEase(Ease.Linear);
        return;
    }
}
于 2014-10-05T03:18:57.037 回答