Над физикой работаю. Добаил UI минимальное
This commit is contained in:
1 parent
48753f65fb
commit
f9ec01d1c8
912 files changed
+204440
-3555
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user