using UnityEngine;
using UnityEngine.InputSystem;

public class bikeController : MonoBehaviour
{
    private InputAction pedalInput;
    private InputAction steerInput;

    [SerializeField] private WheelCollider frontWheel;
    [SerializeField] private WheelCollider backWheel;

    [SerializeField] private float MotorForce;
    [SerializeField] private float BrakingForce;
    [SerializeField] private float steerSensitivity;

    [SerializeField] private float maxSteerAngle;

    private float pedalInp;
    private float steerInp;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        pedalInput = InputSystem.actions.FindAction("Pedal");
        steerInput = InputSystem.actions.FindAction("Steer");
    }

    // Update is called once per frame
    void Update()
    {
        pedalInp = pedalInput.ReadValue<float>();
        steerInp = steerInput.ReadValue<float>();
    }

    private void FixedUpdate()
    {
        frontWheel.motorTorque = pedalInp * MotorForce;
        backWheel.motorTorque = pedalInp * MotorForce;

        frontWheel.brakeTorque = 0;
        backWheel.brakeTorque = 0;

        if (pedalInp == 0)
        {
            frontWheel.brakeTorque = BrakingForce;
            backWheel.brakeTorque = BrakingForce;
        }

        frontWheel.steerAngle = maxSteerAngle * steerInp;
    }
}
