Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Sajjoo · May 23, 2011 at 03:11 PM · iphoneracebike

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.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Joshua · May 23, 2011 at 03:32 PM

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.

Comment
Add comment · Show 1 · Share
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
avatar image Sajjoo · May 23, 2011 at 03:48 PM 0
Share

thanks @Joshua for your answer. i have tried what you suggested but result is same.i have tested it on iphone 3g but result is same.

avatar image
0

Answer by beabourg · May 13, 2012 at 12:33 AM

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

Comment
Add comment · Share
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
avatar image
0

Answer by PAHeartBeat · Aug 07, 2012 at 01:17 PM

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; } } }

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get A Camera Collision? 1 Answer

How to Use Activity Indicator in Unity3d 1 Answer

How to make an exe for my application 2 Answers

Charecter controlled by accelaration for iphone.. 0 Answers

Load image from document directory for iphone 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