Search Unity

Question Unicycle Physics.

Discussion in 'Physics' started by Some-Yahoo, Apr 19, 2022.

  1. Some-Yahoo

    Some-Yahoo

    Joined:
    Jun 27, 2013
    Posts:
    26
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public bool EngineRunning = false;
    8.  
    9.     public float RotationMult = 0.001f;
    10.     public float SpeedMult = 0.1f;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     //the transform here is the bike body, not the wheel
    21.     void Update()
    22.     {
    23.         float rightleft = Input.GetAxis("Horizontal");
    24.         float updown = Input.GetAxis("Vertical");
    25.         bool JumpKeyDown = Input.GetKey(KeyCode.Space);
    26.  
    27.         Rigidbody bike = gameObject.GetComponent<Rigidbody>();
    28.         //if (Mathf.Abs(rightleft)>0.01)
    29.         {
    30.             bike.AddRelativeTorque(transform.up * RotationMult * rightleft * Time.deltaTime);
    31.         }
    32.  
    33.         //if (Mathf.Abs(updown)>0.1)
    34.         {
    35.             bike.AddRelativeForce(transform.forward * SpeedMult * updown * Time.deltaTime);
    36.         }
    37.     }
    38. }

    Capture1.JPG Capturewheel.JPG
     
    the_nem_s likes this.
  2. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    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
     
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    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.
     
    Last edited: Apr 19, 2022
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    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.
     
    alienorbit likes this.