Я делал небольшую сцену для проекта в институте. Я пытался реализовать шестеренки, но не смог. Откатил все назад, как мог, но теперь машина ведет себя странно. Если нажать на газ, он ускорится, даже если вы его уже отпустили. Также есть заметная задержка перед торможением или запуском двигателя.
Самое интересное, что машина едет автоматически, если удерживать кнопку газа примерно до тех пор, пока я ее держу.
public class Car : MonoBehaviour
{
[SerializeField] private WheelCollider[] _frontWheels;
[SerializeField] private WheelCollider[] _rearWheels;
[SerializeField] private TextMeshProUGUI textt;
[SerializeField] private float _maxMotorFroce = 1000;
[SerializeField] private float _maxSteeringAngle = 30;
[SerializeField] private float _brakeForce;
[SerializeField] private float _RRbrakeForce;
[SerializeField] private float silaTormoz;
[SerializeField] private float silaRuchnik;
[SerializeField] private float tormozeniya;
[SerializeField] private Rigidbody rg;
[SerializeField] private GameObject[] points;
private float motorInput;
private float steeringInput;
private float brakeInput;
private void Start()
{
rg = gameObject.GetComponent();
}
private void Update()
{
float bb = rg.velocity.magnitude * 3.6f;
textt.text = bb.ToString();
steeringInput = Input.GetAxis("Horizontal") * _maxSteeringAngle;
Vector3 glawa = points[0].transform.position + rg.velocity;
Vector3 biba = points[1].transform.position - glawa;
Vector3 boba = points[2].transform.position - glawa;
if (biba.magnitude < boba.magnitude)
{
motorInput = Input.GetKey(KeyCode.W) ? _maxMotorFroce : 0;
brakeInput = Input.GetKey(KeyCode.S) ? _brakeForce : 0;
}
else if (biba.magnitude > boba.magnitude)
{
motorInput = Input.GetKey(KeyCode.S) ? _maxMotorFroce * -1 : 0;
brakeInput = Input.GetKey(KeyCode.W) ? _brakeForce : 0;
}
else
{
motorInput = Input.GetKey(KeyCode.W) ? _maxMotorFroce : 0;
motorInput = Input.GetKey(KeyCode.S) ? _maxMotorFroce * -1 : 0;
}
if (Input.GetKeyDown(KeyCode.Space))
{
for (int i = 0; i < _rearWheels.Length; i++)
{
WheelFrictionCurve sidewaysCurve = _rearWheels.sidewaysFriction;
WheelFrictionCurve sggge = _rearWheels.forwardFriction;
sidewaysCurve.stiffness /= 2;
sggge.stiffness /= 2;
_rearWheels.forwardFriction = sggge;
_rearWheels.sidewaysFriction = sidewaysCurve;
Debug.Log("Пробел нажат!");
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
for (int i = 0; i < _rearWheels.Length; i++)
{
WheelFrictionCurve sidewaysCurve = _rearWheels.sidewaysFriction;
WheelFrictionCurve sggge = _rearWheels.forwardFriction;
sidewaysCurve.stiffness *= 2;
sggge.stiffness *= 2;
_rearWheels.forwardFriction = sggge;
_rearWheels.sidewaysFriction = sidewaysCurve;
Debug.Log("Пробел отпущен!");
}
}
}
private void FixedUpdate()
{
steeringInput = Input.GetAxis("Horizontal") * _maxSteeringAngle;
for (int i = 0; i < _frontWheels.Length; i++)
{
_frontWheels.steerAngle = steeringInput;
_frontWheels.brakeTorque = brakeInput;
UpdateWheelPose(_frontWheels[i]);
}
for (int i = 0; i < _rearWheels.Length; i++)
{
_rearWheels[i].motorTorque = motorInput;
_rearWheels[i].brakeTorque = brakeInput;
if (motorInput == 0 && brakeInput == 0)
{
_rearWheels[i].brakeTorque = 200f;
}
UpdateWheelPose(_rearWheels[i]);
}
}
private void UpdateWheelPose(WheelCollider wheel)
{
if (wheel.transform.childCount == 0) return;
Transform child = wheel.transform.GetChild(0);
wheel.GetWorldPose(out Vector3 position, out Quaternion rotation);
child.position = position;
child.rotation = rotation;
}
}