VPS36official
VPS36official

Reputation: 3

bike movement feels like the bike's on ice when turning

whenever I turn my bike it feels like it's sliding. Here's the script

    [SerializeField] float turn;
    [SerializeField] float maxSpeed;
    [SerializeField] GameObject bikeParts;
    [SerializeField] float tilt = 20f;
 
    Rigidbody rb;
    float lastY;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        float straightMov = Input.GetAxis("Vertical");
        float horizontalMov = Input.GetAxis("Horizontal");


        ControlStraightMov(straightMov);
        ControlWeightTurning(horizontalMov);

        lastY = transform.position.y;
    }

    private void ControlWeightTurning(float horizontalMov)
    {
        Vector3 bikePartsRot = bikeParts.transform.eulerAngles;
        bikePartsRot.x = tilt * horizontalMov;
        bikeParts.transform.eulerAngles = bikePartsRot;

        transform.Rotate(0f, (turn * Time.deltaTime) * horizontalMov, 0f);
    }

    private void ControlStraightMov(float straightMov)
    {
        if (straightMov > 0)
        {
            rb.AddRelativeForce((accel * Time.deltaTime) * -straightMov, 0f, 0f);
        }
        else if (straightMov < 0)
        {
            rb.AddRelativeForce(((accel / 1.3f * Time.deltaTime) * -straightMov), 0f, 0f);
        }
    }

Whenever the bike picks up speed, it becomes very difficult to turn the bike because the force that was added keeps moving the bike in a different direction than the direction the bike's facing, how do I apply that force it was facing before on the direction it is facing now so that it doesn't feel like sliding?

I have tried fixing this problem but i just can't because i am pretty new to unity.

Upvotes: 0

Views: 178

Answers (1)

derHugo
derHugo

Reputation: 90724

how do I apply that force it was facing before on the direction it is facing now

Not sure if this would feel right but you could probably do it like e.g.

private void Update ()
{
    ...

    rb.velocity = transform.forward * rb.velocity.magnitude;
}

Which will keep the current speed and just redirect it into the new forward direction.


In general note: Whenever there is a Rigidbody involved you don't want to set any manipulation through the Transform component but only via the Rigidbody.

So you should not do

bikeParts.transform.eulerAngles = bikePartsRot;

transform.Rotate(0f, (turn * Time.deltaTime) * horizontalMov, 0f);

but both times rather calculate the target rotation and go through e.g. rb.MoveRotation in FixedUpdate.

Which might look somewhat like

float horizontalMove;

void Update()
{
    var straightMove = Input.GetAxis("Vertical");
    // Get and store the input in Update
    horizontalMove = Input.GetAxis("Horizontal");

    ControlStraightMov(straightMove);
    
    lastY = transform.position.y;
}

private void FixedUpdate ()
{
    // Handle it in FixedUpdate
    ControlWeightTurning(horizontalMove);
}

private void ControlWeightTurning(float horizontalMove)
{
    var bikePartsRot = bikeParts.transform.eulerAngles;
    bikePartsRot.x = tilt * horizontalMove;

    // Calculate the new final rotation
    var newRotation = Quaternion.Euler(bikePartsRot) * Quaternion.Euler(Vector3.up * (turn * Time.deltaTime) * horizontalMove));

    // Apply it only via the Rigidbody
    rb.MoveRotation(newRotation);
}

Upvotes: 1

Related Questions