From 7e2f7095ca22d434d7bad8e0c1a233395fd9d060 Mon Sep 17 00:00:00 2001 From: magn9775 Date: Fri, 5 Apr 2024 12:30:05 +0200 Subject: [PATCH] another car sript --- Assets/Scenes/SampleScene.unity | 104 +++++++++++++++++++++++-- Assets/Scripts/InputController.cs | 17 ++++ Assets/Scripts/InputController.cs.meta | 11 +++ Assets/Scripts/VehichleControl.cs | 54 +++++++++++++ Assets/Scripts/VehichleControl.cs.meta | 11 +++ 5 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 Assets/Scripts/InputController.cs create mode 100644 Assets/Scripts/InputController.cs.meta create mode 100644 Assets/Scripts/VehichleControl.cs create mode 100644 Assets/Scripts/VehichleControl.cs.meta diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5f99b11..6f00926 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -421,6 +421,50 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &2026056263 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2026056264} + - component: {fileID: 2026056265} + m_Layer: 0 + m_Name: InputController + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &2026056264 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2026056263} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -3.367576, y: 0.82818604, z: 3.8048892} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2026056265 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2026056263} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 43a3b21977ed2c64b8d62ca5013ab914, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &1328367594557032345 PrefabInstance: m_ObjectHideFlags: 0 @@ -429,6 +473,10 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 1857703685905104433, guid: 5851f609a58dc884a99030da0105c6db, type: 3} + propertyPath: m_Enabled + value: 0 + objectReference: {fileID: 0} - target: {fileID: 5420764024659664985, guid: 5851f609a58dc884a99030da0105c6db, type: 3} propertyPath: m_LocalPosition.x value: -4.25 @@ -476,13 +524,52 @@ PrefabInstance: m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 5711380006811689493, guid: 5851f609a58dc884a99030da0105c6db, type: 3} + insertIndex: -1 + addedObject: {fileID: 1328367594557032350} m_SourcePrefab: {fileID: 100100000, guid: 5851f609a58dc884a99030da0105c6db, type: 3} +--- !u!1 &1328367594557032346 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 5711380006811689493, guid: 5851f609a58dc884a99030da0105c6db, type: 3} + m_PrefabInstance: {fileID: 1328367594557032345} + m_PrefabAsset: {fileID: 0} +--- !u!146 &1328367594557032347 stripped +WheelCollider: + m_CorrespondingSourceObject: {fileID: 6619084378601959143, guid: 5851f609a58dc884a99030da0105c6db, type: 3} + m_PrefabInstance: {fileID: 1328367594557032345} + m_PrefabAsset: {fileID: 0} +--- !u!146 &1328367594557032348 stripped +WheelCollider: + m_CorrespondingSourceObject: {fileID: 1553392025741153767, guid: 5851f609a58dc884a99030da0105c6db, type: 3} + m_PrefabInstance: {fileID: 1328367594557032345} + m_PrefabAsset: {fileID: 0} --- !u!4 &1328367594557032349 stripped Transform: m_CorrespondingSourceObject: {fileID: 5420764024659664985, guid: 5851f609a58dc884a99030da0105c6db, type: 3} m_PrefabInstance: {fileID: 1328367594557032345} m_PrefabAsset: {fileID: 0} +--- !u!114 &1328367594557032350 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1328367594557032346} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8c9672ffa7e21bf41b27f35da94be659, type: 3} + m_Name: + m_EditorClassIdentifier: + InputCtrl: {fileID: 2026056265} + WheelColliders: + - {fileID: 1328367594557032348} + - {fileID: 1328367594557032347} + Wheels: [] + CenterOfMass: {fileID: 0} + Force: 1000 + Angle: 35 + BrakeForce: 500 --- !u!1001 &4212961364147142189 PrefabInstance: m_ObjectHideFlags: 0 @@ -505,11 +592,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.w - value: 0.9938837 + value: 0.9907348 objectReference: {fileID: 0} - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.x - value: 0.11043153 + value: 0.13581057 objectReference: {fileID: 0} - target: {fileID: 320316327550477191, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.y @@ -553,11 +640,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 6331817402021486753, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalPosition.y - value: 1.9205093 + value: 4.4205093 objectReference: {fileID: 0} - target: {fileID: 6331817402021486753, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalPosition.z - value: -6.9411907 + value: -13.941193 objectReference: {fileID: 0} - target: {fileID: 6331817402021486753, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.w @@ -590,7 +677,7 @@ PrefabInstance: - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_Follow value: - objectReference: {fileID: 1328367594557032349} + objectReference: {fileID: 0} - target: {fileID: 8858152487650577344, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LookAt value: @@ -613,11 +700,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.w - value: 0.9938837 + value: 0.9907348 objectReference: {fileID: 0} - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.x - value: 0.11043153 + value: 0.13581057 objectReference: {fileID: 0} - target: {fileID: 8890610309520933432, guid: 80c5f8bcfe7e033419638b39d9a278c4, type: 3} propertyPath: m_LocalRotation.y @@ -641,6 +728,7 @@ SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 705507995} + - {fileID: 2026056264} - {fileID: 1915174281} - {fileID: 4212961364147142189} - {fileID: 1235630211} diff --git a/Assets/Scripts/InputController.cs b/Assets/Scripts/InputController.cs new file mode 100644 index 0000000..5741414 --- /dev/null +++ b/Assets/Scripts/InputController.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class InputController : MonoBehaviour +{ + public float Horizontal { get; private set; } + public float Vertical { get; private set; } + public float Brake { get; private set; } + + void Update() + { + Horizontal = Input.GetAxis("Horizontal"); + Vertical = Input.GetAxis("Vertical"); + Brake = Input.GetAxis("Jump"); + } +} diff --git a/Assets/Scripts/InputController.cs.meta b/Assets/Scripts/InputController.cs.meta new file mode 100644 index 0000000..c15fd03 --- /dev/null +++ b/Assets/Scripts/InputController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 43a3b21977ed2c64b8d62ca5013ab914 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/VehichleControl.cs b/Assets/Scripts/VehichleControl.cs new file mode 100644 index 0000000..ac2269a --- /dev/null +++ b/Assets/Scripts/VehichleControl.cs @@ -0,0 +1,54 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class VehichleControl : MonoBehaviour +{ + public InputController InputCtrl; + [Tooltip("Set ref in order of FL, FR, RL, RR")] + public WheelCollider[] WheelColliders; + + [Tooltip("Set ref of wheel meshes in order of FL, FR, RL, RR")] + public Transform[] Wheels; + + public Transform CenterOfMass; + + public int Force; + public int Angle; + public int BrakeForce; + + private void Drive() + { + WheelColliders[0].motorTorque = WheelColliders[1].motorTorque = InputCtrl.Vertical * Force; + } + + private void Steer() + { + WheelColliders[0].steerAngle = WheelColliders[1].steerAngle = InputCtrl.Horizontal * Angle; + } + + private void Brake() + { + WheelColliders[0].brakeTorque = WheelColliders[1].brakeTorque = InputCtrl.Brake * BrakeForce; + } + + private void UpdateWheelMovements() + { + for (var i = 0; i < Wheels.Length; i++) + { + Vector3 pos; + Quaternion rot; + WheelColliders[i].GetWorldPose(out pos, out rot); + Wheels[i].transform.position = pos; + Wheels[i].transform.rotation = rot; + } + } + + private void FixedUpdate() + { + Steer(); + Drive(); + Brake(); + UpdateWheelMovements(); + } +} diff --git a/Assets/Scripts/VehichleControl.cs.meta b/Assets/Scripts/VehichleControl.cs.meta new file mode 100644 index 0000000..75b851f --- /dev/null +++ b/Assets/Scripts/VehichleControl.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8c9672ffa7e21bf41b27f35da94be659 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: