From c2b26c0491886b99f422e830cd9ec1637a4ddc2e Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 1 Mar 2026 09:36:29 +0800 Subject: first --- Assets/Scripts/GamePlay/Player.meta | 8 ++ Assets/Scripts/GamePlay/Player/ChairMovement.cs | 102 +++++++++++++++++++++ .../Scripts/GamePlay/Player/ChairMovement.cs.meta | 2 + .../Scripts/GamePlay/Player/WheelMeshPosition.cs | 17 ++++ .../GamePlay/Player/WheelMeshPosition.cs.meta | 2 + Assets/Scripts/GamePlay/Player/WheelTransforms.cs | 48 ++++++++++ .../GamePlay/Player/WheelTransforms.cs.meta | 2 + Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef | 16 ++++ .../Scripts/GamePlay/WCGame.GamePlay.asmdef.meta | 7 ++ 9 files changed, 204 insertions(+) create mode 100644 Assets/Scripts/GamePlay/Player.meta create mode 100644 Assets/Scripts/GamePlay/Player/ChairMovement.cs create mode 100644 Assets/Scripts/GamePlay/Player/ChairMovement.cs.meta create mode 100644 Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs create mode 100644 Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs.meta create mode 100644 Assets/Scripts/GamePlay/Player/WheelTransforms.cs create mode 100644 Assets/Scripts/GamePlay/Player/WheelTransforms.cs.meta create mode 100644 Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef create mode 100644 Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef.meta (limited to 'Assets/Scripts/GamePlay') diff --git a/Assets/Scripts/GamePlay/Player.meta b/Assets/Scripts/GamePlay/Player.meta new file mode 100644 index 0000000..bebca6a --- /dev/null +++ b/Assets/Scripts/GamePlay/Player.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 70966302095eeba4d87abbe830c3f877 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GamePlay/Player/ChairMovement.cs b/Assets/Scripts/GamePlay/Player/ChairMovement.cs new file mode 100644 index 0000000..f254c97 --- /dev/null +++ b/Assets/Scripts/GamePlay/Player/ChairMovement.cs @@ -0,0 +1,102 @@ +using System; +using UnityEngine; +using UnityEngine.InputSystem; + +namespace GamePlay.Player +{ + [RequireComponent(typeof(PlayerInput))] + [RequireComponent(typeof(Rigidbody))] + public class ChairMovement : MonoBehaviour + { + private PlayerInput _playerInput; + private Rigidbody _rigidbody; + + private float _inputHorizontal; + private float _inputVertical; + + public float maxMotorTorque = 400f; + public float maxSteerAngle = 90f; + public float brakeTorque = 1000f; + + public WheelCollider frontLeftWheel; + public WheelCollider frontRightWheel; + public WheelCollider rearLeftWheel; + public WheelCollider rearRightWheel; + + private void Awake() + { + _playerInput = GetComponent(); + _rigidbody = GetComponent(); + + _playerInput.onActionTriggered += OnActionTriggered; + } + + private void OnActionTriggered(InputAction.CallbackContext ctx) + { + if (ctx.action.name == "Move") + { + var moveInput = ctx.ReadValue(); + _inputHorizontal = moveInput.x; + _inputVertical = moveInput.y; + } + } + + private void FixedUpdate() + { + ApplySteering(); + ApplyMotor(); + ApplyBrakes(); + } + + private void ApplySteering() + { + float steer = maxSteerAngle * _inputHorizontal; + frontLeftWheel.steerAngle = steer; + frontRightWheel.steerAngle = steer; + } + + private void ApplyMotor() + { + if (_inputVertical > 0) + { + float motor = maxMotorTorque * _inputVertical; + OperateWheel(w => w.motorTorque = motor); + + // 释放刹车(因为在前进) + OperateWheel(w => w.brakeTorque = 0); + } + } + + private void ApplyBrakes() + { + if (_inputVertical < 0) + { + if (_rigidbody.linearVelocity.z > 0.1f) + { + float brake = brakeTorque * Mathf.Abs(_inputVertical); + OperateWheel(w => w.brakeTorque = brake); + OperateWheel(w => w.motorTorque = 0); + } + else + { + OperateWheel(w => w.brakeTorque = 0); + OperateWheel(w => w.motorTorque = maxMotorTorque * _inputVertical); + } + } + else if (Mathf.Abs(_inputVertical) < 0.1f) + { + float parkingBrake = 10f; + OperateWheel(w => w.brakeTorque = parkingBrake); + OperateWheel(w => w.motorTorque = 0); + } + } + + private void OperateWheel(Action o) + { + o(frontLeftWheel); + o(frontRightWheel); + o(frontLeftWheel); + o(frontRightWheel); + } + } +} diff --git a/Assets/Scripts/GamePlay/Player/ChairMovement.cs.meta b/Assets/Scripts/GamePlay/Player/ChairMovement.cs.meta new file mode 100644 index 0000000..04bb4df --- /dev/null +++ b/Assets/Scripts/GamePlay/Player/ChairMovement.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: eb1f6a494562d1f4c9f1cf36acd136b2 \ No newline at end of file diff --git a/Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs b/Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs new file mode 100644 index 0000000..6f68a4c --- /dev/null +++ b/Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace GamePlay.Player +{ + public class WheelMeshPosition : MonoBehaviour + { + [Range(0, 1)] public float lerpSpeed; + public WheelCollider wheelCollider; + + private void FixedUpdate() + { + wheelCollider.GetWorldPose(out Vector3 pos, out Quaternion rot); + transform.position = Vector3.Lerp(transform.position, pos, lerpSpeed); + transform.rotation = Quaternion.Slerp(transform.rotation, rot, lerpSpeed); + } + } +} diff --git a/Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs.meta b/Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs.meta new file mode 100644 index 0000000..f0cb003 --- /dev/null +++ b/Assets/Scripts/GamePlay/Player/WheelMeshPosition.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: c250814400f5b9f4197bd111feae1bdb \ No newline at end of file diff --git a/Assets/Scripts/GamePlay/Player/WheelTransforms.cs b/Assets/Scripts/GamePlay/Player/WheelTransforms.cs new file mode 100644 index 0000000..0834f32 --- /dev/null +++ b/Assets/Scripts/GamePlay/Player/WheelTransforms.cs @@ -0,0 +1,48 @@ +using System; +using UnityEngine; + +namespace GamePlay.Player +{ + [ExecuteAlways] + public class WheelTransforms : MonoBehaviour + { + [Header("Parameters")] + [Range(0f, 4f)] public float length; + [Range(0f, 4f)] public float width; + [Range(-5f, 5f)] public float offsetX; + [Range(-5f, 5f)] public float offsetZ; + [Range(-5f, 5f)] public float offsetY; + + [Header("Bindings")] + public Transform frontLeft; + public Transform frontRight; + public Transform rearLeft; + public Transform rearRight; + +#if UNITY_EDITOR + private void Update() + { + if (Application.isPlaying) return; + UpdateWheels(); + } +#endif + + private void FixedUpdate() + { + UpdateWheels(); + } + + private void UpdateWheels() + { + frontLeft.localPosition = GetPosition(-1, 1); + frontRight.localPosition = GetPosition(1, 1); + rearLeft.localPosition = GetPosition(-1, -1); + rearRight.localPosition = GetPosition(1, -1); + } + + private Vector3 GetPosition(float axisX, float axisZ) + { + return new Vector3(width / 2 * axisX + offsetX, offsetY, length / 2 * axisZ + offsetZ); + } + } +} diff --git a/Assets/Scripts/GamePlay/Player/WheelTransforms.cs.meta b/Assets/Scripts/GamePlay/Player/WheelTransforms.cs.meta new file mode 100644 index 0000000..ad330a6 --- /dev/null +++ b/Assets/Scripts/GamePlay/Player/WheelTransforms.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b17f16ff6b13c774ba87283567ee2169 \ No newline at end of file diff --git a/Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef b/Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef new file mode 100644 index 0000000..852576f --- /dev/null +++ b/Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef @@ -0,0 +1,16 @@ +{ + "name": "WCGame.GamePlay", + "rootNamespace": "", + "references": [ + "GUID:75469ad4d38634e559750d17036d5f7c" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef.meta b/Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef.meta new file mode 100644 index 0000000..084005e --- /dev/null +++ b/Assets/Scripts/GamePlay/WCGame.GamePlay.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 0ed168a6ec579ae4c8dc6a86d00f033b +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: -- cgit