- Home /
Question by
ultimategamersultimategamers · Jun 09, 2020 at 08:53 PM ·
unity 5carcar gamecar physicscars
Car Physics
Hello, i recently started new project, and have some problems with my car physics. I was searching the internet all day and found this script which i use in my game. I have some stutters while driving, and most important bug is that whatever float i set speed to be, it is approximately always the same, can't go faster. Any tips? Here's video from my problem: https://www.youtube.com/watch?v=iaUgNzMOpIM
public class BMW_i8 : MonoBehaviour
{
private float m_horizontalInput; // A & D
private float m_verticalInput; // W & S
private float m_steeringAngle; // Modified by m_horizontalInput
public WheelCollider frontDriverW, frontPassengerW; //W = Wheel
public WheelCollider rearDriverW, rearPassengerW;
public Transform frontDriverT, frontPassengerT; //T = Transform
public Transform rearDriverT, rearPassengerT;
public float maxSteerAngle = 30; // 30 Degrees Max Steering Angle
public float motorForce = 50; // Motor Torque Variable
////////////////////////////////////////////////////////////////////////////////////////////////////
public void GetInput()
{
m_horizontalInput = Input.GetAxis("Horizontal"); //Unity Input Method (Edit > Project Settings > Input)
m_verticalInput = Input.GetAxis("Vertical");
}
private void Steer()
{
m_steeringAngle = maxSteerAngle * m_horizontalInput;
frontDriverW.steerAngle = m_steeringAngle;
frontPassengerW.steerAngle = m_steeringAngle;
}
private void Accelerate()
{
frontDriverW.motorTorque = m_verticalInput * motorForce;
frontPassengerW.motorTorque = m_verticalInput * motorForce;
}
private void UpdateWheelPoses()
{
UpdateWheelPose(frontDriverW, frontDriverT); // Taking Pose Information from _collider and Applying it to the _transform
UpdateWheelPose(frontPassengerW, frontPassengerT);
UpdateWheelPose(rearDriverW, rearDriverT);
UpdateWheelPose(rearPassengerW, rearPassengerT);
}
private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
{
Vector3 _pos = _transform.position;
Quaternion _quat = _transform.rotation;
_collider.GetWorldPose(out _pos, out _quat); // Out Values
_transform.position = _pos;
_transform.rotation = _quat;
}
private void FixedUpdate()
{
GetInput();
Steer();
Accelerate();
UpdateWheelPoses();
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Tilting car on two wheels 1 Answer
transmission clutch help! 1 Answer
Engine animation curve 1 Answer
Extremely simple car movement 1 Answer