Keep Bicycle up right. Wheel colliders.

Hey guys!

New to coding here but im trying to learn. I’ve made a basic script that allows a bike to move around and turn using wheel colliders, and aslong as the z axis is locked on rotation it works until it slowly tips over. for instance if you spin left more that like 4 times in a row it will tip over. So i was thinking if i wrote another script that reads the euler angle z of the bike, if it’s greater than say 15 degrees it locks the angle at 15 till the player releases the key then it moves it back to 0 degrees. Would this work? and if it would, How would i go about it?

you could do an add force when the angle is grater then x.
this code an example. it will most likely not work for your scene.

public float speed = 0.5f;
if(rotation.z > 15)
{
rb.AddTorque(transform.up * speed);
}

Gonna go ahead and try this out now. Didn’t know that rotation.z was a thing i could use! haha only further proving my noobiness. Cheers mate.

you have transform.rotation .x .y and .z
you may also need to check localRotation

I’m gonna assume i have to check local rotation cause im still tipping over. Off to learn how to do that! haha

post a piece of the code so i see what you’re doing

//balancing
        if(GetComponent<Rigidbody>().transform.rotation.z > 15)
        //(transform.rotation.z > 15)
        {
            GetComponent<Rigidbody>().AddForce(transform.up * speed);
            //rb.AddTorque(transform.up * speed);
        }
        if(GetComponent<Rigidbody>().transform.rotation.z < -15)
        //(transform.rotation.z < -15)
        {
            GetComponent<Rigidbody>().AddTorque(transform.up * speed);
            //rb.AddTorque(transform.up * speed);
        }

ok, here is the next thing to play with.
transform.up is just a vector 3. but i always forget witch way it adds the force.
and add a debug so you can tell if the if statements runs. and when you are testing the game. you can adjust the speed on the fly. you may need a higher speed value.

using UnityEngine;
using System.Collections;

public class bike : MonoBehaviour {

    public float speed = 5;
   
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (GetComponent<Rigidbody>().transform.rotation.z > 15)
        {
            Debug.Log("If 1 is true");
            //GetComponent<Rigidbody>().AddForce(transform.up * speed);
            GetComponent<Rigidbody>().AddForce(new Vector3(0,0,1) * speed);
        }

        if (GetComponent<Rigidbody>().transform.rotation.z < -15)
        {
            Debug.Log("If 2 is true");
            //GetComponent<Rigidbody>().AddTorque(transform.up * speed);
            GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, -1) * speed);
        }
    }
}

So our problem seems to sit with the if statement becoming true. the debug log dosent print anything out when i tip so im assuming that its not reading the transform rotation right…Could it be because it has child objects?

after alittle bit of thinking i was looking at my two wheel coliders as i tipped the bike and i noticed that there z rotation does not change when i tip. Could that be why it does not set the if as true?

yes, that would be why the if is not true

mm…So for some reason it only prints out true if the angle is set at 0 haha. But now when its set at 0 it just throws it back and forth a few times till it eventually gets thrown over the other direction. I’m gonna keep playing around though and ill post again if i come up with anything productive haha. Cheers for the help mate.