Мотоцикл в Unity
делаю мотоцикл в Unity, не прям реалистичный. Проблема в том, что при большой скорости его заносит
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class bike_controller : MonoBehaviour
{
//physx
Rigidbody rb;
[SerializeField] WheelCollider FW;
[SerializeField] WheelCollider RW;
//input
[SerializeField] float throttle;
[SerializeField] float steering;
//info
[SerializeField] float power;
[SerializeField] float speed;
[SerializeField] float acceleration;
[SerializeField] float incline;
[SerializeField] float brake;
[SerializeField] float slipLimit;
//other
[SerializeField] float turn_speed;
[SerializeField] bool isdrive, isbrake;
[SerializeField] Text _speed;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.mass = RW.motorTorque;
float steeringAngle = steering * .1f * speed / rb.velocity.magnitude;
FW.steerAngle = Mathf.Clamp(Mathf.Lerp(FW.steerAngle, steeringAngle, turn_speed), -15, 15);
if (isdrive)
{
float a = Mathf.Lerp(RW.motorTorque, throttle * power, acceleration);
FW.motorTorque = Mathf.Clamp(a, -20, speed);
}
if (isbrake)
{
FW.brakeTorque = brake * acceleration;
RW.brakeTorque = brake * acceleration;
}
else
{
FW.brakeTorque = 0; RW.brakeTorque = 0;
}
// Расчет коэффициента проскальзывания колес
float slip = Mathf.Abs(RW.rpm * RW.radius * Mathf.PI * 2 / 60 - rb.velocity.magnitude / RW.radius);
// Ограничение коэффициента проскальзывания для предотвращения заноса
if (slip > slipLimit)
{
RW.motorTorque *= 0.01f; // Уменьшение крутящего момента при проскальзывании
}
}
private void Update()
{
throttle = Input.GetAxis("Vertical");
steering = Input.GetAxis("Horizontal");
isdrive = throttle != 0 | steering != 0 ? true : false;
isbrake = Input.GetAxis("Jump") != 0 ? true: false;
_speed.text = "speed: " + RW.motorTorque.ToString() + "\n" + "speed rb: " + rb.velocity.magnitude.ToString();
}
}