diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 413254d..373cf04 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -177,9 +177,97 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] - m_AddedGameObjects: [] + m_AddedGameObjects: + - targetCorrespondingSourceObject: {fileID: 3861359295483779369, guid: 0a84c5c2846d02440b5cea7fafee4fa5, type: 3} + insertIndex: -1 + addedObject: {fileID: 1284890352} + - targetCorrespondingSourceObject: {fileID: 3861359295483779369, guid: 0a84c5c2846d02440b5cea7fafee4fa5, type: 3} + insertIndex: -1 + addedObject: {fileID: 1530882415} + - targetCorrespondingSourceObject: {fileID: 3861359295483779369, guid: 0a84c5c2846d02440b5cea7fafee4fa5, type: 3} + insertIndex: -1 + addedObject: {fileID: 1654286369} + - targetCorrespondingSourceObject: {fileID: 3861359295483779369, guid: 0a84c5c2846d02440b5cea7fafee4fa5, type: 3} + insertIndex: -1 + addedObject: {fileID: 645202328} m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 0a84c5c2846d02440b5cea7fafee4fa5, type: 3} +--- !u!4 &614713748 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 3861359295483779369, guid: 0a84c5c2846d02440b5cea7fafee4fa5, type: 3} + m_PrefabInstance: {fileID: 614713747} + m_PrefabAsset: {fileID: 0} +--- !u!1 &645202327 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 645202328} + - component: {fileID: 645202329} + m_Layer: 0 + m_Name: Wheel_BR_Collider + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &645202328 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 645202327} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: 1, z: -0, w: 0} + m_LocalPosition: {x: 0.88400006, y: 0.416, z: -1.527} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 614713748} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!146 &645202329 +WheelCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 645202327} + serializedVersion: 2 + m_Center: {x: 0, y: 0, z: 0} + m_Radius: 0.5 + m_SuspensionSpring: + spring: 35000 + damper: 4500 + targetPosition: 0.5 + m_SuspensionDistance: 0.3 + m_ForceAppPointDistance: 0 + m_Mass: 20 + m_WheelDampingRate: 0.25 + m_ForwardFriction: + m_ExtremumSlip: 0.4 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.8 + m_AsymptoteValue: 0.5 + m_Stiffness: 1 + m_SidewaysFriction: + m_ExtremumSlip: 0.2 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.5 + m_AsymptoteValue: 0.75 + m_Stiffness: 1 + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_Enabled: 1 + m_ProvidesContacts: 0 --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 @@ -345,6 +433,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1284890352} + - component: {fileID: 1284890353} m_Layer: 0 m_Name: Wheel_FL_Collider m_TagString: Untagged @@ -360,13 +449,52 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1284890351} serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 25.933868, y: -4.4398885, z: -0.03654265} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: -0.8859999, y: 0.416, z: 1.539} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 0} + m_Father: {fileID: 614713748} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!146 &1284890353 +WheelCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1284890351} + serializedVersion: 2 + m_Center: {x: 0, y: 0, z: 0} + m_Radius: 0.5 + m_SuspensionSpring: + spring: 35000 + damper: 4500 + targetPosition: 0.5 + m_SuspensionDistance: 0.3 + m_ForceAppPointDistance: 0 + m_Mass: 20 + m_WheelDampingRate: 0.25 + m_ForwardFriction: + m_ExtremumSlip: 0.4 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.8 + m_AsymptoteValue: 0.5 + m_Stiffness: 1 + m_SidewaysFriction: + m_ExtremumSlip: 0.2 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.5 + m_AsymptoteValue: 0.75 + m_Stiffness: 1 + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_Enabled: 1 + m_ProvidesContacts: 0 --- !u!1 &1530882414 GameObject: m_ObjectHideFlags: 0 @@ -376,8 +504,9 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1530882415} + - component: {fileID: 1530882416} m_Layer: 0 - m_Name: Wheel_FL_Collider (1) + m_Name: Wheel_FR_Collider m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -391,13 +520,123 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1530882414} serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 25.933868, y: -4.4398885, z: -0.03654265} + m_LocalRotation: {x: -0, y: 1, z: -0, w: 0} + m_LocalPosition: {x: 0.88400006, y: 0.416, z: 1.5360001} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 0} + m_Father: {fileID: 614713748} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!146 &1530882416 +WheelCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1530882414} + serializedVersion: 2 + m_Center: {x: 0, y: 0, z: 0} + m_Radius: 0.5 + m_SuspensionSpring: + spring: 35000 + damper: 4500 + targetPosition: 0.5 + m_SuspensionDistance: 0.3 + m_ForceAppPointDistance: 0 + m_Mass: 20 + m_WheelDampingRate: 0.25 + m_ForwardFriction: + m_ExtremumSlip: 0.4 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.8 + m_AsymptoteValue: 0.5 + m_Stiffness: 1 + m_SidewaysFriction: + m_ExtremumSlip: 0.2 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.5 + m_AsymptoteValue: 0.75 + m_Stiffness: 1 + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_Enabled: 1 + m_ProvidesContacts: 0 +--- !u!1 &1654286368 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1654286369} + - component: {fileID: 1654286370} + m_Layer: 0 + m_Name: Wheel_BL_Collider + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1654286369 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1654286368} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: -0.8859999, y: 0.416, z: -1.527} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 614713748} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!146 &1654286370 +WheelCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1654286368} + serializedVersion: 2 + m_Center: {x: 0, y: 0, z: 0} + m_Radius: 0.5 + m_SuspensionSpring: + spring: 35000 + damper: 4500 + targetPosition: 0.5 + m_SuspensionDistance: 0.3 + m_ForceAppPointDistance: 0 + m_Mass: 20 + m_WheelDampingRate: 0.25 + m_ForwardFriction: + m_ExtremumSlip: 0.4 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.8 + m_AsymptoteValue: 0.5 + m_Stiffness: 1 + m_SidewaysFriction: + m_ExtremumSlip: 0.2 + m_ExtremumValue: 1 + m_AsymptoteSlip: 0.5 + m_AsymptoteValue: 0.75 + m_Stiffness: 1 + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_Enabled: 1 + m_ProvidesContacts: 0 --- !u!1 &1897464303 stripped GameObject: m_CorrespondingSourceObject: {fileID: 4840733166066636179, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} @@ -686,5 +925,3 @@ SceneRoots: - {fileID: 4212961364147142189} - {fileID: 1235630211} - {fileID: 614713747} - - {fileID: 1284890352} - - {fileID: 1530882415} diff --git a/Assets/Scripts/CarControl.cs b/Assets/Scripts/CarControl.cs new file mode 100644 index 0000000..a9be03e --- /dev/null +++ b/Assets/Scripts/CarControl.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CarControl : MonoBehaviour +{ + + public float motorTorque = 2000; + public float brakeTorque = 2000; + public float maxSpeed = 20; + public float steeringRange = 30; + //public float steeringRangeAtMaxSpeed = 10; + WheelControl[] wheels; + Rigidbody rigidBody; + // Start is called before the first frame update + void Start() + { + rigidBody = GetComponent(); + + // Find all child GameObjects that have the WheelControl script attached + wheels = GetComponentsInChildren(); + } + + // Update is called once per frame + void Update() + { + float vInput = Input.GetAxis("Vertical"); + float hInput = Input.GetAxis("Horizontal"); + + foreach (var wheel in wheels) + { + // Apply steering to Wheel colliders that have "Steerable" enabled + if (wheel.steerable) + { + wheel.WheelCollider.steerAngle = hInput * steeringRange; + } + + // Apply torque to Wheel colliders that have "Motorized" enabled + if (wheel.motorized) + { + wheel.WheelCollider.motorTorque = vInput * motorTorque; + } + } + } +} diff --git a/Assets/Scripts/CarControl.cs.meta b/Assets/Scripts/CarControl.cs.meta new file mode 100644 index 0000000..bc6da8b --- /dev/null +++ b/Assets/Scripts/CarControl.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a23c7d66d3ff94847a946cf7b30ca1b7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/car control v2.cs b/Assets/Scripts/car control v2.cs index 82aeb15..5044e17 100644 --- a/Assets/Scripts/car control v2.cs +++ b/Assets/Scripts/car control v2.cs @@ -9,7 +9,6 @@ public class carcontrolv2 : MonoBehaviour public float maxSpeed = 20; public float steeringRange = 30; public float steeringRangeAtMaxSpeed = 10; - public float centreOfGravityOffset = -1f; WheelControl[] wheels; Rigidbody rigidBody; @@ -19,9 +18,6 @@ public class carcontrolv2 : MonoBehaviour { rigidBody = GetComponent(); - // Adjust center of mass vertically, to help prevent the car from rolling - rigidBody.centerOfMass += Vector3.up * centreOfGravityOffset; - // Find all child GameObjects that have the WheelControl script attached wheels = GetComponentsInChildren(); }