• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by PAHeartBeat · Aug 08, 2012 at 09:25 AM · rigidbody.velocitywheel colliderphysic

Motorcycle Physic Move bikes up from back side

Hi Friends,

I am New bee in Unity3D Development. Now days i am working on Bike Racing Game, and dealing with Physic and WheelCollider.

I am in my wheels and bike body with a Empty gameObject and put another empty gameObject for colliers in main parent object for wheel collider, collider are in separate from Wheel Mesh Object.

Bike moves with applying torque on back wheel collider and a script rotated backwheel on X axis, and using physic bike moves. after such a speed Bike moves with jerks and then it's will become up on front wheel just like stunt

here I shared my script w$$anonymous$$ch moves bike (not w$$anonymous$$ch rotated wheel)

 using UnityEngine;
 using System.Collections;
 
 public class BikeMove : MonoBehaviour {
  
  public const float PI  = 3.1415f;
 
  public WheelCollider frontWheel;
  public WheelCollider rearWheel;
  
  public float[] gearRatio = {4.31f, 3.11f, 1.88f, 1.41f, 1.13f, 1.0f};
  private int intCurrentGear = 0;
  private float torqueRatio = 0f; 
  private bool isBrake = false;
  
  private float fltHandBrake = 1.0f;
  
  private float engineTorque = 0.0f;
  
  public float maxEngineRPM = 3000f;
  private float engineRMP = 0f;
  
  //private float bikeHP = 0f;
  public int acceleration =1; 
  
  public GUIText guiSpeed;
  public GUIText guiGear;
  public GUIText guiRPM;
  
  private float speed = 0f;
  public float maxSpeed = 140;
  
  public float mass = 250.0f;
  
  public int maxSteer = 18;
 
  public float turnAngle = 0F;
  public float gyroscope = 0.0f;
  public float gyrosteer = 0.0f;
  public float steerLean = 0.0f;
  public float  leanFactor = 0.0f;
  private Vector3 relativeVelocity = Vector3.zero;
  
  private float inputY = 0;
  private float inputX = 0;
  
  private float inputForce = 0f;
 
  private Transform target;
  
  public float rakeAngle = 25f;
  
  private float wheelBase = 0f;
  public float turningRadius = 0f;
  public float leanAngle = 0f;
  
 
  private float m2bRatio = 0f;
  private float zMass = 0f;
  private Vector3 centerOfMass = new Vector3(0f,-1f,0f);
  
  // Use t$$anonymous$$s for initialization
  void Start () {
  target = transform;
  m2bRatio = rearWheel.radius * 3.28084f * 2.20462f;
  rigidbody.centerOfMass = centerOfMass;
  rigidbody.mass = mass;
  wheelBase = Mathf.Abs (Vector3.Distance (frontWheel.transform.localPosition, rearWheel.transform.localPosition));
  }
  
  // Update is called once per frame
  void Update () {
  
  //Getting Platform and input Value from the palyer
  switch (Application.platform) {
  case RuntimePlatform.IPhonePlayer:
  case RuntimePlatform.Android:
  inputX = -Input.acceleration.y;
  break;
 
  case RuntimePlatform.OSXPlayer:
  case RuntimePlatform.OSXEditor:
  case RuntimePlatform.OSXWebPlayer:
  case RuntimePlatform.WindowsPlayer:
  case RuntimePlatform.WindowsEditor:
  case RuntimePlatform.WindowsWebPlayer:
  inputY = Input.GetAxis ("Vertical");
  inputX = Input.GetAxis ("Horizontal");
  break;
  }
  
  rigidbody.drag = rigidbody.velocity.magnitude / 250;
  if (inputX != 0)
  turnAngle += inputX * Time.deltaTime * 02f;
  //else if (turnAngle < -0.025 || turnAngle > 0.025){
  // turnAngle = (-turnAngle) * Time.deltaTime *0.1f ;}
  else 
  turnAngle = 0;
  
  isBrake = false;
  if (inputY > 0)
  inputForce += acceleration;
  
  if (inputY < 0) {
  inputForce -= (inputForce * 0.10f);
  isBrake = true;
  } 
  if (inputY == 0)
  inputForce -= (inputForce * 0.10f);
  
  if (inputForce < 0)
  inputForce = 0;
  
  if (speed >= maxSpeed || engineRMP >= maxEngineRPM)
  inputForce -= inputForce * 0.10f;
  
  engineTorque = inputForce * m2bRatio;
  torqueRatio = (engineTorque) / gearRatio [intCurrentGear];
  
  if (isBrake) {
  rearWheel.motorTorque = 0;
  rearWheel.brakeTorque = mass;
  } else {
  rearWheel.brakeTorque = 0;
  rearWheel.motorTorque = torqueRatio;
  }
 
  fltHandBrake = (Input.GetButton ("Jump") ? 1.0f : 0.0f);
  if (fltHandBrake > 0)
  frontWheel.brakeTorque = mass;
  else
  frontWheel.brakeTorque = 0;
  
  speed = (target.rigidbody.velocity.magnitude * 3.6f);
  
  
  engineRMP = rearWheel.rpm * gearRatio [intCurrentGear];
  
  audio.pitch = Mathf.Abs (engineRMP / maxEngineRPM) + 1.0f;
 
  turnAngle = Mathf.Clamp (turnAngle, -maxSteer, maxSteer); 
  frontWheel.steerAngle = turnAngle;
  
  if (turnAngle != 0) {
  turningRadius = wheelBase / (turnAngle * Mathf.Cos (rakeAngle));
  leanAngle = Mathf.Atan (Mathf.Pow (speed, 2f) / (turningRadius));
  } else
  leanAngle = 0;
  
  
  /*relativeVelocity = transform.InverseTransformDirection (rigidbody.velocity);
  gyroscope = Mathf.Clamp01 ((rigidbody.velocity.magnitude) / 20);
  gyrosteer = (1 - gyroscope) * 0.90f;
  steerLean = turnAngle * gyroscope; 
  leanAngle = turnAngle - steerLean * gyrosteer;*/
 
  
  //leanAngle = (speed / maxSpeed) * turnAngle * 100;
  //leanAngle = Mathf.Clamp (leanAngle, -45, 45);
  //target.eulerAngles = new Vector3 (target.localEulerAngles.x, target.localEulerAngles.y, -leanAngle);
  target.eulerAngles.Set(0,0,leanAngle);
  
  
  }
  void FixedUpdate () {
  S$$anonymous$$ftGears (); 
 
  guiSpeed.text = "Speed: " + speed.ToString ("f2") + " KM/h";
  guiGear.text = "Currne Gear: " + intCurrentGear.ToString () + ", Input Force: " + inputForce.ToString ("f2");
  guiRPM.text = "Engine RPM: " + engineRMP.ToString ("f2") + ", RW RPM: " + rearWheel.rpm;
  }
  
  void S$$anonymous$$ftGears () {
  if (engineRMP >= 1400f && intCurrentGear < gearRatio.Length - 1) {
  intCurrentGear ++;
  inputForce = inputForce * 0.75f;
  }
  
  if (engineRMP <= 800f && intCurrentGear > 0) {
  intCurrentGear --;
  inputForce = inputForce * 0.75f;
  }
  }
 }


After some web searc$$anonymous$$ng i found the solution but the solution is creating another problem in leaning of bike when i rotated my front wheel.

solution is i put few line of code in Update function after calculating speed on base of object Velocity magnitude w$$anonymous$$ch moves centerOfMass back side of bike as speed increasing

//code for un tilt bike from back /if (speed >= 1f) zMass = (-1.55f / 228f) speed; else zMass = 0f;

centerOfMass.Set (0, -1, zMass); rigidbody.centerOfMass = centerOfMass;*/ // code end

So code I adds in my script is not a real solution of the problem... looking for some help...

thanks guys in andvance

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth motion physics Playmaker 0 Answers

Collider with cinema 4D 2 Answers

rigidbody.velocity.x also affecting z-axis? 1 Answer

Velocity on a rigidbody won't stop? 2 Answers

Rigidbody.velocity Not working on parent rigidbody with four child rigidbodies attached to it 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges