diff --git a/.idea/.idea.RoboLeague/.idea/contentModel.xml b/.idea/.idea.RoboLeague/.idea/contentModel.xml index 74c9b21..e5f9fc7 100644 --- a/.idea/.idea.RoboLeague/.idea/contentModel.xml +++ b/.idea/.idea.RoboLeague/.idea/contentModel.xml @@ -107,6 +107,7 @@ + diff --git a/Assets/Materials/Octane/Transparent Car Body Red.mat b/Assets/Materials/Octane/Transparent Car Body Red.mat index 29be3bd..fb8fc3a 100644 --- a/Assets/Materials/Octane/Transparent Car Body Red.mat +++ b/Assets/Materials/Octane/Transparent Car Body Red.mat @@ -278,7 +278,7 @@ Material: m_Colors: - _BaseColor: {r: 1, g: 1, b: 1, a: 1} - _BaseColorMap_MipInfo: {r: 0, g: 0, b: 0, a: 0} - - _Color: {r: 1, g: 0, b: 0, a: 1} + - _Color: {r: 1, g: 0, b: 0, a: 0.5019608} - _DiffusionProfileAsset: {r: 0, g: 0, b: 0, a: 0} - _DoubleSidedConstants: {r: 1, g: 1, b: -1, a: 0} - _EmissionColor: {r: 1, g: 1, b: 1, a: 1} diff --git a/Assets/Materials/Octane/Transparent Car Body.mat b/Assets/Materials/Octane/Transparent Car Body.mat index 82b0ad4..dfbff95 100644 --- a/Assets/Materials/Octane/Transparent Car Body.mat +++ b/Assets/Materials/Octane/Transparent Car Body.mat @@ -73,5 +73,5 @@ Material: - _UVSec: 0 - _ZWrite: 0 m_Colors: - - _Color: {r: 0.5471698, g: 0.5471698, b: 0.5471698, a: 1} + - _Color: {r: 0.5471698, g: 0.5471698, b: 0.5471698, a: 0.5019608} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} diff --git a/Assets/Scenes/RoboLeague.unity b/Assets/Scenes/RoboLeague.unity index 9e95d59..c6dfd96 100644 --- a/Assets/Scenes/RoboLeague.unity +++ b/Assets/Scenes/RoboLeague.unity @@ -631,7 +631,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!65 &279860897 BoxCollider: m_ObjectHideFlags: 0 @@ -6706,15 +6706,14 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: steerAngle: 0 - forwardForce: 0 - Fy: {x: 0, y: 0, z: 0} + Fx: 0 wheelFL: 0 wheelFR: 0 wheelRL: 1 wheelRR: 0 wheelMesh: {fileID: 5136046490304972089} - isDrawWheelVelocities: 0 - isDrawWheelDisc: 1 + isDrawWheelVelocities: 1 + isDrawWheelDisc: 0 isDrawForces: 0 --- !u!1 &595860660 GameObject: @@ -32881,8 +32880,8 @@ GameObject: - component: {fileID: 1276522807} - component: {fileID: 1276522806} - component: {fileID: 1276522805} - - component: {fileID: 1276522808} - component: {fileID: 1276522809} + - component: {fileID: 1276522808} m_Layer: 8 m_Name: FL m_TagString: SphereCollider @@ -33070,6 +33069,7 @@ GameObject: - component: {fileID: 1370048098} - component: {fileID: 1370048097} - component: {fileID: 1370048096} + - component: {fileID: 1370048100} - component: {fileID: 1370048094} m_Layer: 8 m_Name: CubeController @@ -33100,7 +33100,6 @@ MonoBehaviour: carState: 0 cogLow: {fileID: 850044590} sceneViewFocusObject: {fileID: 850044589} - cubeParticleSystem: {fileID: 0} --- !u!54 &1370048094 Rigidbody: m_ObjectHideFlags: 0 @@ -33178,15 +33177,12 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 632d9afdf18b1cc448b6bc66407c5311, type: 3} m_Name: m_EditorClassIdentifier: - steerSensivity: 10 turnRadiusCoefficient: 50 currentSteerAngle: 0 driftTime: 3 currentWheelSideFriction: 10 wheelSideFriction: 8 wheelSideFrictionDrift: 0.5 - throttleInput: 0 - steerInput: 0 --- !u!114 &1370048099 MonoBehaviour: m_ObjectHideFlags: 0 @@ -33199,6 +33195,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 21fac67fa63ff654a915e32ce231068c, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &1370048100 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1370048092} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 498cd74abbec0fd40a63836e21610c62, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1381964343 GameObject: m_ObjectHideFlags: 0 @@ -33347,15 +33355,14 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: steerAngle: 0 - forwardForce: 0 - Fy: {x: 0, y: 0, z: 0} + Fx: 0 wheelFL: 0 wheelFR: 1 wheelRL: 0 wheelRR: 0 wheelMesh: {fileID: 5141208236129452049} isDrawWheelVelocities: 0 - isDrawWheelDisc: 1 + isDrawWheelDisc: 0 isDrawForces: 0 --- !u!1 &1454353713 GameObject: @@ -39495,15 +39502,14 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: steerAngle: 0 - forwardForce: 0 - Fy: {x: 0, y: 0, z: 0} + Fx: 0 wheelFL: 0 wheelFR: 0 wheelRL: 0 wheelRR: 1 wheelMesh: {fileID: 5141408967537233807} - isDrawWheelVelocities: 0 - isDrawWheelDisc: 1 + isDrawWheelVelocities: 1 + isDrawWheelDisc: 0 isDrawForces: 0 --- !u!1 &1907187650 GameObject: @@ -39536,7 +39542,17 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 3647dc28d553f054086959c1d51bdadf, type: 3} m_Name: m_EditorClassIdentifier: + throttleInput: 0 + steerInput: 0 + yawInput: 0 + pitchInput: 0 + rollInput: 0 isBoost: 0 + isDrift: 0 + isAirRoll: 0 + isJump: 0 + isJumpUp: 0 + isJumpDown: 0 axisName: Horizontal sensitivityCurve: serializedVersion: 2 @@ -40001,8 +40017,6 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 2055948158} - - component: {fileID: 2055948160} - - component: {fileID: 2055948159} - component: {fileID: 2055948161} m_Layer: 8 m_Name: FL @@ -40025,53 +40039,6 @@ Transform: m_Father: {fileID: 600793202} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!23 &2055948159 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2055948157} - m_Enabled: 0 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 2100000, guid: 9fe49c64afd9961458d42c3ff50d077f, type: 2} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 ---- !u!33 &2055948160 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2055948157} - m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} --- !u!114 &2055948161 MonoBehaviour: m_ObjectHideFlags: 0 @@ -40085,15 +40052,14 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: steerAngle: 0 - forwardForce: 0 - Fy: {x: 0, y: 0, z: 0} + Fx: 0 wheelFL: 1 wheelFR: 0 wheelRL: 0 wheelRR: 0 wheelMesh: {fileID: 5136234058821885859} - isDrawWheelVelocities: 0 - isDrawWheelDisc: 1 + isDrawWheelVelocities: 1 + isDrawWheelDisc: 0 isDrawForces: 0 --- !u!1 &2077504888 GameObject: diff --git a/Assets/Scripts/CarControllers/CubeController/CubeAirControl.cs b/Assets/Scripts/CarControllers/CubeController/CubeAirControl.cs index 8b630d9..885eebe 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeAirControl.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeAirControl.cs @@ -1,18 +1,25 @@ using UnityEngine; +[RequireComponent(typeof(CubeController))] public class CubeAirControl : MonoBehaviour { public bool isUseDamperTorque = true; + + float _inputRoll = 0, _inputPitch = 0, _inputYaw = 0; + Rigidbody _rb; CubeController _controller; - + + #region Torque Coefficients for rotation and drag + const float Tr = 36.07956616966136f; // torque coefficient for roll const float Tp = 12.14599781908070f; // torque coefficient for pitch const float Ty = 8.91962804287785f; // torque coefficient for yaw const float Dr = -4.47166302201591f; // drag coefficient for roll const float Dp = -2.798194258050845f; // drag coefficient for pitch const float Dy = -1.886491900437232f; // drag coefficient for yaw - float _inputRoll = 0, _inputPitch = 0, _inputYaw = 0; + + #endregion void Start() { @@ -22,17 +29,11 @@ void Start() void Update() { - _inputYaw = Input.GetAxis("Horizontal"); - _inputPitch = Input.GetAxis("PitchAxis"); - - if (Input.GetKey(KeyCode.E) || Input.GetButton("B")) - _inputRoll = -1; - else if (Input.GetKey(KeyCode.Q) || Input.GetButton("Y")) - _inputRoll = 1; - else - _inputRoll = 0; + _inputYaw = GameManager.InputManager.yawInput; + _inputPitch = GameManager.InputManager.pitchInput; + _inputRoll = GameManager.InputManager.rollInput; - if (Input.GetButton("LB") || Input.GetKey(KeyCode.LeftShift)) + if (GameManager.InputManager.isAirRoll) { _inputRoll = -_inputYaw; _inputYaw = 0; @@ -41,19 +42,18 @@ void Update() private void FixedUpdate() { - if (_controller.numWheelsSurface < 3) - { - // roll - _rb.AddTorque(Tr * _inputRoll * transform.forward, ForceMode.Acceleration); - if(isUseDamperTorque) _rb.AddTorque(Dr * transform.InverseTransformDirection(_rb.angularVelocity).z * transform.forward, ForceMode.Acceleration); - - // pitch - _rb.AddTorque(Tp * _inputPitch * transform.right, ForceMode.Acceleration); - if(isUseDamperTorque) _rb.AddTorque(transform.right * (Dp * (1 - Mathf.Abs(_inputPitch)) * transform.InverseTransformDirection(_rb.angularVelocity).x), ForceMode.Acceleration); - - //yaw - _rb.AddTorque(Ty * _inputYaw * transform.up, ForceMode.Acceleration); - if(isUseDamperTorque) _rb.AddTorque(transform.up * (Dy * (1 - Mathf.Abs(_inputYaw)) * transform.InverseTransformDirection(_rb.angularVelocity).y), ForceMode.Acceleration); - } + if (_controller.numWheelsSurface >= 3) return; + + // roll + _rb.AddTorque(Tr * _inputRoll * transform.forward, ForceMode.Acceleration); + if(isUseDamperTorque) _rb.AddTorque(Dr * transform.InverseTransformDirection(_rb.angularVelocity).z * transform.forward, ForceMode.Acceleration); + + // pitch + _rb.AddTorque(Tp * _inputPitch * transform.right, ForceMode.Acceleration); + if(isUseDamperTorque) _rb.AddTorque(transform.right * (Dp * (1 - Mathf.Abs(_inputPitch)) * transform.InverseTransformDirection(_rb.angularVelocity).x), ForceMode.Acceleration); + + //yaw + _rb.AddTorque(Ty * _inputYaw * transform.up, ForceMode.Acceleration); + if(isUseDamperTorque) _rb.AddTorque(transform.up * (Dy * (1 - Mathf.Abs(_inputYaw)) * transform.InverseTransformDirection(_rb.angularVelocity).y), ForceMode.Acceleration); } -} +} \ No newline at end of file diff --git a/Assets/Scripts/CarControllers/CubeController/CubeBoosting.cs b/Assets/Scripts/CarControllers/CubeController/CubeBoosting.cs new file mode 100644 index 0000000..a4d6fef --- /dev/null +++ b/Assets/Scripts/CarControllers/CubeController/CubeBoosting.cs @@ -0,0 +1,33 @@ +using UnityEngine; + +[RequireComponent(typeof(CubeController))] +public class CubeBoosting : MonoBehaviour +{ + const float BoostForce = 991 / 100; + + CubeController _c; + Rigidbody _rb; + + private void Start() + { + _c = GetComponent(); + _rb = GetComponentInParent(); + + // Activate ParticleSystems GameObject + if (Resources.FindObjectsOfTypeAll()[0] != null) + Resources.FindObjectsOfTypeAll()[0].gameObject.SetActive(true); + } + + void FixedUpdate() + { + Boosting(); + } + + void Boosting() + { + if (GameManager.InputManager.isBoost && _c.forwardSpeed < CubeController.MaxSpeedBoost) + { + _rb.AddForce(BoostForce * transform.forward, ForceMode.Acceleration); + } + } +} diff --git a/Assets/Scripts/CarControllers/CubeController/CubeBoosting.cs.meta b/Assets/Scripts/CarControllers/CubeController/CubeBoosting.cs.meta new file mode 100644 index 0000000..5286d38 --- /dev/null +++ b/Assets/Scripts/CarControllers/CubeController/CubeBoosting.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 498cd74abbec0fd40a63836e21610c62 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/CarControllers/CubeController/CubeController.cs b/Assets/Scripts/CarControllers/CubeController/CubeController.cs index 303f342..22a6949 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeController.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeController.cs @@ -1,8 +1,7 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEditor; +using UnityEditor; using UnityEngine; +[RequireComponent(typeof(Rigidbody))] public class CubeController : MonoBehaviour { [Header("Car State")] @@ -12,6 +11,17 @@ public class CubeController : MonoBehaviour public int numWheelsSurface; public bool isBodySurface; public CarStates carState; + + [Header("Other")] + public Transform cogLow; + public GameObject sceneViewFocusObject; + + public const float MaxSpeedBoost = 2300 / 100; + + Rigidbody _rb; + GUIStyle _style; + CubeSphereCollider[] _sphereColliders; + public enum CarStates { AllWheelsGround, @@ -21,19 +31,7 @@ public enum CarStates BodySideGround, BodyGroundDead } - - [Header("Other")] - public Transform cogLow; - - const float MaxSpeedBoost = 2300 / 100; - - Rigidbody _rb; - private CubeGroundControl _groundControl; - static readonly GUIStyle Style = new GUIStyle(); - CubeSphereCollider[] _sphereColliders; - public GameObject sceneViewFocusObject; - public CubeParticleSystem cubeParticleSystem; - + void Start() { _rb = GetComponent(); @@ -41,54 +39,32 @@ void Start() _rb.maxAngularVelocity = 5.5f; _sphereColliders = GetComponentsInChildren(); - _groundControl = GetComponent(); - Style.normal.textColor = Color.red; - Style.fontSize = 25; - Style.fontStyle = FontStyle.Bold; + + // GUI stuff + _style = new GUIStyle(); + _style.normal.textColor = Color.red; + _style.fontSize = 25; + _style.fontStyle = FontStyle.Bold; // Lock scene view camera to the car Selection.activeGameObject = sceneViewFocusObject; SceneView.lastActiveSceneView.FrameSelected(true); - - // Activate ParticleSystems GameObject - if (Resources.FindObjectsOfTypeAll()[0] != null) - Resources.FindObjectsOfTypeAll()[0].gameObject.SetActive(true); } - - private void Update() - { - SetCarState(); - //TODO: limit _rb.velocity.magnitude to < maxSpeedBoost - } - + void FixedUpdate() { + SetCarState(); UpdateCarVariables(); - Boosting(); - DownForce(); - //SetDrag(); + //TODO: limit _rb.velocity.magnitude to < maxSpeedBoost } private void UpdateCarVariables() { forwardSpeed = Vector3.Dot(_rb.velocity, transform.forward); - //if(Mathf.Abs(forwardSpeed) < 0.1f) forwardSpeed = (float) System.Math.Round(forwardSpeed, 2); forwardSpeedAbs = Mathf.Abs(forwardSpeed); forwardSpeedSign = Mathf.Sign(forwardSpeed); } - - void Boosting() - { - if (GameManager.InputManager.isBoost) - { - if (forwardSpeed < MaxSpeedBoost) - { - _rb.AddForce(transform.forward * 991 / 100, ForceMode.Acceleration); - //_groundControl.throttleInput = 1; - } - } - } void SetCarState() { @@ -123,48 +99,35 @@ void SetCarState() // In the air if (!isBodySurface && numWheelsSurface == 0) - { carState = CarStates.Air; - //rb.centerOfMass = Vector3.zero; - } - + isCanDrive = false || (carState == CubeController.CarStates.AllWheelsSurface || carState == CubeController.CarStates.AllWheelsGround); } - - private void SetDrag() - { - if (carState == CarStates.Air) - { - _rb.drag = 0; - _rb.angularDrag = 0; - } - else - { - _rb.drag = 1; - _rb.angularDrag = 1; - } - } - + void DownForce() { if (carState == CarStates.AllWheelsSurface || carState == CarStates.AllWheelsGround) _rb.AddForce(-transform.up * 5, ForceMode.Acceleration); } + # region GUI + + void OnGUI() + { + GUI.Label(new Rect(30.0f, 20.0f, 150, 130), $"{forwardSpeed:F2} m/s {forwardSpeed * 100:F0} uu/s", _style); + //GUI.Label(new Rect(30.0f, 40.0f, 150, 130), string.Format("turnRadius: {0:F2} m curvature: {1:F4}", turnRadius, curvature), style); + GUI.Label(new Rect(30.0f, 60.0f, 150, 130), $"car state: {carState.ToString()}", _style); + } + private void OnDrawGizmos() { - Gizmos.DrawSphere(cogLow.transform.position, 0.03f); - if (_rb == null) return; // Draw CG + if (_rb == null) return; Gizmos.color = Color.black; Gizmos.DrawSphere(_rb.transform.TransformPoint(_rb.centerOfMass), 0.03f); } - void OnGUI() - { - GUI.Label(new Rect(30.0f, 20.0f, 150, 130), $"{forwardSpeed:F2} m/s {forwardSpeed * 100:F0} uu/s", Style); - //GUI.Label(new Rect(30.0f, 40.0f, 150, 130), string.Format("turnRadius: {0:F2} m curvature: {1:F4}", turnRadius, curvature), style); - GUI.Label(new Rect(30.0f, 60.0f, 150, 130), $"car state: {carState.ToString()}", Style); - } + + #endregion #region Utils diff --git a/Assets/Scripts/CarControllers/CubeController/CubeDebugController.cs b/Assets/Scripts/CarControllers/CubeController/CubeDebugController.cs index 318d1b5..e21ce5b 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeDebugController.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeDebugController.cs @@ -57,15 +57,4 @@ void IsDrawForces() w.isDrawForces = !w.isDrawForces; } } - - void Start() - { - - } - - // Update is called once per frame - void Update() - { - - } } diff --git a/Assets/Scripts/CarControllers/CubeController/CubeGroundControl.cs b/Assets/Scripts/CarControllers/CubeController/CubeGroundControl.cs index 5a1eca6..af7025a 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeGroundControl.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeGroundControl.cs @@ -1,31 +1,21 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEditor; -using UnityEngine; +using UnityEngine; +[RequireComponent(typeof(CubeController))] public class CubeGroundControl : MonoBehaviour { [Header("Steering")] - public float steerSensivity; - public float turnRadiusCoefficient = 40; + public float turnRadiusCoefficient = 50; public float currentSteerAngle; [Header("Drift")] - public float driftTime; + public float driftTime = 3; public float currentWheelSideFriction = 10; public float wheelSideFriction = 8; public float wheelSideFrictionDrift = 0.5f; - CubeWheel[] _wheelArray; - Rigidbody _rb; CubeController _c; - - float naiveRotationForce = 5; - float naiveRotationDampeningForce = -10; - - [HideInInspector] public float throttleInput = 0, steerInput = 0; + CubeWheel[] _wheelArray; void Start() { @@ -36,63 +26,57 @@ void Start() private void Update() { - //TODO: Move input processing to a dedicated input manager static class - // Process Input - { - throttleInput = 0; - if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("RT") > 0) - throttleInput = Mathf.Max(Input.GetAxis("Vertical"), Input.GetAxis("RT")); - else if (Input.GetAxis("Vertical") < 0 || Input.GetAxis("LT") < 0) - throttleInput = Mathf.Min(Input.GetAxis("Vertical"), Input.GetAxis("LT")); - - // Sliding / drifting, lowers the wheel side friction when drifting - if (Input.GetButton("LB") || Input.GetKey(KeyCode.LeftShift)) - currentWheelSideFriction = Mathf.Lerp(currentWheelSideFriction, wheelSideFrictionDrift, - Time.deltaTime * driftTime); - else - currentWheelSideFriction = - Mathf.Lerp(currentWheelSideFriction, wheelSideFriction, Time.deltaTime * driftTime); - } + // Sliding / drifting, lowers the wheel side friction when drifting + var currentDrift = GameManager.InputManager.isDrift ? wheelSideFrictionDrift : wheelSideFriction; + currentWheelSideFriction = Mathf.MoveTowards(currentWheelSideFriction, currentDrift, Time.deltaTime * driftTime); } private void FixedUpdate() { + var throttleInput = GameManager.InputManager.throttleInput; + // Throttle - var Fx = CalculateForwardForce(); + float forwardAcceleration = 0; + if (GameManager.InputManager.isBoost) + forwardAcceleration = GetForwardAcceleration(_c.forwardSpeedAbs); + else + forwardAcceleration = throttleInput * GetForwardAcceleration(_c.forwardSpeedAbs); + // Braking - if(_c.forwardSpeedSign != Mathf.Sign(throttleInput)) - Fx += -1 * _c.forwardSpeedSign * 35; - + if(_c.forwardSpeedSign != Mathf.Sign(throttleInput) && throttleInput != 0) + forwardAcceleration += -1 * _c.forwardSpeedSign * 35; + // Steering currentSteerAngle = CalculateSteerAngle(); // Apply forces and steer angle to each wheel - foreach (var w in _wheelArray) + foreach (var wheel in _wheelArray) { - if (_c.isCanDrive) w.forwardForce = Fx / 4; + //TODO: Func. call like this below OR Wheel class fetches data from this class? + // Also probably should be an interface to a concrete implementation. Same for the NaiveGroundControl below. + if (_c.isCanDrive) + wheel.ApplyForwardForce(forwardAcceleration / 4); - if (w.wheelFL || w.wheelFR) - w.steerAngle = currentSteerAngle; + if (wheel.wheelFL || wheel.wheelFR) + wheel.RotateWheels(currentSteerAngle); } } - private float CalculateSteerAngle() + private float CalculateForwardForce(float input, float speed) { - steerInput = Mathf.MoveTowards(steerInput, Input.GetAxis("Horizontal"), Time.fixedDeltaTime * steerSensivity); - return (1 / GetTurnRadius(_c.forwardSpeed)) * turnRadiusCoefficient * steerInput; + return input * GetForwardAcceleration(_c.forwardSpeedAbs); } - - private float CalculateForwardForce() + + private float CalculateSteerAngle() { - if (GameManager.InputManager.isBoost) - return 1 * GetThrottleSpeed(_c.forwardSpeedAbs); - - return throttleInput * GetThrottleSpeed(_c.forwardSpeedAbs); + var curvature = 1 / GetTurnRadius(_c.forwardSpeed); + return GameManager.InputManager.steerInput * curvature * turnRadiusCoefficient; } - - float GetThrottleSpeed(float speed) + + static float GetForwardAcceleration(float speed) { + // Replicates acceleration curve from RL, depends on current car forward velocity speed = Mathf.Abs(speed); float throttle = 0; @@ -105,8 +89,8 @@ float GetThrottleSpeed(float speed) return throttle; } - - float GetTurnRadius(float speed) + + static float GetTurnRadius(float speed) { float forwardSpeed = Mathf.Abs(speed); float turnRadius = 0; @@ -129,6 +113,8 @@ float GetTurnRadius(float speed) return turnRadius; } + float _naiveRotationForce = 5; + float _naiveRotationDampeningForce = -10; private void NaiveGroundControl() { if (_c.carState != CubeController.CarStates.AllWheelsSurface && @@ -136,7 +122,7 @@ private void NaiveGroundControl() // Throttle var throttleInput = Input.GetAxis("Vertical"); - float Fx = throttleInput * GetThrottleSpeed(_c.forwardSpeedAbs); + float Fx = throttleInput * GetForwardAcceleration(_c.forwardSpeedAbs); _rb.AddForceAtPosition(Fx * transform.forward, _rb.transform.TransformPoint(_rb.centerOfMass), ForceMode.Acceleration); @@ -147,9 +133,8 @@ private void NaiveGroundControl() //if (throttleInput == 0) _rb.AddForce(transform.forward * (5.25f * -Mathf.Sign(forwardSpeed)), ForceMode.Acceleration); // Steering - _rb.AddTorque(transform.up * (Input.GetAxis("Horizontal") * naiveRotationForce), ForceMode.Acceleration); - _rb.AddTorque(transform.up * (naiveRotationDampeningForce * (1 - Mathf.Abs(Input.GetAxis("Horizontal"))) * + _rb.AddTorque(transform.up * (Input.GetAxis("Horizontal") * _naiveRotationForce), ForceMode.Acceleration); + _rb.AddTorque(transform.up * (_naiveRotationDampeningForce * (1 - Mathf.Abs(Input.GetAxis("Horizontal"))) * transform.InverseTransformDirection(_rb.angularVelocity).y), ForceMode.Acceleration); } -} - +} \ No newline at end of file diff --git a/Assets/Scripts/CarControllers/CubeController/CubeJumping.cs b/Assets/Scripts/CarControllers/CubeController/CubeJumping.cs index 3e045c5..f8ed6c7 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeJumping.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeJumping.cs @@ -1,5 +1,6 @@ using UnityEngine; +[RequireComponent(typeof(CubeController))] public class CubeJumping : MonoBehaviour { [Header("Forces")] @@ -8,14 +9,10 @@ public class CubeJumping : MonoBehaviour public int upForce = 3; public int upTorque = 50; - float jumpTimer = 0; - bool _isMouseButton1 = false; - bool _isMouseButtonDown1 = false; - bool _isMouseButtonUp1 = false; - - bool isCanFirstJump = false; - bool isJumping = false; - bool isCanKeepJumping = false; + float _jumpTimer = 0; + bool _isCanFirstJump = false; + bool _isJumping = false; + bool _isCanKeepJumping = false; Rigidbody _rb; CubeController _controller; @@ -26,70 +23,58 @@ void Start() _controller = GetComponent(); } - void Update() + private void FixedUpdate() { - if (Input.GetMouseButton(1) || Input.GetButton("A")) - _isMouseButton1 = true; - - if (Input.GetMouseButtonDown(1) || Input.GetButtonDown("A")) - _isMouseButtonDown1 = true; - - if (Input.GetMouseButtonUp(1) || Input.GetButtonUp("A")) - _isMouseButtonUp1 = true; - + Jump(); JumpBackToTheFeet(); } - private void FixedUpdate() + private void Jump() { // Reset jump flags when landed if (_controller.isAllWheelsSurface) { // Need a timer, otherwise while jumping we are setting isJumping flag to false right on the next frame - if (jumpTimer >= 0.1f) - isJumping = false; + if (_jumpTimer >= 0.1f) + _isJumping = false; - jumpTimer = 0; - isCanFirstJump = true; + _jumpTimer = 0; + _isCanFirstJump = true; //if (isDebug) controller.ClearConsole(); } // Cant start jumping while in the air else if (!_controller.isAllWheelsSurface) - isCanFirstJump = false; + _isCanFirstJump = false; // Do initial jump impulse only once - if (_isMouseButtonDown1 && isCanFirstJump) + if (GameManager.InputManager.isJumpDown && _isCanFirstJump) { - isCanKeepJumping = true; - isCanFirstJump = false; - isJumping = true; + _isCanKeepJumping = true; + _isCanFirstJump = false; + _isJumping = true; _rb.AddForce(transform.up * 292 / 100 * jumpForceMultiplier, ForceMode.VelocityChange); - jumpTimer += Time.fixedDeltaTime; + _jumpTimer += Time.fixedDeltaTime; } // If jump button was released we can't start jumping again mid air - if(_isMouseButtonUp1) - isCanKeepJumping = false; + if (GameManager.InputManager.isJumpUp) + _isCanKeepJumping = false; - // Keep jumping if jump button pressed - if (_isMouseButton1 && isJumping && isCanKeepJumping && jumpTimer <= 0.2f ) + // Keep jumping if the jump button is being pressed + if (GameManager.InputManager.isJump && _isJumping && _isCanKeepJumping && _jumpTimer <= 0.2f) { _rb.AddForce(transform.up * 1458f / 100 * jumpForceMultiplier, ForceMode.Acceleration); - jumpTimer += Time.fixedDeltaTime; + _jumpTimer += Time.fixedDeltaTime; } - - _isMouseButton1 = false; - _isMouseButtonDown1 = false; - _isMouseButtonUp1 = false; } - + //Auto jump and rotate when the car is on the roof void JumpBackToTheFeet() { if (_controller.carState != CubeController.CarStates.BodyGroundDead) return; - if (_isMouseButtonDown1 || Input.GetButtonDown("A")) + if (GameManager.InputManager.isJumpDown || Input.GetButtonDown("A")) { _rb.AddForce(Vector3.up * upForce, ForceMode.VelocityChange); _rb.AddTorque(transform.forward * upTorque, ForceMode.VelocityChange); diff --git a/Assets/Scripts/CarControllers/CubeController/CubeParticleSystem.cs b/Assets/Scripts/CarControllers/CubeController/CubeParticleSystem.cs index 323599f..60c6ae1 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeParticleSystem.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeParticleSystem.cs @@ -20,6 +20,7 @@ void Start() _trails[0].time = _trails[1].time = 0; firePs.SetActive(false); + windPs.transform.position += new Vector3(0, 0, 10); } void Update() diff --git a/Assets/Scripts/CarControllers/CubeController/CubeSphereCollider.cs b/Assets/Scripts/CarControllers/CubeController/CubeSphereCollider.cs index ad5d2d4..152e7e4 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeSphereCollider.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeSphereCollider.cs @@ -29,16 +29,15 @@ private void Awake() private void FixedUpdate() { isTouchingSurface = IsSurfaceContact(); - - ApplyStickyForces(); + //ApplyStickyForces(); } private void ApplyStickyForces() { if (isTouchingSurface) { - var StickyForce = -(325 / 100) / 4 * _contactNormal; - _rb.AddForceAtPosition(StickyForce, _contactPoint, ForceMode.Acceleration); + var stickyForce = -(325 / 100) / 4 * _contactNormal; + _rb.AddForceAtPosition(stickyForce, _contactPoint, ForceMode.Acceleration); } } @@ -50,8 +49,6 @@ bool IsSurfaceContact() _contactNormal = hit.normal; return false || isHit; } - - private void OnTriggerStay(Collider other) { isTouchingSurface = true; diff --git a/Assets/Scripts/CarControllers/CubeController/CubeWheel.cs b/Assets/Scripts/CarControllers/CubeController/CubeWheel.cs index ef23866..1eeb96b 100644 --- a/Assets/Scripts/CarControllers/CubeController/CubeWheel.cs +++ b/Assets/Scripts/CarControllers/CubeController/CubeWheel.cs @@ -7,28 +7,25 @@ public class CubeWheel : MonoBehaviour { - public float steerAngle; - public float forwardForce; - - public Vector3 Fy; + // public float steerAngle; + // public float Fx; + float Fy; public bool wheelFL, wheelFR, wheelRL, wheelRR; - + public Transform wheelMesh; + private float _meshRevolutionAngle; Rigidbody _rb; CubeController _c; CubeGroundControl _groundControl; - float _wheelRadius; - Vector3 _wheelVelocity = Vector3.zero; - Vector3 _lastWheelVelocity = Vector3.zero; - Vector3 _wheelAcceleration = Vector3.zero; - float _wheelForwardVelocity; - float _wheelLateralVelocity; - Vector3 _wheelContactPoint; - Vector3 _lateralForcePosition; - + float _wheelRadius, _wheelForwardVelocity, _wheelLateralVelocity; + Vector3 _wheelVelocity, _lastWheelVelocity, _wheelAcceleration, _wheelContactPoint, _lateralForcePosition = Vector3.zero; + + const float AutoBrakeAcceleration = 5.25f; + + // Debug Draw [Title("Debug Draw Options")] [Button("@\"Draw Wheel Velocities: \" + _isDrawWheelVelocities", ButtonSizes.Large)] void IsDrawWheelVelocities() @@ -46,58 +43,62 @@ void Start() _wheelRadius = transform.localScale.z / 2; } - private float _angle; + void Update() { - transform.localRotation = Quaternion.Euler(Vector3.up * steerAngle); - // Update mesh rotations of the wheel if (wheelMesh) { //wheelMesh.transform.position = transform.position; wheelMesh.transform.localRotation = transform.localRotation; - _angle += (Time.deltaTime * transform.InverseTransformDirection(_wheelVelocity).z) / (2 * Mathf.PI * _wheelRadius) * 360; - //transform.Rotate(new Vector3(0, 1, 0), wheel.steerAngle - transform.localEulerAngles.y); - wheelMesh.transform.Rotate(Vector3.right, _angle * 1.3f); + _meshRevolutionAngle += (Time.deltaTime * transform.InverseTransformDirection(_wheelVelocity).z) / + (2 * Mathf.PI * _wheelRadius) * 360; + wheelMesh.transform.Rotate(Vector3.right, _meshRevolutionAngle * 1.3f); + //transform.Rotate(new Vector3(0, 1, 0), steerAngle - transform.localEulerAngles.y); } } - + + public void RotateWheels(float steerAngle) + { + transform.localRotation = Quaternion.Euler(Vector3.up * steerAngle); + } + private void FixedUpdate() { UpdateWheelState(); - if(!_c.isCanDrive) return; - - ApplyForwardForce(); - + if (!_c.isCanDrive) return; + //ApplyForwardForce(); ApplyLateralForce(); + SimulateDrag(); } - + + public void ApplyForwardForce(float force) + { + _rb.AddForce(force * transform.forward, ForceMode.Acceleration); + + // Kill velocity to 0 for small car velocities + if (force == 0 && _c.forwardSpeedAbs < 0.1) + _rb.velocity = new Vector3(_rb.velocity.x, _rb.velocity.y, 0); + } + private void ApplyLateralForce() { - // Fy Lateral Force - Fy = _wheelLateralVelocity * _groundControl.currentWheelSideFriction * transform.right; + Fy = _wheelLateralVelocity * _groundControl.currentWheelSideFriction ; _lateralForcePosition = transform.localPosition; _lateralForcePosition.y = _c.cogLow.localPosition.y; _lateralForcePosition = _c.transform.TransformPoint(_lateralForcePosition); - _rb.AddForceAtPosition(-Fy, _lateralForcePosition, ForceMode.Acceleration); + _rb.AddForceAtPosition(-Fy * transform.right, _lateralForcePosition, ForceMode.Acceleration); } - - private void ApplyForwardForce() + + private void SimulateDrag() { - // Fx Forward Force - _rb.AddForce(forwardForce * transform.forward, ForceMode.Acceleration); - - //Apply auto braking if no input, simulates drag - if (_c.forwardSpeedAbs >= 0.1) - { - var dragForce = 5.25f / 4 * _c.forwardSpeedSign * (1 - Mathf.Abs(_groundControl.throttleInput)); - _rb.AddForce(-transform.forward * dragForce, ForceMode.Acceleration); - } - - // Kill velocity to 0 for small car velocities - if (forwardForce == 0 && _c.forwardSpeedAbs < 0.1) - _rb.velocity = new Vector3(_rb.velocity.x, _rb.velocity.y, 0); + //Applies auto braking if no input, simulates air and ground drag + if (!(_c.forwardSpeedAbs >= 0.1)) return; + + //TODO Use constants, also make a separate function + var dragForce = AutoBrakeAcceleration / 4 * _c.forwardSpeedSign * (1 - Mathf.Abs(GameManager.InputManager.throttleInput)); + _rb.AddForce(-dragForce * transform.forward, ForceMode.Acceleration); } private void UpdateWheelState() @@ -144,7 +145,7 @@ private void OnDrawGizmos() if(isDrawForces) { // Draw induced lateral friction Fy - DrawRay(_lateralForcePosition, 0.3f * -Fy, Color.magenta); + DrawRay(_lateralForcePosition, 0.3f * -Fy * transform.right, Color.magenta); // Draw observed forces DrawLocalRay(transform.up, _wheelAcceleration.z, transform.forward, Color.gray); @@ -168,5 +169,4 @@ void DrawLocalRay(Vector3 from, float length, Vector3 dir, Color c) } #endregion -} - +} \ No newline at end of file diff --git a/Assets/Scripts/Managers/InputManager.cs b/Assets/Scripts/Managers/InputManager.cs index d2084ee..cb61493 100644 --- a/Assets/Scripts/Managers/InputManager.cs +++ b/Assets/Scripts/Managers/InputManager.cs @@ -5,10 +5,53 @@ public class InputManager : MonoBehaviour { - public bool isBoost = false; + public float throttleInput, steerInput, yawInput, pitchInput, rollInput; + public bool isBoost, isDrift, isAirRoll; + public bool isJump, isJumpUp, isJumpDown; void Update() { + throttleInput = GetThrottle(); + steerInput = GetSteerInput(); + + yawInput = Input.GetAxis("Horizontal"); + pitchInput = Input.GetAxis("PitchAxis"); + rollInput = GetRollInput(); + + isJump = Input.GetMouseButton(1) || Input.GetButton("A"); + isJumpUp = Input.GetMouseButtonUp(1) || Input.GetButtonUp("A"); + isJumpDown = Input.GetMouseButtonDown(1) || Input.GetButtonDown("A"); + isBoost = Input.GetButton("RB") || Input.GetMouseButton(0); + isDrift = Input.GetButton("LB") || Input.GetKey(KeyCode.LeftShift); + isAirRoll = Input.GetButton("LB") || Input.GetKey(KeyCode.LeftShift); + } + + private static float GetRollInput() + { + var inputRoll = 0; + if (Input.GetKey(KeyCode.E) || Input.GetButton("B")) + inputRoll = -1; + else if (Input.GetKey(KeyCode.Q) || Input.GetButton("Y")) + inputRoll = 1; + + return inputRoll; + } + + static float GetThrottle() + { + float throttle = 0; + if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("RT") > 0) + throttle = Mathf.Max(Input.GetAxis("Vertical"), Input.GetAxis("RT")); + else if (Input.GetAxis("Vertical") < 0 || Input.GetAxis("LT") < 0) + throttle = Mathf.Min(Input.GetAxis("Vertical"), Input.GetAxis("LT")); + + return throttle; + } + + static float GetSteerInput() + { + //return Mathf.MoveTowards(steerInput, Input.GetAxis("Horizontal"), Time.fixedDeltaTime); + return Input.GetAxis("Horizontal"); } public string axisName = "Horizontal"; @@ -37,7 +80,7 @@ public float GetValue() private void OnGUI() { - GUILayout.HorizontalSlider(Input.GetAxis(axisName), -1, 1, GUILayout.Width(200)); - GUILayout.HorizontalSlider(GetValue(), -1, 1, GUILayout.Width(200)); + GUILayout.HorizontalSlider(Input.GetAxis("Horizontal"), -1, 1, GUILayout.Width(200)); + GUILayout.HorizontalSlider(steerInput, -1, 1, GUILayout.Width(200)); } } diff --git a/Assets/todo.txt b/Assets/todo.txt index ce9ca4b..85f323c 100644 --- a/Assets/todo.txt +++ b/Assets/todo.txt @@ -13,9 +13,8 @@ Features TODO: Problems: - lateral speed goes behind the wheels - Camera fixes (maybe use Cinemamachine, camera as in Kart game) - - Aerial CoM + - Aerial CoM (?) - Breakdancing (sphere collider touches the ground but not the raycast, enables aircontrol on the ground) - - No fake ground drag when upside down - No fake friction when upside down on the ground Low priority: