Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Motorcycle controller with hinge joints

Discussion in 'Physics' started by fierarux777, Nov 14, 2021.

  1. fierarux777

    fierarux777

    Joined:
    Feb 11, 2019
    Posts:
    35
    Hello!
    I am trying to make a motorcycle controller using hinje joints since they are easier to setup as wheels then the wheel collider. But the problem is is this: since my vehicle has only 2 wheels, it doesnt have enought balance to the sides and I dont know what to do to keep it balanced while allowing a degree of Z axis turning. And then comes another problem: since I'm using the hinje joint and rigidbodyes, the steering is difficult to make. I also use forces to move the motorcycle.

    Here is the script:

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class applyForce : MonoBehaviour
    8. {
    9.     public Rigidbody rb1;
    10.     public Rigidbody rb2;
    11.     public Camera cam;
    12.     public Vector3 offset;
    13.     Rigidbody rb;
    14.     public float power;
    15.     bool parkingDrag = false;
    16.     public float steerSpeedInDegrees_per_Sec;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.        /* rb1 = GetComponent<Rigidbody>();
    21.         rb2 = GetComponent<Rigidbody>();*/
    22.  
    23.         rb=GetComponent<Rigidbody>();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         cam.transform.position = transform.position + offset;
    30.     }
    31.  
    32.     void FixedUpdate()
    33.     {
    34.  
    35.         /*if(Input.GetKey(KeyCode.W))
    36.         {
    37.             rb2.AddForce(Vector3.forward * power, ForceMode.Acceleration);
    38.         }
    39.         if(Input.GetKey(KeyCode.S))
    40.         {
    41.             rb1.AddForce(Vector3.back * power, ForceMode.Acceleration);
    42.         }*/
    43.  
    44.         if(Input.GetKey(KeyCode.W))
    45.         {
    46.             rb.AddForce(Vector3.forward * power, ForceMode.Acceleration);
    47.         }
    48.         if(Input.GetKey(KeyCode.S))
    49.         {
    50.             rb.AddForce(Vector3.back * power, ForceMode.Acceleration);
    51.         }
    52.         if(Input.GetKey(KeyCode.Space))
    53.         {
    54.             if(rb.velocity.z>0)
    55.             {
    56.                 rb.AddForce(Vector3.back * power * 2, ForceMode.Acceleration);
    57.             }
    58.             if(rb.velocity.z<0)
    59.             {
    60.                 rb.AddForce(Vector3.forward * power * 2, ForceMode.Acceleration);
    61.             }
    62.         }
    63.  
    64.  
    65.  
    66.         if(Input.GetKeyDown(KeyCode.F))
    67.         {
    68.             parkingDrag = !parkingDrag;
    69.             if (parkingDrag == true)
    70.             {
    71.                 rb.drag = 1000;  
    72.             }
    73.             if (parkingDrag==false)
    74.             {
    75.                 rb.drag=0.0001f;
    76.             }
    77.         }
    78.  
    79.  
    80.  
    81.     }
    82. }
    83.  
    84.  
     

    Attached Files:

  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    I always wanted to try this but never did: set the center of mass of your rigidbody below the ground. I’m not sure why you would use hinge btw: not hinje) joints instead of wheel colliders, but moving the center of mass below the ground should work either way.

    let me know how it looks and if it works :)
     
  3. fierarux777

    fierarux777

    Joined:
    Feb 11, 2019
    Posts:
    35
    I tryed, and it did balance itself. But it worked only when going straight. If I even slighgtly tryed to change the rotation or oriantation of the bike, it either fall down on the side or glitched in all directions. After some time I found a motorbike controller that satisfied my needs. (You can search motorbike controller by ArcaDone if you want to find it. It's on Github.)
     
    tjmaul likes this.
  4. KudryashovV

    KudryashovV

    Joined:
    Apr 5, 2017
    Posts:
    5
    Hello, fierarux777
    I recently made a simple bike controller. In this controller the balance of the bike is achieved solely by changing the steering angle. No extra forces.
    Project link: https://github.com/V-Kudryashov/BikeBalance
    Video:
     
  5. fierarux777

    fierarux777

    Joined:
    Feb 11, 2019
    Posts:
    35
    Is there a way to modify the angle of the front fork
     
  6. KudryashovV

    KudryashovV

    Joined:
    Apr 5, 2017
    Posts:
    5
    I am going to do it this way:
    The axis of the collider remains vertical. For visual objects, I will add a separate axis. This axis will be inclined. The visible wheel will move along the inclined axis in such a way as to remain on the same height as the collider. In this case, the wheel will move slightly back and forth relative to the collider.

    I will do it in another project. The BikeBalance project is only physics.
     
  7. KudryashovV

    KudryashovV

    Joined:
    Apr 5, 2017
    Posts:
    5
    I made a fork.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FrontForkVisualizer : MonoBehaviour
    6. {
    7.     public WheelCollider frontCollider;
    8.     public Transform physicalWheel;
    9.     public Transform fork;
    10.     public Transform axis;
    11.     public Transform wheel;
    12.  
    13.     private Quaternion startingForkRotation;
    14.     void Start()
    15.     {
    16.         startingForkRotation = fork.rotation;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         fork.localRotation = startingForkRotation;
    22.         fork.Rotate(Vector3.up, frontCollider.steerAngle, Space.Self);
    23.  
    24.         frontCollider.GetWorldPose(out Vector3 pos, out Quaternion rot);
    25.  
    26.         Vector3 diff = frontCollider.transform.InverseTransformVector(pos - axis.position);
    27.         float cos = Vector3.Dot(frontCollider.transform.up, fork.up);
    28.         axis.Translate(0, diff.y / cos, 0, Space.Self);
    29.  
    30.         physicalWheel.rotation = rot;
    31.         wheel.localRotation = physicalWheel.localRotation;
    32.  
    33.         // We need to turn the wheel a little more to simulate rolling during the wheelbase change.
    34.         float targetPos = -frontCollider.suspensionDistance * frontCollider.suspensionSpring.targetPosition;
    35.         float currentPos = frontCollider.transform.InverseTransformPoint(pos).y;
    36.         float tan = Mathf.Sqrt(1 - cos * cos) / cos;
    37.         float HorizontalOffset = (currentPos - targetPos) * tan;
    38.         float rotAngle = -HorizontalOffset / frontCollider.radius * Mathf.Rad2Deg;
    39.         wheel.Rotate(Vector3.right, rotAngle, Space.Self);
    40.     }
    41. }
    42.  
    Video:
    https://drive.google.com/file/d/11f1rKp8De21W9JNhkzBaK1lLOvakUlVt/view?usp=sharing

     

    Attached Files:

    Last edited: Jun 14, 2022