First of all sorry for my bad English.
I am making a car but it accelerates as a train but the top speed is infinite.
Does anyone know how I can give it limits and boost the acceleration? I tried playing with motor force and adding/reducing mass.
This is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
private float m_horizontalInput;
private float m_verticalInput;
private float m_steeringAngle;
public WheelCollider FrontD;
public WheelCollider RearD;
public WheelCollider FrontP;
public WheelCollider RearP;
public Transform FrontDT;
public Transform RearDT;
public Transform FrontPT;
public Transform RearPT;
public float maxSteerAngle = 30;
public float motorForce = 50;
public void GetInput(){
m_horizontalInput = Input.GetAxis("Horizontal");
m_verticalInput = Input.GetAxis("Vertical");
}
private void Steer(){
m_steeringAngle = maxSteerAngle * m_horizontalInput;
FrontD.steerAngle = m_steeringAngle;
FrontP.steerAngle = m_steeringAngle;
}
private void Accelerate(){
RearD.motorTorque = m_verticalInput * motorForce;
RearP.motorTorque = m_verticalInput * motorForce;
}
private void UpdateWheelPoses(){
UpdateWheelPose(FrontD, FrontDT);
UpdateWheelPose(RearD, RearDT);
UpdateWheelPose(FrontP, FrontPT);
UpdateWheelPose(RearP, RearPT);
}
private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
{
Vector3 _pos = _transform.position;
Quaternion _quat = _transform.rotation;
_collider.GetWorldPose(out _pos, out _quat);
_transform.position = _pos;
_transform.rotation = _quat;
}
private void FixedUpdate(){
GetInput();
Steer();
Accelerate();
UpdateWheelPoses();
}
}
This is what the inspector looks like
