Над физикой работаю. Добаил UI минимальное

This commit is contained in:
bybohka committed 2026-09-20 02:35:27 +03:00
1 parent 48753f65fb
commit f9ec01d1c8
912 files changed
+204440 -3555

No files matched your search

+28 -11
View File
@@ -5,15 +5,14 @@ public enum DriveType { FWD, RWD, AWD } // Передний, Задний, По
[RequireComponent(typeof(Rigidbody))]
public class CarController : MonoBehaviour
{
public float SpeedKmH => rb.linearVelocity.magnitude * 3.6f;
[Header("Drive Settings")]
public DriveType driveType = DriveType.AWD;
[SerializeField] private float motorForce = 1500f;
[SerializeField] private float brakeForce = 3000f;
[SerializeField] private float maxSteerAngle = 30f;
[Header("Arcade Feel")]
[SerializeField] private Transform centerOfMass; // Заниженный центр тяжести
[Header("Wheel Colliders")]
[SerializeField] private WheelCollider frontLeftCollider;
[SerializeField] private WheelCollider frontRightCollider;
@@ -26,6 +25,10 @@ public class CarController : MonoBehaviour
[SerializeField] private Transform rearLeftMesh;
[SerializeField] private Transform rearRightMesh;
[Header("Arcade Braking")]
[SerializeField] private float brakingDrag = 2f;
private float defaultDrag;
private Rigidbody rb;
private float currentMotorForce;
private float currentBrakeForce;
@@ -34,10 +37,7 @@ public class CarController : MonoBehaviour
private void Start()
{
rb = GetComponent<Rigidbody>();
if (centerOfMass != null)
{
rb.centerOfMass = centerOfMass.localPosition; // Смещаем центр масс вниз для аркадности
}
defaultDrag = rb.linearDamping;
}
// Метод вызывается из другого скрипта (ввода)
@@ -54,6 +54,7 @@ public class CarController : MonoBehaviour
HandleSteering();
ApplyBraking();
UpdateWheelPoses();
ApplyArcadeDrag();
}
private void HandleMotor()
@@ -87,10 +88,13 @@ public class CarController : MonoBehaviour
private void ApplyBraking()
{
frontLeftCollider.brakeTorque = currentBrakeForce;
frontRightCollider.brakeTorque = currentBrakeForce;
rearLeftCollider.brakeTorque = currentBrakeForce;
rearRightCollider.brakeTorque = currentBrakeForce;
float frontBrake = currentBrakeForce;
float rearBrake = currentBrakeForce * 0.4f;
frontLeftCollider.brakeTorque = frontBrake;
frontRightCollider.brakeTorque = frontBrake;
rearLeftCollider.brakeTorque = rearBrake;
rearRightCollider.brakeTorque = rearBrake;
}
private void UpdateWheelPoses()
@@ -101,6 +105,19 @@ public class CarController : MonoBehaviour
UpdateWheelPose(rearRightCollider, rearRightMesh);
}
private void ApplyArcadeDrag()
{
// Если нажали тормоз и машина едет быстрее 1 м/с
if (currentBrakeForce > 0 && rb.linearVelocity.magnitude > 1f)
{
rb.linearDamping = brakingDrag;
}
else
{
rb.linearDamping = defaultDrag;
}
}
private void UpdateWheelPose(WheelCollider collider, Transform mesh)
{
if (mesh == null) return;
+55
View File
@@ -0,0 +1,55 @@
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class EasySuspension : MonoBehaviour {
[Range(0, 20)]
public float naturalFrequency = 10;
[Range(0, 3)]
public float dampingRatio = 0.8f;
[Range(-1, 1)]
public float forceShift = 0.03f;
public bool setSuspensionDistance = true;
void Update () {
// work out the stiffness and damper parameters based on the better spring model
foreach (WheelCollider wc in GetComponentsInChildren<WheelCollider>()) {
JointSpring spring = wc.suspensionSpring;
spring.spring = Mathf.Pow(Mathf.Sqrt(wc.sprungMass) * naturalFrequency, 2);
spring.damper = 2 * dampingRatio * Mathf.Sqrt(spring.spring * wc.sprungMass);
wc.suspensionSpring = spring;
Vector3 wheelRelativeBody = transform.InverseTransformPoint(wc.transform.position);
float distance = GetComponent<Rigidbody>().centerOfMass.y - wheelRelativeBody.y + wc.radius;
wc.forceAppPointDistance = distance - forceShift;
// the following line makes sure the spring force at maximum droop is exactly zero
if (spring.targetPosition > 0 && setSuspensionDistance)
wc.suspensionDistance = wc.sprungMass * Physics.gravity.magnitude / (spring.targetPosition * spring.spring);
}
}
// uncomment OnGUI to observe how parameters change
/*
public void OnGUI()
{
foreach (WheelCollider wc in GetComponentsInChildren<WheelCollider>()) {
GUILayout.Label (string.Format("{0} sprung: {1}, k: {2}, d: {3}", wc.name, wc.sprungMass, wc.suspensionSpring.spring, wc.suspensionSpring.damper));
}
var rb = GetComponent<Rigidbody> ();
GUILayout.Label ("Inertia: " + rb.inertiaTensor);
GUILayout.Label ("Mass: " + rb.mass);
GUILayout.Label ("Center: " + rb.centerOfMass);
}
*/
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4d9eeaad380562f42bac8b304778252c
+13 -5
View File
@@ -6,7 +6,8 @@ public class VehicleInteraction : MonoBehaviour, IInteractable
[Header("Cameras & Setup")]
[SerializeField] private GameObject carCamera; // Камера от 3-го лица для авто
[SerializeField] private Transform exitPoint; // Точка, где спавнится игрок при выходе (возле левой двери)
[SerializeField] private UIManager uiManager;
private CarController carController;
private GameObject playerInstance;
private bool isPlayerInside = false;
@@ -41,6 +42,8 @@ public class VehicleInteraction : MonoBehaviour, IInteractable
// Включаем камеру машины
carCamera.SetActive(true);
if (uiManager != null) uiManager.ToggleDashboard(true);
}
private void ExitVehicle()
@@ -53,16 +56,16 @@ public class VehicleInteraction : MonoBehaviour, IInteractable
// Включаем камеру игрока и отключаем камеру машины
carCamera.SetActive(false);
if (uiManager != null) uiManager.ToggleDashboard(false);
CharacterController charController = playerInstance.GetComponent<CharacterController>();
if (charController != null)
{
charController.enabled = false;
}
// 2. Перемещаем игрока
playerInstance.transform.position = exitPoint.position;
// 3. Включаем CharacterController обратно
if (charController != null)
{
charController.enabled = true;
@@ -84,6 +87,11 @@ public class VehicleInteraction : MonoBehaviour, IInteractable
// Передаем управление в физику машины
carController.ProcessInput(vertical, horizontal, isBraking);
if (uiManager != null)
{
uiManager.UpdateSpeed(carController.SpeedKmH);
}
// Обработка выхода из машины
if (Input.GetKeyDown(KeyCode.E))
{