void addToQueue() { GameObject chasers = GameObject.Find("Chasers"); ChasersController chasersController = chasers.GetComponent <ChasersController>(); if (turnFlag == 1) { if (chasersController.getSize() > 0) { chasersController.Dequeue(); } chasersController.Enqueue("Left"); savedDir = dir; } if (turnFlag == 2) { if (chasersController.getSize() > 0) { chasersController.Dequeue(); } chasersController.Enqueue("Right"); savedDir = dir; } }
// Update is called once per frame void Update() { GameObject chasers = GameObject.Find("Chasers"); ChasersController chasersController = chasers.GetComponent <ChasersController>(); CheckEndTile(); Inputs(); pos = player.transform.position; ballTileX = Mathf.RoundToInt(pos.x / 10); ballTileZ = Mathf.RoundToInt(pos.z / 10); if (ballTileZ > -1 && ballTileZ < layout.Length && ballTileX > -1 && ballTileX < layout[ballTileZ].Length) { if (layout[ballTileZ][ballTileX].CompareTo('*') == 0 || layout[ballTileZ][ballTileX].CompareTo('F') == 0) { onTile = true; } else { onTile = false; } } else { onTile = false; } if (speed < maxspeed) { speed = speed + accel * Time.deltaTime; if (speed > 30) { speed = 30; } } TileMidX = ballTileX * 10; TileMidZ = ballTileZ * 10; newpos = pos + dirV[dir] * speed * Time.deltaTime; if ((dir == 0 && newpos.z > TileMidZ) || (dir == 1 && newpos.x > TileMidX) || (dir == 2 && newpos.z < TileMidZ) || (dir == 3 && newpos.x < TileMidX)) //If past middle of tile { if (SavedTileX != ballTileX || SavedTileZ != ballTileZ) { SavedTileX = ballTileX; SavedTileZ = ballTileZ; chasersController.Enqueue("Forward"); } } //Actual Movement if (gameObject.CompareTag("Player")) // 0 = No Input { if (Input.GetButtonDown("Left")) // 1 = Left { if (InSecondHalfOfTile()) { speed = (speed / 4); } else { dir = (dir + 3) % 4; if (!OutOfBounds() && !NextEmpty()) { turnFlag = 1; counter = 0; addToQueue(); flagX = ballTileX; flagZ = ballTileZ; } else { speed = (speed / 4); } dir = (dir + 5) % 4; } } if (Input.GetButtonDown("Right")) // 2 = Right { if (InSecondHalfOfTile()) { speed = (speed / 4); } else { dir = (dir + 5) % 4; if (!OutOfBounds() && !NextEmpty()) { turnFlag = 2; counter = 0; addToQueue(); flagX = ballTileX; flagZ = ballTileZ; } else { speed = (speed / 4); } dir = (dir + 3) % 4; } } if (turnFlag != 0 && (flagX != ballTileX || flagZ != ballTileZ)) { turnFlag = 0; } else if (!OutOfBounds() && !NextEmpty()) { if ((dir == 0 || dir == 2) && turnFlag != 0 && Mathf.RoundToInt(newpos.z * 2) % 20 == 0) { transform.position = new Vector3(newpos.x, 1.5f, (Mathf.RoundToInt(newpos.z) / 2) * 2); if (turnFlag == 1) { dir = (dir + 3) % 4; } if (turnFlag == 2) { dir = (dir + 5) % 4; } turnFlag = 0; } else if ((dir == 1 || dir == 3) && turnFlag != 0 && Mathf.RoundToInt(newpos.x * 2) % 20 == 0) { transform.position = new Vector3((Mathf.RoundToInt(newpos.x) / 2) * 2, 1.5f, newpos.z); if (turnFlag == 1) { dir = (dir + 3) % 4; } if (turnFlag == 2) { dir = (dir + 5) % 4; } turnFlag = 0; } else { transform.position = newpos; } } else { if (dir == 0 || dir == 2) { transform.position = new Vector3(newpos.x, 1.5f, (Mathf.RoundToInt(newpos.z) / 2) * 2); } else { transform.position = new Vector3((Mathf.RoundToInt(newpos.x) / 2) * 2, 1.5f, newpos.z); } if (turnFlag == 1) { dir = (dir + 3) % 4; turnFlag = 0; } else if (turnFlag == 2) { dir = (dir + 5) % 4; turnFlag = 0; } else { speed = (speed / 4); } } } DebugInfo(); }