Balancing a BiCycle in Unity

Updated on May 11, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
1 on May 11, 2017

Hello Guys,I am really in learning stage of unity, I am making a Bicycle in my game, i did most of the part like steering,wheelColliders and all but cannot balance my bike,it just falls as soon as i start peddling , i used a basic idea to ray a cast from its
bicycles gameobjects centre in four directions and as soon as one of the ray (i.e,Right or Left) detects the terrain then the bike should go in opposite side to balance itself , but i cannot really figure how to make the bike oppose

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class BikePhysics : MonoBehaviour {
    void Update ()
    {
        Raycasting ();
    }
    void Raycasting()
    {
         RaycastHit hit;
  if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),out hit))
      {
           Debug.DrawLine (transform.position, hit.transform.position);
      }
if(Physics.Raycast(transform.position,transform.TransformDirection(-Vector3.forward),out hit))
     {
          Debug.DrawLine (transform.position, hit.transform.position);
     }
 if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.right),out hit))
     {
         Debug.DrawLine (transform.position, hit.transform.position);
         if(hit.transform.gameObject.tag == "Terrain")
           {
               //Oppose the side
           }
     }
 if(Physics.Raycast(transform.position,transform.TransformDirection(-Vector3.right),out hit))
    {
        Debug.DrawLine (transform.position, hit.transform.position);
        if(hit.transform.gameObject.tag == "Terrain")
            {
                  //Oppose the side
            }
    }
  }
}
 

  • Liked by
Reply
0 on May 11, 2017

I’ve not done anything like this before but i have a suggestion, try using the riders colider and some offset value in the balence calculations your currently using  to balance the bike

  • Liked by
Reply
Cancel