using System; using System.Collections.Generic; using UnityEngine; namespace Code.Example.Race.Racer { public abstract class CarControllerRB : MonoBehaviour { public enum ControlMode { Keyboard, Buttons }; public enum Axel { Front, Rear } [Serializable] public struct Wheel { public GameObject wheelModel; public WheelCollider wheelCollider; public GameObject wheelEffectObj; public ParticleSystem smokeParticle; public Axel axel; } public ControlMode control; public Vector3 _centerOfMass; public List wheels; protected Rigidbody CarRb; protected Vector2 SteeringInput = Vector2.zero; public abstract void SetSteering(Vector2 steeringLinear); [SerializeField] protected Racer racer; // private CarLights carLights; private void Start() { CarRb = GetComponent(); CarRb.centerOfMass = _centerOfMass; // carLights = GetComponent(); } protected virtual void Update() { // GetInputs(); AnimateWheels(); // WheelEffects(); } private void FixedUpdate() { Move(); Steer(); Brake(); } // public void MoveInput(float input) // { // moveInput = input; // } // // public void SteerInput(float input) // { // steerInput = input; // } void GetInputs() { // if (control == ControlMode.Keyboard) // { // moveInput = Input.GetAxis("Vertical"); // steerInput = Input.GetAxis("Horizontal"); // } } private void Move() { foreach (var wheel in wheels) { wheel.wheelCollider.motorTorque = SteeringInput.y * 600 * racer.Data.maxAcceleration * Time.fixedDeltaTime; // wheel.wheelCollider.motorTorque = _playerCarInput.MovementInput.y * 600 * _playerRacer.Data.maxAcceleration * Time.fixedDeltaTime; } } private void Steer() { foreach (var wheel in wheels) { if (wheel.axel != Axel.Front) continue; var steerAngle = SteeringInput.x * racer.Data.turnSensitivity * racer.Data.maxSteerAngle; // var _steerAngle = _playerCarInput.MovementInput.x * _playerRacer.Data.turnSensitivity * _playerRacer.Data.maxSteerAngle; wheel.wheelCollider.steerAngle = steerAngle; // wheel.wheelCollider.steerAngle = Mathf.Lerp(wheel.wheelCollider.steerAngle, _steerAngle, 0.6f); } } protected virtual void Brake() { //|| _playerCarInput.MovementInput.y == 0 // if (_playerCarInput.Brake ) // { // foreach (var wheel in wheels) // { // wheel.wheelCollider.brakeTorque = 300 * _racer.Data.brakeAcceleration * Time.fixedDeltaTime; // } // // // carLights.isBackLightOn = true; // // carLights.OperateBackLights(); // } // else // { // foreach (var wheel in wheels) // { // wheel.wheelCollider.brakeTorque = 0; // } // // // carLights.isBackLightOn = false; // // carLights.OperateBackLights(); // } } private void AnimateWheels() { foreach (var wheel in wheels) { Quaternion rot; Vector3 pos; wheel.wheelCollider.GetWorldPose(out pos, out rot); wheel.wheelModel.transform.position = pos; wheel.wheelModel.transform.rotation = rot; } } private void WheelEffects() { foreach (var wheel in wheels) { //var dirtParticleMainSettings = wheel.smokeParticle.main; if (wheel.axel == Axel.Rear && wheel.wheelCollider.isGrounded && CarRb.linearVelocity.magnitude >= 10.0f) { // wheel.wheelEffectObj.GetComponentInChildren().emitting = true; // wheel.smokeParticle.Emit(1); } else { // wheel.wheelEffectObj.GetComponentInChildren().emitting = false; } } } } }