/// <summary> /// Connect wagon to train /// </summary> /// <param name="carCoupler">Current wagon coupler (front or back)</param> /// <param name="otherCarCoupler">Other wagon coupler</param> public void Connect(TrainCarCoupler carCoupler, TrainCarCoupler otherCarCoupler, bool playSFX) { if (coupling == WagonCoupling.Enabled) { if (otherCarCoupler.IsLocomotive) { _locomotive = otherCarCoupler.Locomotive; _reverseAcceleration = (carCoupler.IsBackJoint == otherCarCoupler.IsBackJoint); } else if (otherCarCoupler.IsWagon) { if (!otherCarCoupler.Wagon.IsConected) { return; } _locomotive = otherCarCoupler.Wagon.Locomotive; _reverseAcceleration = (carCoupler.IsBackJoint != otherCarCoupler.IsBackJoint) ? otherCarCoupler.Wagon.ReverseAcceleration : !otherCarCoupler.Wagon.ReverseAcceleration; } TrainPhysics.ConnectTrainCar(_carJoint, otherCarCoupler.GetComponent <Rigidbody>()); _locomotive.wagons.Add(this); _locomotive.UpdateDoorController(); if (playSFX && _sfx.wagonConnectionSFX != null) { _sfx.wagonConnectionSFX.Play(); } } }
/// <summary> /// Fixed update /// </summary> void FixedUpdate() { if (optimized) { _transform.Rotate(_speed, 0f, 0f, Space.Self); } else { TrainPhysics.ApplyBrakes(_rigidbody, _brake, 0f); } }
/// <summary> /// Connect wagons hinges /// </summary> private void ConnectWagons() { for (int i = 0; i < wagons.Count; i++) { if (i == 0) // Connect wagon to locomotive { TrainPhysics.ConnectTrainCar(wagons[i].GetComponent <HingeJoint>(), backJoint); } else // Connect wagon to wagon { TrainPhysics.ConnectTrainCar(wagons[i].GetComponent <HingeJoint>(), wagons[i - 1].backJoint); } wagons[i].Locomotive = this; } }
/// <summary> /// Control speed base on max speed, acceleration and brakes /// </summary> /// <param name="rigidbody"></param> /// <param name="isGrounded"></param> /// <param name="maxSpeedKph"></param> /// <param name="speed_KPH"></param> /// <param name="acceleration"></param> /// <param name="brake"></param> /// <param name="targetVelocityIn"></param> /// <param name="targetVelocityOut"></param> /// <param name="currentSpeedIn"></param> /// <param name="currentSpeedOut"></param> /// <param name="targetSpeedIn"></param> /// <param name="targetSpeedOut"></param> public static void SpeedControl(Rigidbody rigidbody, bool isGrounded, float maxSpeedKph, float speed_KPH, float acceleration, float brake, Vector3 targetVelocityIn, out Vector3 targetVelocityOut, float currentSpeedIn, out float currentSpeedOut, float targetSpeedIn, out float targetSpeedOut) { currentSpeedOut = currentSpeedIn; targetSpeedOut = targetSpeedIn; targetVelocityOut = targetVelocityIn; if (isGrounded) { targetSpeedOut = TrainPhysics.GetTargetSpeed(acceleration, maxSpeedKph); targetSpeedOut = TrainPhysics.ApplyBrakes(rigidbody, brake, targetSpeedOut); currentSpeedOut = TrainPhysics.SoftAcceleration(currentSpeedOut, targetSpeedOut); //Apply velocity if (speed_KPH < maxSpeedKph) { targetVelocityOut = currentSpeedOut == 0f ? Vector3.zero : rigidbody.velocity + (rigidbody.transform.forward * currentSpeedOut); rigidbody.velocity = Vector3.MoveTowards(rigidbody.velocity, targetVelocityOut, Time.deltaTime * GeneralSettings.AccelerationRate); } } }
/// <summary> /// Physics /// </summary> void FixedUpdate() { EnforceAnchorPosition(); UpdateVelocity(); _isGrounded = sensors.leftSensor.grounded || sensors.rightSensor.grounded; _speed = _rigidbody.velocity.magnitude; _wagonAccel = _reverseAcceleration ? (_acceleration * (-1)) : _acceleration; _localVelocity = _transform.InverseTransformDirection(_rigidbody.velocity); TrainPhysics.UpdateWheels(wheelsScripts, _brake, _localVelocity.z); TrainPhysics.SpeedControl(_rigidbody, _isGrounded, _maxSpeedKph, Speed.Convert_MPS_To_KPH(_speed), _wagonAccel, _brake, _targetVelocity, out _targetVelocity, _currentSpeed, out _currentSpeed, _targetSpeed, out _targetSpeed); TrainAudio.PlaySFX(_sfx, Speed.Convert_MPS_To_KPH(_speed), _brake, false, _isGrounded); }
/// <summary> /// Train physics /// </summary> void FixedUpdate() { brake = automaticBrakes ? 1f - Mathf.Abs(acceleration) : brake; IsGrounded = sensors.leftSensor.grounded || sensors.rightSensor.grounded; _onRails = sensors.leftSensor.onRails || sensors.rightSensor.onRails; _speed = _rigidbody.velocity.magnitude; _speed_MPH = Speed.Convert_MPS_To_MPH(_speed); _speed_KPH = Speed.Convert_MPS_To_KPH(_speed); TrainAudio.PlaySFX(_sfx, _speed_KPH, brake, enginesOn, _isGrounded); _localVelocity = _transform.InverseTransformDirection(_rigidbody.velocity); if (!enginesOn) { acceleration = Mathf.MoveTowards(acceleration, 0f, GeneralSettings.DeaccelerationRate * Time.deltaTime); brake = 1f; } TrainPhysics.UpdateWheels(wheelsScripts, brake, _localVelocity.z); TrainPhysics.SpeedControl(_rigidbody, _isGrounded, maxSpeedKph, _speed_KPH, acceleration, brake, _targetVelocity, out _targetVelocity, _currentSpeed, out _currentSpeed, _targetSpeed, out _targetSpeed); }