/// <summary> /// Reinit this vehicle. Reset to default state. /// Should be called when player restarts /// </summary> /// <param name="accelerate">true, if start speed of this vehicle must be 0</param> public void Reinit(bool accelerate) { playerRigidbody.isKinematic = true; // default values currentSpeed = accelerate ? 0 : DefaultSpeed; currentSideSpeed = accelerate ? 0 : DefaultSideSpeed; targetSpeed = currentSpeed; targetSideSpeed = currentSideSpeed; TravelledDistance = 0; OnDistanceChange?.Invoke(TravelledDistance); Health = MaxHealth; OnVehicleHealthChange?.Invoke(Health); // stop and remove existing particles engineSmoke.Stop(); engineFire.Stop(); engineSmoke.Clear(true); engineFire.Clear(true); SteeringWheel.Restart(); OnVehicleStart?.Invoke(); }
void CheckHorizontalDistance() { currentDistance = Mathf.Max(0, (transform.position.x - targetPlayer.position.x)); float percent = currentDistance / transform.position.x; if (incrementalDistancePercent) { percent = 1 - percent; } OnDistanceChange?.Invoke(currentDistance, percent); }
void FixedUpdate() { // process brake / acceleration if (currentSpeed != targetSpeed || currentSideSpeed != targetSideSpeed) { if (Mathf.Abs(currentSpeed - targetSpeed) > SpeedEpsilon || Mathf.Abs(currentSideSpeed - targetSideSpeed) > SpeedEpsilon) { float time = currentSpeed < targetSpeed ? AccelerationTime : BrakeTime; currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, Time.fixedDeltaTime / time); currentSideSpeed = Mathf.Lerp(currentSideSpeed, targetSideSpeed, Time.fixedDeltaTime / time); } else { currentSpeed = targetSpeed; currentSideSpeed = targetSideSpeed; } } // ignore if too small if (currentSpeed < SpeedEpsilon && currentSideSpeed < SpeedEpsilon) { return; } // get data from steering wheel float steering = SteeringWheel.Steering; // change position of the vehicle Vector3 newPosition = playerRigidbody.position; float forwardDistance = currentSpeed * Time.fixedDeltaTime; newPosition += playerTransform.forward * forwardDistance; if (steering > SteeringEpsilon || steering < -SteeringEpsilon) { newPosition += playerTransform.right * steering * currentSideSpeed * Time.fixedDeltaTime; var background = GameController.Instance.Background; if (background != null) { Vector2 backgroundBounds = background.GetBlockBounds(newPosition); if (newPosition.x > backgroundBounds[1]) { newPosition.x = backgroundBounds[1]; } else if (newPosition.x < backgroundBounds[0]) { newPosition.x = backgroundBounds[0]; } } } playerRigidbody.MovePosition(newPosition); TravelledDistance += forwardDistance; if (Mathf.Abs(TravelledDistance - prevUpdatedDistance) > DistanceUpdateEpsilon) { OnDistanceChange?.Invoke(TravelledDistance); prevUpdatedDistance = TravelledDistance; } }