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