Question by
CyberModeSoftware · Dec 20, 2020 at 07:43 PM ·
physicscarmotortorque
Why the car gears dosen't work.
I have a big problem with my car script , the gears is not working . I want a automatic gearbox but my function who change gears dosent want to update the gears just if I call in in the update function. But when I call in in the update the gear jump to 4th gear not in to 2nd.(he jump at the last gear from array)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CarController : MonoBehaviour
{
public AnimationCurve EnginePower;
public Button upshift;
public steeringwheel2 Steering;
public GameManager Manager;
public Slider slider;
public Rigidbody car_rb;
public double currentspeed;
public int[] gearSpeeds = new int[] { 40, 80, 120, 160, 220 };
public float[] gears;
public int gear = 1;
[HideInInspector] public bool reverse = false;
public float m_horizontalInput;
public float m_verticalInput;
public bool isbrake = false;
public float DownForceValue = 10f;
public float wheelsRPM;
public float totalpower;
public float maxRPM;
public float engineRPM;
public void Steer()
{
m_steeringAngle = Steering.GetAngle() / maxSteerAngle;
frontDriverW.steerAngle = m_steeringAngle;
frontPassengerW.steerAngle = m_steeringAngle;
}
public void Accelerate()
{
if (currentspeed <= 220)
{
if (m_verticalInput == 1)
{
frontDriverW.motorTorque = totalpower;
frontPassengerW.motorTorque = totalpower;
rearDriverW.motorTorque = totalpower;
rearPassengerW.motorTorque = totalpower;
}
else if (m_verticalInput == 0)
{
frontDriverW.motorTorque = 0;
frontPassengerW.motorTorque = 0;
rearDriverW.motorTorque = 0;
rearPassengerW.motorTorque = 0;
}
}
}
public void If()
{
if (m_verticalInput == -1)
{
Brake();
}
else if (m_verticalInput == 0)
{
NotBrake();
}
}
public void Brake()
{
frontDriverW.brakeTorque = brakePower;
frontPassengerW.brakeTorque = brakePower;
rearDriverW.brakeTorque = brakePower;
rearPassengerW.brakeTorque = brakePower;
}
public void NotBrake()
{
frontDriverW.brakeTorque = 0;
frontPassengerW.brakeTorque = 0;
rearDriverW.brakeTorque = 0;
rearPassengerW.brakeTorque = 0;
}
public void shifter()
{
if (engineRPM > maxRPM && gear < gears.Length - 1)
{
gear++;
}
}
private void UpdateWheelPoses()
{
UpdateWheelPose(frontDriverW, frontDriverT);
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);
_transform.position = _pos;
_transform.rotation = _quat;
}
public void Calculatingenginepower()
{
WheelRpm();
float velocity = 0.0f;
totalpower = EnginePower.Evaluate(engineRPM) * (gears[gear]);
engineRPM = Mathf.SmoothDamp(engineRPM, 1000 + (Mathf.Abs(wheelsRPM) * 3.6f * (gears[gear])), ref velocity, 0.09f);
}
public void WheelRpm()
{
wheelsRPM = frontDriverW.rpm + frontPassengerW.rpm + rearDriverW.rpm + rearPassengerW.rpm / 4;
}
public void DownForce()
{
car_rb.AddForce(-transform.up * DownForceValue * car_rb.velocity.magnitude);
}
public void Update()
{
Steer();
UpdateWheelPoses();
Accelerate();
Calculatingenginepower();
DownForce();
WheelRpm();
currentspeed = car_rb.velocity.magnitude * 3.6;
}
private float m_steeringAngle;
public WheelCollider frontDriverW, frontPassengerW;
public WheelCollider rearDriverW, rearPassengerW;
public Transform frontDriverT, frontPassengerT;
public Transform rearDriverT, rearPassengerT;
public float maxSteerAngle = 30;
public float motorForce = 500;
public float brakePower = 100000;
}
Comment
Your answer
Follow this Question
Related Questions
Car steering on a Rigidbody (Mario Kart) 0 Answers
How to make physics for rail trolley with gravity changing. Unity 2d game 0 Answers
Help with drift physics 0 Answers
How to make handbrake turn or powerslide with wheel colliders? 0 Answers
About Car Physics 0 Answers