Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Motorcycle stabilization

Discussion in 'Scripting' started by maxwelldoggums, Mar 7, 2009.

  1. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    Hi, I'm having a bit of trouble, I'm writing a motorcycle racing game, and I need to be able to stabilize the bike automatically, allowing it to lean around turns without falling over. I'm trying to keep it written into the Unity3D physics engine so that the player can still fall when turning too sharply.

    I've experimented with several different methods, none of which seem to work for me. The bike must be able to move accurately over ramps and complex terrain, and so I'd like to use some sort of system that applies a torque to the bike to keep it stable.

    Do any of you have any suggestions on how I could do this quickly and easily?

    Thanks in advance
    -Max
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I don't have any tips but I'm *very* interested in this topic as I've long considered taking a stab at a motorcycle simulation and am eager to hear some ideas.
     
  3. pavlito

    pavlito

    Joined:
    Nov 27, 2007
    Posts:
    136
    Maybe this can help.

    Use rigidbody.AddRelativeTorque() when the player presses arrow keys. Then on release of keys, add relative torque the opposite way until rotation reaches zero.

    I've used a similar method when creating a non-physics airplane arcade project.

    The physics engine should automatically knock the driver down if he turned too sharply.

    If you're not aiming at a REAL simulation, I advise you to check MotoRacer 1. That game has the highest arcade playability I've ever seen in moto racing games.

    Hope I helped. If not, slap me around a bit with a large trout. :)

    Cheers!
     
  4. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    Thank you both for your comments.

    I don't think that adding a torque relative to the key the player pressed would work. If you have the bike driving on an uneven surface it wouldn't stay level, even if it were off by only a few degrees. I could try adding torque based on the actual angle though....
     
  5. pavlito

    pavlito

    Joined:
    Nov 27, 2007
    Posts:
    136
    Maybe another thing might be done.

    What makes the bike lean from one side to another? The driver.

    Maybe you could make the driver (a cube or whatever) as a child object (rigidbody) to your bike and then move the driver left / right depending on the keys pressed. That way, the driver can influence physics and make the bike lean, provided that you use wheel colliders with proper traction to them.

    I'm not sure if it would work, just a thought though.
     
    AnPhonEh likes this.
  6. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    I was thinking of shifting the center of mass myself, but again, the motion can't be relative to the control, it has to be dependent on the bike's orientation itself...
     
  7. pavlito

    pavlito

    Joined:
    Nov 27, 2007
    Posts:
    136
    Sorry man, I lost ya :)
     
  8. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    I figured it out. I set the bike rotation to the euler current rotation, but then I altered the component on the z axis and manually set it to a rotation, this way pitch and yaw still work in the physics engine, but roll is set manually.

    There're still some problems with constraining the physics engine, but I'll work those out later =P
     
  9. pavlito

    pavlito

    Joined:
    Nov 27, 2007
    Posts:
    136
    Glad to hear you making your way to the top. Keep it up :)
     
  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Very true indeed (as an avid motorcyclist and racer I can vouch for the fact that this is 100% true).


    @Maxwelldoggums: I'd love to see any code or an example project if you have something to share (even if via PMs). <hint><hint> :)
     
  11. MOverlund

    MOverlund

    Joined:
    Mar 31, 2009
    Posts:
    24
    I've been messing with something similar for a day or so and not seen any results I'm satisfied with. Granted, I'm new to unity so a lot of it is just inexperience. The first thing I noticed is that when I rotate on the z axis beyond a very small margin, the wheel colliders stop applying motorTorque to the road. This make sense for a 4 wheeled vehicle, but of course it's a problem with the bike. I don't see any legitimate way to simulate surface contact area on the tires that acts in a realistic manner.

    I tried a few things but nothing really successful. The closest I came was using 2 rigid bodies connected via hinges (tried a few different types). This allowed me to lean the visible portions of the bike while leaving the wheels upright. Couldn't get it to feel right though, and in most cases the rotations on the y and z axis would not play well together.

    @Tom: What do you ride, if you don't mind sharing? Katana 600 and Honda CBR 1K here.
     
  12. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Ahem, ready for this...

    Street bikes:
    KTM 950 Adventure (with knobbies :D )
    Honda XR650R SM (plated and legal!)

    Track bikes:
    Yamaha TZ250 (bling!)
    Aprilia RS250 (not so bling but fun!)


    Hi, my name is Tom and I'm a bike-a-holic. :)

    </off-topic move to PMs for more bike chatter :) >
     
    anirudha likes this.
  13. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    Nice. I have an updated version of my motorcycle game if you would like to see it.
     
  14. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I'd definitely like to see it! :)
     
  15. dingosmoov

    dingosmoov

    Joined:
    Jul 12, 2005
    Posts:
    559
    @Maxwelldoggums - did you ever get this fully worked out?

    Does anyone have any motorcycle game examples, tips?
     
  16. coalguru

    coalguru

    Joined:
    Feb 13, 2010
    Posts:
    20
    I'm just tried using my universal car script - as shown at http://forum.unity3d.com/viewtopic.php?t=43843 - with two wheels only - and after correction of center of mass the "bike" moves quite realistic and balances itself somehow.
     
  17. AmazingInt

    AmazingInt

    Joined:
    Dec 7, 2009
    Posts:
    157
    Am I correct in assuming that the bike usually loses control due to loss of traction between the rear drive wheel and the road surface, particularly during hammering the throttle too much during banking the turn, as the rider shifts the centre of gravity by leaning?

    If so then you could always have the bike tilt as far as the player wants (until the pegs hit the ground which would obviously case collision) and use the friction between the tyres and road surface to determine if the bike is going to slide out...

    Using this method should easily allow you to set differing friction rates for track/dirt/grass etc while the tyre types could in effect also be set (allows for the player to upgrade tyre types: Hard/soft rubber - knobblies/treaded/racing slicks).
     
  18. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    No, not always as either the front or rear wheel can lose traction. To get all bike-geek on you:

    1. "Tucking the front" is a common crash caused by coming into a corner with too much speed. The front gets extra weight on corner entry as you're usually trying to slow down so weight moves forward, combine that with a smaller contact patch (less overall grip) and you'll often lose the front and fall down in what's called a low-side crash.

    2. "Losing the rear" is another common crash that's caused by giving it too much gas on corner exit, acceleration at lean (smaller than normal contact area) overpowers the rear tire's grip and the back end slides out from under you once again causing a low-side crash.

    3. The ugly crash (been through one myself at the track, separated shoulder was the result :( ) comes when you start losing the back end (see #2 above) but then chop the throttle fast enough to stop the low-side, but the problem is now that the back end regains grip while too far out to the side and the rider is thrown up and over the bike in what's called a "high-side" crash.


    Of course there are other variations but those are the "big 3" crash types. You can lose both tires at the same time, lose one or the other due to bumps/oil/whatever. But on the brakes crashes are usually loss of front tire grip, on the gas crashes are usually loss of rear tire grip.


    Want to see a high-side in action? Then watch this (youtube vid).
     
  19. AmazingInt

    AmazingInt

    Joined:
    Dec 7, 2009
    Posts:
    157
    Thanks for that Higgy :)
    I know I was referencing just the rear wheel loss, but I intended that as a simplification. What I actually meant was why not use a traction-based method rather than just using the angle of the bike to determine whether a crash state is initiated (I used to grind my pegs a lot and went through knees in my leathers more frequently than most of my mates) :D

    I always seem to start responding to the really interesting posts when it's obscenely late and the brain-to-text function fails :wink:
     
  20. kyoryu

    kyoryu

    Joined:
    Feb 15, 2010
    Posts:
    8
    Correct, but via countersteering, not directly by leaning.
     
  21. Route 66 Rambler

    Route 66 Rambler

    Joined:
    Mar 12, 2010
    Posts:
    45
    I know this is an old thread, but I'm sure this is an ongoing problem for some of us simulation types.

    I've been working on a gta-style driving game with different vehicles in it like planes, helicopters, cars, bikes etc.

    Andrew Gotow's scripts at gotow.net made a very good starting point for me to get into the groove of things.

    To stabilize my bicycle and motorcycle models, I found that the car script from there is a good starting point, and once you have set the center mass properly, the bike will run upright on its own, but tends to lean or swing back and forth like a pendulum.

    This piece of code from the helicopter tutorial on that site worked to stabilize the oscillation quite a bit.

    first set up a var : bike = transform statement, with the name of your model in place of the word bike. In the code below, substitute the name of your transform variable in place of BIKE.

    Then the bike will lean realistically in corners, but will return to run upright in a stable manner. There will still be some work to do as far as suspension and traction on the wheel colliders, and you need to pay attention to your torque and gearing.

    The variable steering code from coalguru's Autocar.js script in this thread is a big help on the bike's handling as well.

    It tends to work best if placed at the end of a script, as the last function, inside the last curly brace. This is from a js file.

    Code (csharp):
    1.     function FixedUpdate () {
    2.    if ( Vector3.Angle( Vector3.up, BIKE.up ) < 30) {
    3.     BIKE.rotation = Quaternion.Slerp( BIKE.rotation, Quaternion.Euler( 0, BIKE.rotation.eulerAngles.y, 0 ), Time.deltaTime * 5 );
    4.  }
     
    Speculoos likes this.
  22. holokaust

    holokaust

    Joined:
    Nov 17, 2009
    Posts:
    18
    Anyone making motorcycle/bike racing games or working with it? Any tutorial, examples or demos on how to do one? Im kinda lost on how to do one.

    Thanks in advance,
     
  23. Route 66 Rambler

    Route 66 Rambler

    Joined:
    Mar 12, 2010
    Posts:
    45
    This is Andrew Gotow's blog, called This Is A Test:

    http://www.gotow.net/andrew/blog/

    There are practical tutorials on that site for creating car and helicopter simulations.

    Also, on this forum's main index, check under "Announcements", and you will find an excellent car tutorial by Unity to get you started.

    Once I got the car thing mastered, I started working on aircraft. After that, I moved to the two-wheel vehicles. The helicopter tutorial mentioned above will point you in the right direction to give you the capability to lean back and forth or spin the bike to do stunts like wheelies or 360s. Learn the cars first, then the aircraft, and the bikes will fit in neatly after that.
     
  24. webco

    webco

    Joined:
    Sep 2, 2010
    Posts:
    85
    Hello, I'm trying to modify the game from the car tutorial by replacing the car with a motorcycle. The piece of code of Route 66 works, but I'd like that the moto balances only when the player uses Left or Right Keys. How can I do it properly and by modifiying only the car script ?
     
  25. webco

    webco

    Joined:
    Sep 2, 2010
    Posts:
    85
    How can I reduce the skid effect please ? My motorbike is too sensible when there is a change of direction.
     
  26. webco

    webco

    Joined:
    Sep 2, 2010
    Posts:
    85
    Once again sorry, but I've got another problem. How can I replace more quickly the camera behind the vehicle when it turns ?

    Here's the actual piece of code :
    Code (csharp):
    1.     void FixedUpdate()
    2.     {
    3.         currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
    4.         currentVelocity.y = 0;
    5.         prevVelocity = currentVelocity;
    6.     }
    7.    
    8.     void LateUpdate()
    9.     {
    10.         float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 70.0f);
    11.         camera.fieldOfView = Mathf.Lerp(60, 72, speedFactor);
    12.         float currentDistance = Mathf.Lerp(5.5f, 6.5f, speedFactor);
    13.        
    14.         currentVelocity = currentVelocity.normalized;
    15.        
    16.         Vector3 newTargetPosition = target.position + Vector3.up * height;
    17.         Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
    18.         newPosition.y = newTargetPosition.y;
    19.        
    20.         Vector3 targetDirection = newPosition - newTargetPosition;
    21.         if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
    22.         {
    23.             newPosition = hit.point;
    24.         }
    25.        
    26.         transform.position = newPosition;
    27.         transform.LookAt(newTargetPosition);
    28.     }
    29.  
    My previous question is still on.
     
  27. spaceMan-2.5

    spaceMan-2.5

    Joined:
    Oct 21, 2009
    Posts:
    710
    Is there another answer here ?? ^^
     
  28. stuartf

    stuartf

    Joined:
    Sep 5, 2010
    Posts:
    30
    OK folks this is a very first stab at a bike game heavily dependent on cut and paste of examples and links/sugestions in this forum post + Andrew Gotow, Andeee (admin) + Eugene.

    It’s very primitive (and wobbly) at the moment and there are no starts/finishes/collison detectors/lap times etc etc etc but still a bit of fun and frustrating at the same time :)

    It’a also intended to be low poly for web access and use.

    The non player bikes have very basic AI and physics and do some amusing things if you leave them to interact with each oher and the player e.g. riding round backwards etc.etc.

    Use the arrow keys to accelerate, brake and steer. Feel free to give the other bikes a nudge in anger or to get them going again if they’ve fallen by the wayside :)

    http://www.bikerwales.com/rally/rally.htm
     
    Last edited: Nov 25, 2010
  29. Route 66 Rambler

    Route 66 Rambler

    Joined:
    Mar 12, 2010
    Posts:
    45
    Hi, guys, I've been out of the country since my posts above. I am sorry for not being involved with this thread before now, but there was no helping it.

    To make the balance dependent on the keys, I think that you could take input from a key using GetInputKey and adding a + or - increment to the stabilization function, or to introduce a "sloppiness" to the main steering function.
     
  30. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I would like to get into this conversation, though all my stuff is not with my at the moment. I will throw up a simple 3ds file tommorrow.

    The concept I want to explore is using SphereColliders for the wheels. controlling y rotation around the center of mass, x rotation around the wheel bottom. As the bike turns, it will bank into the turn forcing the sphere collider to start shrinking according a formula based on the width of the tire. This in turn will start a calculation based on the angle of bank for the turn. Also, I will want it to turn the forks if it is going below a certain speed and bank less.

    Two ConfigurableJoints on the front wheel and fork that allows sliding for the front forks, so that you can get a smooth ride also this can be used to calculate the compression of the front fork visibly. This process will also be applied to the back wheel, though it's compression is rotational on a hinge and spring rather than a fork. A Hard Tail bike can also be created by simply stating that the rear spring distance is zero.

    The controls of he bike would be WASD for movement and turning, space for hand brake and probably a couple of other keys for push and pull.
     
  31. Route 66 Rambler

    Route 66 Rambler

    Joined:
    Mar 12, 2010
    Posts:
    45
    Webco, try setting the rotation damping on your following camera to a higher value. Also, there are settings for tire grip and other tire factors, I believe it's in one of the wheel scripts.
     
  32. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    sidewaysfriction and forwardfriction settings give you slip factors for a vehicle, setting those to about 0.02 or so will do nicely. In my vehicle scripts I use a AnimationCurve to set a sidewaysfriction So when you are going slow, you have lots of grip, when you are going fast, you have little, this prevents a car from flipping. So I have a range from 0.5 standing still to 0.001 at high rates of speed.
     
  33. Route 66 Rambler

    Route 66 Rambler

    Joined:
    Mar 12, 2010
    Posts:
    45
    That idea for a sphere collider cycle system sounds interesting. I've considered sphere colliders before, but for things like big, fat offroad tires.
     
  34. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Oh yeah, the sphere's didn't work at all. or at least I wasn't patient enough with them. I did however come up with a basic script though it's not that basic. But kind of gets the point across.

    The camera script that I am using is HERE

    Ok, the script below is rather un-clean. It works exactly like the demo I attached. There are variables unused, stuff I pulled from other scripts I wrote and rather un-commented. This, like the other scripts I wrote creates the physics stuff on the fly, so there isn't much setup to the bikes. There are a lot of private variables, some of which should be public, but since I am testing, I don't want to have to dink with the editor to change anything at this point.

    The major problems occur when the bike reaches 90 degree's in the Y and X axis. it does funky stuff and my brain is pretty much mush from thinking about it. Other than that, it works pretty much like a bike does.

    Features: turning front handlebars, fork and wheel. Actually less of a feature because it had to be done to make the bike turn. ;) Gyroscopic steering. (very fake at this point) The code actually controls the Z rotation of the bike and the rotation of the handlebars. It uses that alone to steer.

    Model Setup: Wheels and the Fork should be referenced to their axis, the frame and all other components at this point are referenced to the world center. (center of the bike ;) ) Models are of course forward/forward.

    Things to do: Front and rear suspension, Hard Tail suspension and making the wheels rotate with the tires. (getting a better bike with textures would be nice) Also, fix the freaky rotations.

    Enjoy!
    Code (csharp):
    1.  
    2. var frame : Transform;
    3. var frontFork : Transform;
    4. var rearCarriage : Transform;
    5.  
    6.  
    7. var frontWheel : Transform;
    8. var rearWheel : Transform;
    9.  
    10. var col : Transform;
    11.  
    12. private var maxSteer=45;
    13. private var minSteer=5;
    14. private var rake = 30.0;
    15. private var motorPower=150;
    16. private var mass=500.0;
    17. private var centerOfMass=Vector3(0,-0.25,1);
    18.  
    19.  
    20. private var frontJoint;
    21. private var rearJoint;
    22.  
    23. private var frontWheelSize : Vector3;
    24. private var rearWheelSize : Vector3;
    25. private var stopTilt = 5.0;
    26. private var doScript=false;
    27.  
    28. private var steerAngle=0.0;
    29. private var motorAmount=0.0;
    30. private var motorTorque=0.0;
    31. private var handBrake=0.0;
    32. private var engineBrake=0.0;
    33. private var engineRPM=0.0;
    34. private var motorMod=22.0 / 50;
    35. private var gear=0;
    36. private var relativeVelocity=Vector3.zero;
    37. private var veloSteer=maxSteer;
    38. private var lastX =0.0;
    39. private var lastY=0.0;
    40. private var lastZ=0.0;
    41. private var steerLean = 0.0;
    42. private var steerAmount=0.0;
    43. private var defaultForkAngle;
    44. private var forkSteeringPoint;
    45.  
    46. function Start(){
    47.     if(!col)return;
    48.     if(!frontWheel)return;
    49.     if(!rearWheel)return;
    50.     if(!frontFork)return;
    51.     if(!rearCarriage)return;
    52.     if(!frontWheel.gameObject.GetComponent(MeshFilter))return;
    53.     if(!rearWheel.gameObject.GetComponent(MeshFilter))return;
    54.    
    55.    
    56.     gameObject.AddComponent(Rigidbody);
    57.    
    58.     var MC = col.gameObject.AddComponent(MeshCollider);
    59.     MC.convex=true;
    60.     MC.smoothSphereCollisions=true;
    61.     col.renderer.enabled=false;
    62.    
    63.     frontWheelSize=frontWheel.gameObject.GetComponent(MeshFilter).mesh.bounds.size;
    64.     rearWheelSize=rearWheel.gameObject.GetComponent(MeshFilter).mesh.bounds.size;
    65.    
    66.     forkSteeringPoint=GameObject().transform;
    67.     forkSteeringPoint.parent=frontFork;
    68.     forkSteeringPoint.localPosition=Vector3.zero;
    69.     forkSteeringPoint.Rotate(Vector3(-rake, 0, 0 ));//self space
    70.     var axis = forkSteeringPoint.TransformDirection(Vector3.up);
    71.    
    72.     defaultForkAngle=frontFork.localRotation;
    73.     if(false){
    74.         frontFork.gameObject.AddComponent(Rigidbody);
    75.         //frontFork.rigidbody.isKinematic=true;
    76.        
    77.         var joint=frontFork.gameObject.AddComponent(HingeJoint);
    78.         joint.axis=axis;
    79.         joint.anchor=Vector3.zero;
    80.         joint.useSpring = false;
    81.         joint.useLimits=true;
    82.         joint.limits.min = -5;
    83.         joint.limits.minBounce = 0;
    84.         joint.limits.max = 5;
    85.         joint.limits.maxBounce = 0;
    86.         joint.connectedBody=rigidbody;//1.740522
    87.     }
    88.    
    89.     frontWheel.parent=frontFork;
    90.     frontJoint = frontWheel.gameObject.AddComponent(WheelCollider);
    91.     frontJoint.radius=frontWheelSize.y * transform.localScale.y / 2;
    92.     frontJoint.suspensionDistance = 0.0;
    93.     frontJoint.forwardFriction.stiffness = 0.02;
    94.     frontJoint.sidewaysFriction.stiffness = 0.02;
    95.    
    96.     rearJoint = rearWheel.gameObject.AddComponent(WheelCollider);
    97.     rearJoint.radius=rearWheelSize.y * transform.localScale.y / 2;
    98.     rearJoint.suspensionDistance = 0.0;
    99.     rearJoint.forwardFriction.stiffness = 0.02;
    100.     rearJoint.sidewaysFriction.stiffness = 0.02;
    101.    
    102.     resetCenterOfMass();
    103.    
    104.     doScript=true;
    105. }
    106.  
    107.  
    108. function resetCenterOfMass(){
    109.     rigidbody.mass=mass;
    110.     rigidbody.drag=0.175;
    111.     rigidbody.centerOfMass+=centerOfMass;
    112. }
    113.  
    114. function Update () {
    115.     if(!doScript)return;
    116.    
    117.     relativeVelocity = transform.InverseTransformDirection(rigidbody.velocity);
    118.     veloSteer=Mathf.Clamp(maxSteer-Mathf.Abs(relativeVelocity.z) * 0.5, minSteer, maxSteer);
    119.    
    120.     motorAmount = Input.GetAxis("Vertical") * motorPower * Time.deltaTime;
    121.     steerAngle = Input.GetAxis("Horizontal") * veloSteer;
    122.     handBrake = (Input.GetButton("Jump")?1.0:0.0) * mass;
    123.    
    124.     if(handBrake>0){
    125.         rearJoint.motorTorque = 0.0;
    126.         rearJoint.brakeTorque = handBrake;
    127.         frontJoint.brakeTorque = handBrake * 0.25;
    128.     }else{
    129.         rearJoint.motorTorque = motorAmount * 100;
    130.         rearJoint.brakeTorque = 0.0;
    131.         frontJoint.brakeTorque = 0.0;
    132.     }
    133.    
    134.     var x=transform.localEulerAngles.x;
    135.     var y=transform.localEulerAngles.y;
    136.     var z=transform.localEulerAngles.z;
    137.    
    138.     if(x>180)x=x-360;
    139.     if(y>180)y=y-360;
    140.     if(z>180)z=z-360;
    141.    
    142.    
    143.     //frontFork.Rotate(Vector3.right);
    144.     // steering lean
    145.     // slow movement gets a less lean
    146.     var rpm=(Mathf.Abs(frontJoint.rpm) + Mathf.Abs(rearJoint.rpm))/2;
    147.     var gyroscope=Mathf.Clamp01((relativeVelocity.z) / 20);
    148.     var gyrosteer =(1-gyroscope) * 0.9;
    149.     steerLean=steerAngle * gyroscope;
    150.     steerAmount=steerAngle - steerLean * gyrosteer;
    151.    
    152.     //print(Array(steerLean, steerAmount, relativeVelocity.z, rpm));
    153.    
    154.     if(!frontJoint.isGrounded  !rearJoint.isGrounded  relativeVelocity.z > 5){
    155.         //steerLean=lastLean;
    156.         transform.localEulerAngles.y=lastY;
    157.         if(Input.GetKey("space"))
    158.             rigidbody.AddRelativeTorque (Vector3.right * mass * 2);
    159.         //else
    160.         //  if(Input.GetAxis("Vertical")>0)
    161.         //      rigidbody.AddRelativeTorque (-Vector3.right * mass * 2);
    162.         //transform.localEulerAngles.x=lastX;
    163.     }
    164.    
    165.     transform.localEulerAngles.z=-steerLean;// Mathf.Lerp(lastLean, -steerLean, Time.deltaTime * 8);
    166.    
    167.     frontFork.localRotation=defaultForkAngle;
    168.     var axis = forkSteeringPoint.TransformDirection(Vector3.up);
    169.     frontFork.RotateAround (frontFork.position, axis, steerAmount);
    170.    
    171.     x=transform.localEulerAngles.x;
    172.     y=transform.localEulerAngles.y;
    173.     z=transform.localEulerAngles.z;
    174.    
    175.     if(x>180)x=x-360;
    176.     if(y>180)y=y-360;
    177.     if(z>180)z=z-360;
    178.    
    179.     lastX=x;
    180.     lastY=y;
    181.     lastZ=-z;
    182.    
    183.     if(Input.GetKeyDown("r"))doReset();
    184. }
    185.  
    186. function LateUpdate(){
    187.    
    188. }
    189.  
    190. function doReset(){
    191.     transform.position+=Vector3(0,0.5,0);
    192.     transform.rotation=Quaternion.Euler( 0.0, transform.localEulerAngles.y, 0.0 );
    193.     rigidbody.velocity=Vector3.zero;
    194.     rigidbody.angularVelocity=Vector3.zero;
    195. }
    196.  
    197. function OnCollisionEnter(collision : Collision) {
    198.     //print(Array("Enter Collision",collision.relativeVelocity.magnitude, collision.contacts[0].thisCollider.transform.name));
    199.     for (var contact : ContactPoint in collision.contacts) {
    200.         //print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
    201.         // Visualize the contact point
    202.         Debug.DrawRay(contact.point, contact.normal, Color.white);
    203.     }
    204. }
    205.  
    206.  
     

    Attached Files:

  35. ivanzu

    ivanzu

    Joined:
    Nov 25, 2010
    Posts:
    2,065
    Well im nota programmer but i havea suggestion try to make a some kind of system that detects the rotation of you bike lets say 30° and its stay there until player releases D button or any other then it goes back to 0° :)
     
  36. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    That would be pretty much what I wrote, but a little more fancy. It uses a velocity check that tells it whether or not to lean, and how much to lean. In the same setup it also says, turn the handle bars the total steering - the lean amount. So if you can't lean because you are going slow, thenpull the bar and lean a little.
     
  37. panni

    panni

    Joined:
    Mar 24, 2011
    Posts:
    3
    Help! Used in the project script respected bigmisterb, the problem with the stabilization of the motorcycle? Motorcycle jumping over terrain!

    frontJoint.radius=frontWheelSize.y * transform.localScale.y / 2; ?
     
  38. hencz

    hencz

    Joined:
    Aug 19, 2010
    Posts:
    86
    I think you should use lossyScale not localScale.
     
  39. panni

    panni

    Joined:
    Mar 24, 2011
    Posts:
    3
  40. panni

    panni

    Joined:
    Mar 24, 2011
    Posts:
    3
    MissingComponentException: There is no 'Renderer' attached to the "Collider" game object, but a script is trying to access it.
    You probably need to add a Renderer to the game object "Collider". Or your script needs to check if the component is attached before using it.
    Bice Script test.Start () (at Assets/Scripts/Bice Script test.js:60)

    col.renderer.enabled=false; ???
     
  41. LS16BMX

    LS16BMX

    Joined:
    Nov 11, 2011
    Posts:
    94
    I think its relevant to post my WIP hear if anybodys interested I need all the help I can get.
    I'm using a one of a kind realtime physics setup that I haven't seen anybody use before
     
  42. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Noice. Btw, last trick = Flair fail :p
    If anyone doesnt do BMX, a flair is a backflip180.
     
  43. LS16BMX

    LS16BMX

    Joined:
    Nov 11, 2011
    Posts:
    94
    yup massive fail lol, what you don't see from this video is how fun learning to control the bike is. Its something that has lead me down the path of only using pure physics rather than simulated physics. I can spend hours on one jump and not get board, just like the real thing, I know i'm onto something its just a long and lonely road if you know what i mean :O)
     
  44. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Sorry to bring this up from the dead, but how exactly do you get this script working? I downloaded the script, and the motorcycle 3DS model.
     
  45. Pabong

    Pabong

    Joined:
    Sep 21, 2012
    Posts:
    25
  46. Pabong

    Pabong

    Joined:
    Sep 21, 2012
    Posts:
    25
  47. LS16BMX

    LS16BMX

    Joined:
    Nov 11, 2011
    Posts:
    94
  48. bcman

    bcman

    Joined:
    Feb 9, 2013
    Posts:
    139

    What do you think guys ?
    It's standard wheel collider with some thickness(I have 7 wheels as rearwheel) BUT there is no support wheels. So, all 7 wheel colliders are "IN" tire. middle is biggest and side ones are smaller AS real bike tire.
     
    Last edited: Jan 11, 2016
  49. LS16BMX

    LS16BMX

    Joined:
    Nov 11, 2011
    Posts:
    94
    Interesting, I think you will have problems with this setup as the bike won't be able to wheelie very well. I just use mesh collider with physics joints, makes much more realistic.
     
  50. the7347

    the7347

    Joined:
    Dec 29, 2012
    Posts:
    33
    @bcman Great job ...

    Here is a video of my motobike physics that I'm doing for my project ... still is very basic and I have much to do.

    [video=youtube_share;LaAZ-G9Piko]http://youtu.be/LaAZ-G9Piko

    As you can see in the video the bike is stable but have several problems, for now I have to do the movements are smoother, and much more.

    The scene is the unity demo car, the bike and the character the download of a page of free 3D models.
     
    Last edited: Jun 29, 2013