如何使Pac-Man像持续运动一样,输入允许在车道内转弯,并使用Raycasts自动开启与墙壁的碰撞?

时间:2014-10-04 10:40:17

标签: arrays unity3d pacman

我有一个基于网格的级别,每个网格大小为1x1单位(默认)。如何使我的游戏对象在特定方向(Pacman)上加速移动,直到输入改变其方向,或者如果它与一个deadend墙碰撞并转向'对'?机芯需要与瓷砖的中间对齐。下面给出了玩家对象的等级的示例。绿色对象是玩家,红色圆柱是目标,图片中的场景是没有按下输入按钮时,允许玩家对象自动左转。

Screenshot

1 个答案:

答案 0 :(得分:0)

@Savlon。 是的,我做到了。目前,我只能在按下按键时移动,然后如果瓷砖可以步行则检查该特定方向。如果是的话,就会采取行动。只有在我按住键的情况下才会发生这种情况。以下是相同的代码。

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;
    }
}