void Start() { rbody = GetComponent <Rigidbody>(); tilePosition = GamePlay.grid.GetTileLocation(transform.position); if (!GamePlay.grid.CheckWithinBounds(tilePosition)) { enabled = false; return; } var cellObj = GamePlay.grid.GetCellData(GamePlay.grid.GetTileLocation(transform.position)).cellObject; if (cellObj != null) { currentRail = cellObj.GetComponent <RailConfiguration>(); transform.forward = currentRail.transform.forward; currentDirection = GridController.GetDirectionFromRotationInt(GamePlay.grid.GetCellData(GamePlay.grid.GetTileLocation(transform.position)).rotations); } }
void Update() { if (currentRail == null) { enabled = false; rbody.isKinematic = false; rbody.AddForce(tileVelocity + Vector3.up * 5.0f, ForceMode.Impulse); return; } transform.position = GamePlay.grid.GetInstantiationLocation(tilePosition) + Vector3.up * heightOffset; railProgress += railSpeed * Time.deltaTime; while (railProgress >= 1.0f) { // get next tile var thing = currentDirection; foreach (var route in currentRail.connections) { if (GridController.GetReciprocalConnection(route.Key) == thing) { continue; } thing = route.Key; break; } currentRail = currentRail.connections[thing]; if (currentRail == null) { return; } prevTilePosition = tilePosition; tilePosition = currentRail.GetTilePosition(); transform.forward = currentRail.transform.forward; currentDirection = thing; railProgress -= 1.0f; } }