- Home /
Question by
Daivik110 · Nov 02, 2020 at 10:03 AM ·
car gamecar physicscar racecar tutorial
How add kilometre per hour system in car script,how to add kilometre per hour speed system in script
I want my car to move 0 KPH TO 60 KPH ( when i press "W" the speed increase 0 to 60 KPH) and after reaching 60 KPH car continues to move on 60 KPH my script is this
public class Carcontroller3 : MonoBehaviour { [SerializeField] private WheelCollider FrontLeftWheelC; [SerializeField] private WheelCollider FrontRightWheelC; [SerializeField] private WheelCollider RearLeftWheelC; [SerializeField] private WheelCollider RearRightWheelC;
[SerializeField] private Transform FrontLeftWheelT;
[SerializeField] private Transform FrontRightWheelT;
[SerializeField] private Transform RearLeftWheelT;
[SerializeField] private Transform RearRightWheelT;
private float currentSteerAngle;
private const string HORIZONTAL = "Horizontal";
private const string VERTICAL = "Vertical";
[SerializeField] private float maxTorque = 200f;
[SerializeField] private float SteerAngle = 30f;
[SerializeField] private float Speedinkph = 100;
private float horizontalInput;
private float verticalInput;
public void FixedUpdate()
{
GetInput();
Acceleration();
HandleSteering();
UpdateWheel();
}
public void GetInput()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
}
public void Acceleration()
{
RearLeftWheelC.motorTorque = verticalInput * maxTorque;
RearRightWheelC.motorTorque = verticalInput * maxTorque;
}
public void HandleSteering()
{
currentSteerAngle = SteerAngle * horizontalInput;
FrontLeftWheelC.steerAngle = currentSteerAngle;
FrontRightWheelC.steerAngle = currentSteerAngle;
}
public void UpdateWheel()
{
UpdateSingleWheel(FrontLeftWheelC, FrontLeftWheelT);
UpdateSingleWheel(FrontRightWheelC, FrontRightWheelT);
UpdateSingleWheel(RearLeftWheelC, RearLeftWheelT);
UpdateSingleWheel(RearRightWheelC, RearRightWheelT);
}
public void UpdateSingleWheel(WheelCollider WheelCollider, Transform WheelTransform)
{
Vector3 pos;
Quaternion rot;
WheelCollider.GetWorldPose (out pos, out rot );
WheelTransform.position = pos;
WheelTransform.rotation = rot;
}
}
please help me out
Comment
Your answer
Follow this Question
Related Questions
Rotate car steer angle towards target direction 0 Answers
Destroyed car styled need for speed? 0 Answers
drift car using rigidbody 1 Answer
how to rotate car to camera direction using wheel colliders. 1 Answer
Tilting car on two wheels 1 Answer