diff --git a/Assets/Prefabs/Camera.prefab b/Assets/Prefabs/Camera.prefab index 2ebb45e..13794e7 100644 --- a/Assets/Prefabs/Camera.prefab +++ b/Assets/Prefabs/Camera.prefab @@ -90,7 +90,7 @@ MonoBehaviour: FieldOfView: 60.000004 OrthographicSize: 5 NearClipPlane: 0.3 - FarClipPlane: 1000 + FarClipPlane: 500 Dutch: 0 ModeOverride: 0 LensShift: {x: 0, y: 0} @@ -163,17 +163,17 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: bd6043bde05a7fc4cba197d06915c1e3, type: 3} m_Name: m_EditorClassIdentifier: - Damping: {x: 5, y: 0.5, z: 1.5} - ShoulderOffset: {x: 0, y: 2, z: 0} + Damping: {x: 4, y: 0.4, z: 1} + ShoulderOffset: {x: 0, y: 1.75, z: 0} VerticalArmLength: 0.4 - CameraSide: 1 + CameraSide: 0.5 CameraDistance: 5 CameraCollisionFilter: serializedVersion: 2 m_Bits: 0 IgnoreTag: CameraRadius: 0.5 - DampingIntoCollision: 0 + DampingIntoCollision: 4.6 DampingFromCollision: 2 --- !u!114 &7119497487385811357 MonoBehaviour: @@ -214,8 +214,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 5810170567460595970} serializedVersion: 2 - m_LocalRotation: {x: 0.22189745, y: -0, z: -0, w: 0.97507} - m_LocalPosition: {x: 6.063599, y: 1.029345, z: -2.1784945} + m_LocalRotation: {x: 0.20165665, y: -0, z: -0, w: 0.9794563} + m_LocalPosition: {x: 6.063599, y: 0.77934504, z: -2.1784945} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -253,7 +253,7 @@ Camera: width: 1 height: 1 near clip plane: 0.3 - far clip plane: 1000 + far clip plane: 500 field of view: 60.000004 orthographic: 0 orthographic size: 5 diff --git a/Assets/Scenes/Racetrack.unity b/Assets/Scenes/Racetrack.unity index f8c2a78..804007a 100644 --- a/Assets/Scenes/Racetrack.unity +++ b/Assets/Scenes/Racetrack.unity @@ -1642,11 +1642,6 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 3b4743a43e04ed145b531ab0f1ae828b, type: 3} ---- !u!4 &1293318021 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - m_PrefabInstance: {fileID: 98617657} - m_PrefabAsset: {fileID: 0} --- !u!1001 &1298794172 PrefabInstance: m_ObjectHideFlags: 0 @@ -1875,6 +1870,11 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 8e14ccd68551a1a4e93b83808515b996, type: 3} +--- !u!4 &1403837502 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + m_PrefabInstance: {fileID: 98617657} + m_PrefabAsset: {fileID: 0} --- !u!1001 &1443287320 PrefabInstance: m_ObjectHideFlags: 0 @@ -2123,10 +2123,38 @@ PrefabInstance: propertyPath: m_LocalPosition.z value: -2.1784945 objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.x + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 25.641 + objectReference: {fileID: 0} + - target: {fileID: 679652745285801860, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: far clip plane + value: 500 + objectReference: {fileID: 0} + - target: {fileID: 679652745285801860, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: near clip plane + value: 0.3 + objectReference: {fileID: 0} - target: {fileID: 1387890555408215438, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_Name value: Camera objectReference: {fileID: 0} + - target: {fileID: 2100622393876020061, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: CameraSide + value: 0.5 + objectReference: {fileID: 0} + - target: {fileID: 2100622393876020061, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: ShoulderOffset.y + value: 2 + objectReference: {fileID: 0} - target: {fileID: 6331817402021486753, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalPosition.x value: -2.5158076 @@ -2170,11 +2198,11 @@ PrefabInstance: - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_Follow value: - objectReference: {fileID: 1293318021} + objectReference: {fileID: 1403837502} - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LookAt value: - objectReference: {fileID: 1293318021} + objectReference: {fileID: 1403837502} - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalPosition.x value: 8.515808 @@ -2189,13 +2217,18 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.w - value: 0.97507 + value: 1 objectReference: {fileID: 0} - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.x - value: 0.22189745 + value: -0 objectReference: {fileID: 0} - m_RemovedComponents: [] + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 10.641 + objectReference: {fileID: 0} + m_RemovedComponents: + - {fileID: 7119497487385811357, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} m_RemovedGameObjects: [] m_AddedGameObjects: [] m_AddedComponents: [] diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index f843540..11faa4f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.44657826, g: 0.49641263, b: 0.57481676, a: 1} + m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -123,11 +123,6 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} ---- !u!4 &614480971 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - m_PrefabInstance: {fileID: 4305065282094742130} - m_PrefabAsset: {fileID: 0} --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 @@ -222,6 +217,38 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!1 &1897464303 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 4840733166066636179, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + m_PrefabInstance: {fileID: 4212961364147142189} + m_PrefabAsset: {fileID: 0} +--- !u!114 &1897464305 +MonoBehaviour: + m_ObjectHideFlags: 3 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1897464303} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f4044717213e31446939f7bd49c896ea, type: 3} + m_Name: + m_EditorClassIdentifier: + m_TrackedObjectOffset: {x: 0, y: 0, z: 0} + m_LookaheadTime: 0 + m_LookaheadSmoothing: 0 + m_LookaheadIgnoreY: 1 + m_HorizontalDamping: 0.5 + m_VerticalDamping: 0.5 + m_ScreenX: 0.5 + m_ScreenY: 0.5 + m_DeadZoneWidth: 0 + m_DeadZoneHeight: 0 + m_SoftZoneWidth: 0.8 + m_SoftZoneHeight: 0.8 + m_BiasX: 0 + m_BiasY: 0 + m_CenterOnActivate: 1 --- !u!1 &1915174277 GameObject: m_ObjectHideFlags: 0 @@ -323,7 +350,7 @@ Transform: serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 10, y: 1, z: 10} + m_LocalScale: {x: 100, y: 1, z: 100} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} @@ -336,10 +363,50 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalPosition.x + value: 2.5158076 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalPosition.y + value: 0.22949076 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalPosition.z + value: -3.0588093 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.w + value: 0.99439913 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.x + value: 0.10569032 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.y + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.z + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 679652745285801860, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: field of view + value: 60 + objectReference: {fileID: 0} - target: {fileID: 1387890555408215438, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_Name value: Camera objectReference: {fileID: 0} + - target: {fileID: 2100622393876020061, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: CameraDistance + value: 10 + objectReference: {fileID: 0} + - target: {fileID: 2100622393876020061, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: ShoulderOffset.z + value: 0 + objectReference: {fileID: 0} - target: {fileID: 6331817402021486753, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalPosition.x value: -2.5158076 @@ -383,17 +450,53 @@ PrefabInstance: - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_Follow value: - objectReference: {fileID: 614480971} + objectReference: {fileID: 9056490074252112709} - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LookAt value: - objectReference: {fileID: 614480971} - m_RemovedComponents: [] + objectReference: {fileID: 9056490074252112709} + - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_Lens.FieldOfView + value: 60 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalPosition.x + value: 2.5158076 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalPosition.y + value: 0.22949076 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalPosition.z + value: -3.0588093 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.w + value: 0.99439913 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.x + value: 0.10569032 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.y + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + propertyPath: m_LocalRotation.z + value: -0 + objectReference: {fileID: 0} + m_RemovedComponents: + - {fileID: 7119497487385811357, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 4840733166066636179, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} + insertIndex: -1 + addedObject: {fileID: 1897464305} m_SourcePrefab: {fileID: 100100000, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} ---- !u!1001 &4305065282094742130 +--- !u!1001 &5532270393676571022 PrefabInstance: m_ObjectHideFlags: 0 serializedVersion: 2 @@ -401,92 +504,65 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 0} m_Modifications: - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalPosition.x - value: -2.4905412 - objectReference: {fileID: 0} - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalPosition.y - value: 3.9198024 - objectReference: {fileID: 0} - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalPosition.z - value: -13.815386 - objectReference: {fileID: 0} - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalRotation.w - value: -0.9852688 - objectReference: {fileID: 0} - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalRotation.x - value: -0.17096418 - objectReference: {fileID: 0} - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalRotation.y - value: -0.004030049 - objectReference: {fileID: 0} - - target: {fileID: 1959449010828185711, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_LocalRotation.z - value: 0.0006944585 - objectReference: {fileID: 0} - - target: {fileID: 4258781391370674817, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: m_Name - value: Car - objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalPosition.x value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalPosition.y - value: 0.55 + value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalPosition.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalRotation.w value: 1 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalRotation.x value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalRotation.y value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalRotation.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalEulerAnglesHint.x value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalEulerAnglesHint.y value: 0 objectReference: {fileID: 0} - - target: {fileID: 5049018613629602103, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + - target: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 6277473666848002999, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} - propertyPath: field of view - value: 60.000004 + - target: {fileID: 4811018499574817333, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} + propertyPath: m_Name + value: Car objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] m_AddedComponents: [] - m_SourcePrefab: {fileID: 100100000, guid: 77e4cd38c4994de4f9312bea83b75104, type: 3} + m_SourcePrefab: {fileID: 100100000, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} +--- !u!4 &9056490074252112709 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 1898089692201353361, guid: e3976318bf4730e4583a4e03a5b2ce04, type: 3} + m_PrefabInstance: {fileID: 5532270393676571022} + m_PrefabAsset: {fileID: 0} --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 705507995} - {fileID: 1915174281} - - {fileID: 4305065282094742130} + - {fileID: 5532270393676571022} - {fileID: 4212961364147142189} diff --git a/Assets/Scripts/Car Controller.cs b/Assets/Scripts/Car Controller.cs index c111f8f..0bff8cf 100644 --- a/Assets/Scripts/Car Controller.cs +++ b/Assets/Scripts/Car Controller.cs @@ -14,14 +14,12 @@ public class CarController : MonoBehaviour { // Motor input float motorInput = Input.GetAxis("Vertical"); - frontLeftWheel.motorTorque = motorInput * motorForce; - frontRightWheel.motorTorque = motorInput * motorForce; + rearLeftWheel.motorTorque = motorInput * motorForce; + rearRightWheel.motorTorque = motorInput * motorForce; // Steering input float steeringInput = Input.GetAxis("Horizontal"); frontLeftWheel.steerAngle = steeringInput * steeringAngle; frontRightWheel.steerAngle = steeringInput * steeringAngle; - - // Move camera } } diff --git a/Assets/Scripts/WheelControl.cs b/Assets/Scripts/WheelControl.cs new file mode 100644 index 0000000..85da8a3 --- /dev/null +++ b/Assets/Scripts/WheelControl.cs @@ -0,0 +1,35 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class WheelControl : MonoBehaviour +{ + public Transform wheelModel; + + [HideInInspector] public WheelCollider WheelCollider; + + // Create properties for the CarControl script + // (You should enable/disable these via the + // Editor Inspector window) + public bool steerable; + public bool motorized; + + Vector3 position; + Quaternion rotation; + + // Start is called before the first frame update + private void Start() + { + WheelCollider = GetComponent(); + } + + // Update is called once per frame + void Update() + { + // Get the Wheel collider's world pose values and + // use them to set the wheel model's position and rotation + WheelCollider.GetWorldPose(out position, out rotation); + wheelModel.transform.position = position; + wheelModel.transform.rotation = rotation; + } +} diff --git a/Assets/Scripts/WheelControl.cs.meta b/Assets/Scripts/WheelControl.cs.meta new file mode 100644 index 0000000..8d9f1ec --- /dev/null +++ b/Assets/Scripts/WheelControl.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9901c050a200da94bbc65da2891b7708 +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 new file mode 100644 index 0000000..82aeb15 --- /dev/null +++ b/Assets/Scripts/car control v2.cs @@ -0,0 +1,83 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class carcontrolv2 : MonoBehaviour +{ + public float motorTorque = 2000; + public float brakeTorque = 2000; + public float maxSpeed = 20; + public float steeringRange = 30; + public float steeringRangeAtMaxSpeed = 10; + public float centreOfGravityOffset = -1f; + + WheelControl[] wheels; + Rigidbody rigidBody; + + // Start is called before the first frame update + void Start() + { + 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(); + } + + // Update is called once per frame + void Update() + { + + float vInput = Input.GetAxis("Vertical"); + float hInput = Input.GetAxis("Horizontal"); + + // Calculate current speed in relation to the forward direction of the car + // (this returns a negative number when traveling backwards) + float forwardSpeed = Vector3.Dot(transform.forward, rigidBody.velocity); + + + // Calculate how close the car is to top speed + // as a number from zero to one + float speedFactor = Mathf.InverseLerp(0, maxSpeed, forwardSpeed); + + // Use that to calculate how much torque is available + // (zero torque at top speed) + float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor); + + // …and to calculate how much to steer + // (the car steers more gently at top speed) + float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor); + + // Check whether the user input is in the same direction + // as the car's velocity + bool isAccelerating = Mathf.Sign(vInput) == Mathf.Sign(forwardSpeed); + + foreach (var wheel in wheels) + { + // Apply steering to Wheel colliders that have "Steerable" enabled + if (wheel.steerable) + { + wheel.WheelCollider.steerAngle = hInput * currentSteerRange; + } + + if (isAccelerating) + { + // Apply torque to Wheel colliders that have "Motorized" enabled + if (wheel.motorized) + { + wheel.WheelCollider.motorTorque = vInput * currentMotorTorque; + } + wheel.WheelCollider.brakeTorque = 0; + } + else + { + // If the user is trying to go in the opposite direction + // apply brakes to all wheels + wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque; + wheel.WheelCollider.motorTorque = 0; + } + } + } +} diff --git a/Assets/Scripts/car control v2.cs.meta b/Assets/Scripts/car control v2.cs.meta new file mode 100644 index 0000000..3dbeb7a --- /dev/null +++ b/Assets/Scripts/car control v2.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 58d3a2623a4164b4bb25683edc176b03 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: