bike moving forward with jerks

Hello guys, i am totally new to unity and i am making a racing game in which i have a bike. i have applied rigid body on my bike and gave 500 Mass and applied colliders on my bike. here is my script which i have wrote to tilt/turn and move the bike. but its moving by taking jerks and its movement is so slow. i also have tested it on my iphone 3G.

if(GameObject.FindWithTag("MotorCycle"))
	{			
		var accelerator : Vector3 = Input.acceleration;
		//Debug.Log("Z... " + accelerator.z);
		GameObject.FindWithTag("MotorCycle").rigidbody.AddRelativeForce(0, 0, normalizedSpeed * (forwardForce*3) * accelerator.z * 1000);
	
		if (horizontalOrientation)
		{
			var t : float = accelerator.x;
			accelerator.x = -accelerator.y;
			accelerator.y = t;
		}

		 euler.y = accelerator.x * 15;
		
		 euler.z = Mathf.Lerp(euler.z+1, accelerator.x * maxTurnLean, 0.2);
			Debug.Log("Euler z..."+ euler.z);

		 euler.x = Mathf.Lerp(euler.x, 0 , 0.2);
			
		var rot : Quaternion = Quaternion.Euler(euler);
		
		GameObject.FindWithTag("MotorCycle").transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
	} 

i have also increased the forward force but still its taking jerks. i,ll be very thankful if anybody can help me with this.

thanks.

Hi Sajjoo, welcome to UnityAnswers.

I notice that in your small script you reference the same MotorCycle several times, but each time you use GameObject.FindWidthTag, making the machine look up what you mean each time you use it (three times per frame). Since this might be causing your lag I would advice you to simply declare it once and then reference it. Here is a little example:

var MotorCycle : GameObject; //if you just do it like this, you can drag 'n drop your MotorCycle gameObject on it. Then you can simply reference the variable. 
MotorCycle.rigidBody.AddForce(...);

if you wish you can also make the game look up the motorcycle, like it does now, but let it do it only once.

var MotorCycle : GameObject;

function Start () { MotorCycle = GameObject.FindWithTag("MotorCycle"); }

See if this reduces the lag.

Also, when adding the force you want to take the factor time in account. Now you add x force per frame (where x is NormalizedSpeed * otherstuff) you want to replace that with x*Time.deltaTime to add x force per second instead of per frame. Else the MotorCycle moves several times faster/slower on other computers.

I sugest you use wheelcolliders and addtorque, look at the car tutorial!

Hi Guys,

I am working on bike race game (M also new in unity game development) I go with car tutorial of unity, My bike move smooth up to a speed (you can get it like at 100 KM/h) I am applying torque to back wheel and bike moves smooth. After a specific speed my bike also getting jerks and then it will become up from back side and runs only on front wheel (just like bike stunt)

I put centerOfMass of rigid-body back side of bike and it’s move smoth but bike leans automatic after a speed. and even moving center of mass to back side is not an good idea.

here with i share my code

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 this 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);

//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

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);
//transform.localEulerAngles.Set (0, 0, -steerLean);

}
void FixedUpdate () {
ShiftGears ();

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 ShiftGears () {
if (engineRMP >= 1400f && intCurrentGear < gearRatio.Length - 1) {
intCurrentGear ++;
inputForce = inputForce * 0.75f;
}

if (engineRMP <= 800f && intCurrentGear > 0) {
intCurrentGear --;
inputForce = inputForce * 0.75f;
}
}
}