Unicycle Physics.

have a unicycle, got a collider and rigidbody on the body, and using a wheel collider on the wheel.

Trying to find that delicate balance where I can drive the thing.

So far NOTHING I do can make it go forward or back, and any attempt to turn it results in the whole bike flipping all over the level like it was in a giant blender.

Here’s my code trying to drive the thing.

The thing is: I’m not sure if “ApplyRelativeTorque” is instantaneous or if it turns on some kind of thruster that i have to go back and turn off.

Also not sure if I should be “pushing” the bike or “rotating” the wheel.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public bool EngineRunning = false;

    public float RotationMult = 0.001f;
    public float SpeedMult = 0.1f;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    //the transform here is the bike body, not the wheel
    void Update()
    {
        float rightleft = Input.GetAxis("Horizontal");
        float updown = Input.GetAxis("Vertical");
        bool JumpKeyDown = Input.GetKey(KeyCode.Space);

        Rigidbody bike = gameObject.GetComponent<Rigidbody>();
        //if (Mathf.Abs(rightleft)>0.01)
        {
            bike.AddRelativeTorque(transform.up * RotationMult * rightleft * Time.deltaTime);
        }

        //if (Mathf.Abs(updown)>0.1)
        {
            bike.AddRelativeForce(transform.forward * SpeedMult * updown * Time.deltaTime);
        }
    }
}

1 Like

Rigidbody’s AddForce/Torque/etc accumulates forces until the next simulation, and then applies them for just one frame. So, if you need to add a continuous force, it has to be AddForce’d every physics frame (=FixedUpdate/Physics.Simulate)

anthony

Personally I would not try to model a unicycle purely with physics as there are several complications to the system that are going to make life very awkward:

  1. The gyroscopic effect of the rotating wheel is significant to the stability and not included for free in PhysX.

  2. The rider’s ability to subtly shift and control their centre of gravity to manoeuvre the unicycle is a complex control problem (you could choose to approach this with some mixture of PID controllers, but I suspect it will not be easy).

  3. The PhysX wheel collider is not developed to work like a bicycle / motorcycle / unicycle requirements with camber angles and lateral loading.

If you are using a wheel collider you should be driving it with motor torque, not AddRelativeTorque.

More realistic options would probably be to either treat the physics as a small 4 wheeled car or a hoverboard (there are tutorials around for these); then just manipulate the visualisation to appear as a unicycle.

If you don’t need it to be purely realistic, try setting the center of mass of the Rigidbody below the ground, something like rigidbody.centerOfMass = new Vector3(0, -2, 0) where “-2” is enough to fall from the origin of your object to at least 1m below the ground.

This will give the unicycle instant equilibrium, and even fun and pseudo-realistic motion when applying forces. Apply the forces at the contact point of the wheel with the ground (WheelHit.point). This way, when applying a forward force the unicycle will actually lean forwards.

1 Like