Помогите с кодом C#
хочу сделать дрифт для тачки, но при взаимодействии с поверхностью, начинает очень быстро крутиться, на 4 колесах Wheel Collider. Unity 2021 года
скрипт машины:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DRIFT : MonoBehaviour
{
[Header("Основные настройки")]
public float двигательМощность = 200f;
public float тормозУсилие = 100f;
public float сцеплениеШины = 5f;
public float уголМаксРуля = 30f;
[Header("Настройки дрифта")]
public float уголСносаДляДрифта = 15f;
public float усилениеДрифта = 1.5f;
public float восстановлениеСцепления = 0.5f;
private Rigidbody rb;
private WheelHit[] колеса = new WheelHit[4];
private WheelCollider[] wheelColliders = new WheelCollider[4];
void Start()
{
rb = GetComponent<Rigidbody>();
wheelColliders = GetComponentsInChildren<WheelCollider>();
if (wheelColliders.Length != 4)
{
Debug.LogError("Необходимо 4 WheelCollider'а на этом объекте!");
}
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
ApplyEngineForce(verticalInput);
ApplyBrakes(verticalInput);
Steer(horizontalInput);
HandleDrifting(horizontalInput);
}
void ApplyEngineForce(float verticalInput)
{
float engineForce = verticalInput * двигательМощность;
foreach (WheelCollider wheelCollider in wheelColliders)
{
if (verticalInput > 0)
{
wheelCollider.motorTorque = engineForce;
wheelCollider.brakeTorque = 0;
}
else
{
wheelCollider.motorTorque = 0;
}
}
}
void ApplyBrakes(float verticalInput)
{
float brakeForce = 0;
if (verticalInput < 0)
{
brakeForce = -verticalInput * тормозУсилие;
}
foreach (WheelCollider wheelCollider in wheelColliders)
{
wheelCollider.brakeTorque = brakeForce;
}
}
void Steer(float horizontalInput)
{
foreach (WheelCollider wheelCollider in wheelColliders)
{
if (wheelCollider.transform.localPosition.x > 0)
{
wheelCollider.steerAngle = horizontalInput * уголМаксРуля;
}
}
}
void HandleDrifting(float horizontalInput)
{
for (int i = 0; i < wheelColliders.Length; i++)
{
WheelCollider wheelCollider = wheelColliders[i];
wheelCollider.GetGroundHit(out колеса[i]);
float уголСноса = Vector3.Angle(transform.forward, колеса[i].sidewaysDir);
if (уголСноса > уголСносаДляДрифта)
{
float сцепление = Mathf.Lerp(колеса[i].forwardSlip, сцеплениеШины, Time.deltaTime * восстановлениеСцепления);
wheelCollider.forwardFriction = ModifyFriction(wheelCollider.forwardFriction, сцепление * усилениеДрифта);
wheelCollider.sidewaysFriction = ModifyFriction(wheelCollider.sidewaysFriction, сцепление * усилениеДрифта);
Debug.Log("Дрифт");
}
else
{
float сцепление = Mathf.Lerp(колеса[i].forwardSlip, сцеплениеШины, Time.deltaTime * восстановлениеСцепления);
wheelCollider.forwardFriction = ModifyFriction(wheelCollider.forwardFriction, сцепление);
wheelCollider.sidewaysFriction = ModifyFriction(wheelCollider.sidewaysFriction, сцепление);
Debug.Log("Обычное сцепление");
}
}
}
WheelFrictionCurve ModifyFriction(WheelFrictionCurve original, float stiffness)
{
original.stiffness = stiffness;
return original;
}
}
скрипт колес:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KOLESA : MonoBehaviour
{
public WheelCollider targetWheel;
private Transform visualWheel;
void Start()
{
visualWheel = transform;
}
void Update()
{
Vector3 position;
Quaternion rotation;
targetWheel.GetWorldPose(out position, out rotation);
visualWheel.position = position;
visualWheel.rotation = rotation;
}
}
МОЙ СКРИПТ
using UnityEngine;
public class DRIFT : MonoBehaviour
{
[Header("Основные настройки")]
public float двигательМощность = 200f;
public float тормозУсилие = 100f;
public float сцеплениеШины = 5f;
public float уголМаксРуля = 30f;
[Header("Настройки дрифта")]
public float уголСносаДляДрифта = 15f;
public float усилениеДрифта = 1.5f;
public float восстановлениеСцепления = 0.5f;
private Rigidbody rb;
private WheelCollider[] wheelColliders;
private WheelHit[] колеса = new WheelHit[4];
void Start()
{
rb = GetComponent<Rigidbody>();
wheelColliders = GetComponentsInChildren<WheelCollider>();
if (wheelColliders.Length != 4)
{
Debug.LogError("Необходимо 4 WheelCollider'а на этом объекте!");
}
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
ApplyEngineForce(verticalInput);
ApplyBrakes(verticalInput);
Steer(horizontalInput);
HandleDrifting(horizontalInput);
}
void ApplyEngineForce(float verticalInput)
{
float engineForce = verticalInput * двигательМощность;
foreach (WheelCollider wheelCollider in wheelColliders)
{
if (verticalInput > 0)
{
wheelCollider.motorTorque = engineForce;
wheelCollider.brakeTorque = 0;
}
else
{
wheelCollider.motorTorque = 0;
}
}
}
void ApplyBrakes(float verticalInput)
{
float brakeForce = 0;
if (verticalInput < 0)
{
brakeForce = -verticalInput * тормозУсилие;
}
foreach (WheelCollider wheelCollider in wheelColliders)
{
wheelCollider.brakeTorque = brakeForce;
}
}
void Steer(float horizontalInput)
{
foreach (WheelCollider wheelCollider in wheelColliders)
{
// Проверяем, что колесо переднее (по X-координате)
if (Mathf.Abs(wheelCollider.transform.localPosition.x) > 0.1f)
{
wheelCollider.steerAngle = horizontalInput * уголМаксРуля;
}
}
}
void HandleDrifting(float horizontalInput)
{
for (int i = 0; i < wheelColliders.Length; i++)
{
WheelCollider wheelCollider = wheelColliders[i];
if (wheelCollider.GetGroundHit(out колеса[i]))
{
// Угол между направлением движения и вектором скольжения
float уголСноса = Vector3.Angle(transform.forward, колеса[i].sidewaysDir);
if (уголСноса > уголСносаДляДрифта)
{
// Режим дрифта: снижаем сцепление
float сцепление = сцеплениеШины * усилениеДрифта;
wheelCollider.forwardFriction = ModifyFriction(wheelCollider.forwardFriction, сцепление);
wheelCollider.sidewaysFriction = ModifyFriction(wheelCollider.sidewaysFriction, сцепление);
}
else
{
// Нормальное сцепление
wheelCollider.forwardFriction = ModifyFriction(wheelCollider.forwardFriction, сцеплениеШины);
wheelCollider.sidewaysFriction = ModifyFriction(wheelCollider.sidewaysFriction, сцеплениеШины);
}
}
}
}
WheelFrictionCurve ModifyFriction(WheelFrictionCurve original, float stiffness)
{
original.stiffness = stiffness;
return original;
}
}