- Home /
Question by
eni_cay · Dec 20, 2020 at 08:08 PM ·
vehiclecar physicssteeringcar-tutorialwheel colliders
car accelerates too slow ?
trying to make a car game , i followed a tutorial , and my implementation wasn't the same as in the tutorial , like my car moves too slow and takes to move in reverse it also doesn't steer , any of you had this problem before or have any solution or proper tutorial to follow ?i have the new unity ,heres the code : private const string HORIZONTAL = "Horizontal"; private const string VERTICAL = "Vertical";
private float horizontalinput;
private float verticalinput;
private float steerangle;
private bool IsBreaking;
private float currentbreakforce;
[SerializeField] private float motorforce;
[SerializeField] private float breakforce;
[SerializeField] private float maxsteeringangle;
[SerializeField] private WheelCollider frontleftwheelcollider;
[SerializeField] private WheelCollider backleftwheelcollider;
[SerializeField] private WheelCollider frontrightwheelcollider;
[SerializeField] private WheelCollider backrightwheelcollider;
[SerializeField] private Transform frontleftwheeltransform;
[SerializeField] private Transform backleftwheeltransform;
[SerializeField] private Transform frontrightwheeltransform;
[SerializeField] private Transform backrightwheeltransform;
private void FixedUpdate()
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void UpdateWheels()
{
UpdateSingleWheel(frontleftwheelcollider, frontleftwheeltransform);
UpdateSingleWheel(frontrightwheelcollider, frontrightwheeltransform);
UpdateSingleWheel(backleftwheelcollider, backleftwheeltransform);
UpdateSingleWheel(backrightwheelcollider, backrightwheeltransform);
}
private void UpdateSingleWheel(WheelCollider wheelcollider, Transform wheeltransform)
{
Vector3 pos;
Quaternion rot;
wheelcollider.GetWorldPose(out pos, out rot);
wheeltransform.rotation = rot;
wheeltransform.position = pos;
}
private void HandleSteering()
{
steerangle = maxsteeringangle * horizontalinput;
}
private void HandleMotor()
{
frontleftwheelcollider.motorTorque = verticalinput * motorforce;
frontrightwheelcollider.motorTorque = verticalinput * motorforce;
currentbreakforce = IsBreaking ? breakforce : 0f;
if (IsBreaking)
{
Applybreaking();
}
}
private void Applybreaking()
{
frontleftwheelcollider.brakeTorque = currentbreakforce;
backleftwheelcollider.brakeTorque = currentbreakforce;
frontrightwheelcollider.brakeTorque = currentbreakforce;
backrightwheelcollider.brakeTorque = currentbreakforce;
}
private void GetInput()
{
horizontalinput = Input.GetAxis(HORIZONTAL);
verticalinput = Input.GetAxis(VERTICAL);
IsBreaking = Input.GetKey(KeyCode.Space);
}
Comment
Your answer
Follow this Question
Related Questions
How can I set up a tank steering mechanism? 2 Answers
Car turorial reset vehicle 2 Answers
simple drift car movement 0 Answers
Problem with cars in Unity 5 0 Answers
Steering an AI vehicle to a waypoint using float of -1 to 1? 1 Answer