public bool isBehindABlock(GameObject gameObject) { bool uIsPlayer; foreach (Collider collider in UColliders) { if (collider.transform.tag == "Cube" || CheckIPlayer.isPlayer(collider, out uIsPlayer)) { return(true); } else { return(false); } } return(false); }
IEnumerator MoveBack() { isMoving = true; canMove = true; startPos = transform.position; endPos = new Vector3(startPos.x, transform.position.y, startPos.z - movement); finalRot = Quaternion.Euler(-90, 0, 0); bool isPlayer = false; foreach (Collider collider in BColliders) { if (collider.transform.tag == "Cube" || CheckIPlayer.isPlayer(collider.GetComponent <PlayerMovement>(), out isPlayer)) { if (isAgainstWall("back", gameObject)) { Debug.Log(gameObject.ToString() + " is against a wall"); isMoving = false; canMove = false; yield break; } if (isPlayer) { if (isAgainstWall("back", collider.transform.gameObject)) { endPos.y++; } } else { endPos.y++; } if (isBehindABlock(gameObject) && collider.transform.tag == "Cube") { endPos.y--; isMoving = false; Debug.Log(gameObject.ToString() + " is behind a block"); yield break; } } else { if (collider.transform.tag == "WallCube") { if (collider.transform.position.z - transform.position.z == -1) { isMoving = false; yield break; } else { endPos.z++; } } } } if (transform.tag == "YellowCube") { Collider[] BColliders2 = Physics.OverlapSphere(new Vector3(transform.position.x, transform.position.y, transform.position.z - 1), 0.2f); foreach (Collider YCollider in BColliders2) { if (YCollider.transform.tag == "Player") { if (YCollider.transform.position.y == transform.position.y) { endPos.y++; } } if (YCollider.transform.tag == "WallCube") { isMoving = false; yield break; } } } while (endPos != transform.position) { transform.rotation = Quaternion.Lerp(Quaternion.identity, finalRot, t); transform.position = Vector3.Lerp(startPos, endPos, t); t += Time.deltaTime * speed; yield return(null); } canMove = true; isMoving = false; t = 0; }