private static bool movementBlocked(Vector3 currentTile, Vector3 offset, Vector3 currentRampDirection) { if (currentRampDirection == offset) { currentTile += Vector3.up * 2; } GameObject block = SpawnTiles.tileExists(currentTile + offset) ? SpawnTiles.blocks [SpawnTiles.roundVector(currentTile + offset)] : null; RampBehaviour ramp = block == null ? null : block.GetComponent <RampBehaviour> (); return ((!PLAYER_CAN_FALL && !SpawnTiles.tileExists(currentTile + offset + new Vector3(0, -2, 0))) || ( block != null && block.GetComponent <VoidManager> () == null && block.GetComponent <WaterManager> () == null && (ramp == null || (ramp.upSlopeDirection != offset && currentRampDirection != ramp.upSlopeDirection)) )); }
public static void ObjectMove(string axisV, string axisH, Player p) { Vector2 translation = new Vector2(Input.GetAxis(axisV), Input.GetAxis(axisH)); translation = translation.normalized * GetMagnitude(translation) * Time.deltaTime * speed; Vector3 positionOffset = UP * translation.x + RIGHT * translation.y; Vector3 waterOffset = new Vector3(0, 0, 0); Vector3 currentTile = new Vector3( Mathf.Round(p.transform.position.x / 2), Mathf.Round(p.transform.position.y / 2), Mathf.Round(p.transform.position.z / 2)) * 2; RampBehaviour ramp = SpawnTiles.tileExists(currentTile) ? SpawnTiles.blocks [currentTile].GetComponent <RampBehaviour>() : null; Vector3 tileBelow = currentTile + Vector3.down * 2; bool onIce = false; if (ramp == null && SpawnTiles.tileExists(tileBelow)) { ramp = SpawnTiles.blocks [tileBelow].GetComponent <RampBehaviour> (); if (ramp != null) { currentTile = tileBelow; } else { onIce = SpawnTiles.blocks [tileBelow].GetComponent <IceManager> () != null && (p.prevOffset.x != 0 || p.prevOffset.z != 0); } } p.pos = currentTile; if (positionOffset.magnitude > 0) { p.transform.rotation = Quaternion.LookRotation(positionOffset); } if (onIce && p.prevOffset != Vector3.zero) { positionOffset = p.prevOffset.normalized * speed * Time.deltaTime; } Vector3 positionWithinTile = p.transform.position + positionOffset - currentTile; if (!SpawnTiles.tileExists(tileBelow) || (ramp == null && positionWithinTile.y > 0) || SpawnTiles.blocks[tileBelow].GetComponent <VoidManager>() != null || SpawnTiles.blocks[tileBelow].GetComponent <WaterManager>() != null) { positionOffset = Vector3.down * Time.deltaTime * 6; } Vector3 lookVector = p.transform.TransformDirection(Vector3.forward); int normalizedX = lookVector.x > 0 ? 2 : -2; int normalizedZ = lookVector.z > 0 ? 2 : -2; float distX = Mathf.Abs(normalizedX - positionWithinTile.x * 2) / Mathf.Abs(lookVector.x); float distZ = Mathf.Abs(normalizedZ - positionWithinTile.z * 2) / Mathf.Abs(lookVector.z); Vector3 newPos = currentTile + (distX < distZ ? new Vector3(normalizedX, 0, 0) : new Vector3(0, 0, normalizedZ)); p.selector.position = newPos; bool cubeSelector = SpawnTiles.tileExists(p.selector.position); p.selector.GetComponent <Renderer> ().enabled = cubeSelector; p.selectorChild.GetComponent <Renderer> ().enabled = !cubeSelector; bool exceedingBoundaryX = (positionOffset.x > 0 && positionWithinTile.x > RADIUS) || (positionOffset.x < 0 && positionWithinTile.x < -RADIUS); bool exceedingBoundaryZ = (positionOffset.z > 0 && positionWithinTile.z > RADIUS) || (positionOffset.z < 0 && positionWithinTile.z < -RADIUS); bool onEdge = false; Vector3 rampDirection = ramp == null ? Vector3.zero : ramp.upSlopeDirection; if (movementBlocked(currentTile, new Vector3(normalizedX, 0, 0), rampDirection)) { if (exceedingBoundaryX) { positionOffset.x = 0; onEdge = true; } } if (movementBlocked(currentTile, new Vector3(0, 0, normalizedZ), rampDirection)) { if (exceedingBoundaryZ) { positionOffset.z = 0; onEdge = true; } } if (movementBlocked(currentTile, new Vector3(normalizedX, 0, normalizedZ), rampDirection)) { if (exceedingBoundaryX && exceedingBoundaryZ && !onEdge) { positionOffset.x = 0; positionOffset.z = 0; onEdge = true; } } p.prevOffset = onEdge ? Vector3.zero : positionOffset; //acount for water if (SpawnTiles.tileExists(currentTile) && SpawnTiles.blocks [SpawnTiles.roundVector(currentTile)].GetComponent <WaterManager> () != null) { WaterManager w = SpawnTiles.blocks [SpawnTiles.roundVector(currentTile)].GetComponent <WaterManager> (); waterOffset = waterDirections [w.getDirection()] * Time.deltaTime * speed * 1.1f; } float rampOffset = ramp == null ? 0 : mul((positionWithinTile + ramp.upSlopeDirection * 0.5f), ramp.upSlopeDirection).magnitude * 0.5f; Vector3 newPosition = p.transform.position + positionOffset + waterOffset; if (ramp == null) { } else { newPosition.y = currentTile.y + rampOffset; } p.transform.position = newPosition; if (p.transform.position.y < -10) { p.Respawn(); } }