Search Unity

Bicycle Script

Discussion in 'iOS and tvOS' started by shawnpigott, Mar 25, 2011.

  1. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I'm attempting to modify the StarTrooper player control script to work with a bicycle game. I've managed to strip out most of the things I didn't need and have had good success with the steering. I have run into 2 problems though.

    1. I get a lot of shake/bumps over the land object. I'm using a mesh object with a mesh collider and a capsule collider on the bike.

    2. When I go down hills, I get a large amount of acceleration which results in an uncontrollable bicycle.

    Suggestions appreciated.
    Code (csharp):
    1. #pragma strict
    2.  
    3. var turnSpeed : float = 3.0;
    4. var sensitivity : float = .9;
    5. var forwardForce : float = 50.0;
    6.  
    7. private var euler : Vector3 = Vector3.zero;
    8.  
    9. function FixedUpdate () {
    10.     rigidbody.AddRelativeForce(0, 0, forwardForce);
    11.    
    12.     // Rotate turn based on acceleration       
    13.     euler.y += -Input.acceleration.y * turnSpeed;
    14.    
    15.     euler.x = 0.0;
    16.     euler.z = 0.0;
    17.    
    18.     // Apply rotation and apply some smoothing
    19.     var rot : Quaternion = Quaternion.Euler(euler);
    20.     transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
    21. }
    22.  
    Motion constrain script using only Y motion constrained.
    Code (csharp):
    1. #pragma strict
    2.  
    3. var xMotion : boolean = true;
    4. var yMotion : boolean = true;
    5. var zMotion : boolean = true;
    6.  
    7. @script AddComponentMenu("Physics/Constrain Motion")
    8. function FixedUpdate () {
    9.     var relativeSpeed : Vector3 = transform.InverseTransformDirection (rigidbody.velocity);
    10.    
    11.     if (!xMotion)
    12.         relativeSpeed.x = 0;
    13.     if (!yMotion)
    14.         relativeSpeed.y = 0;
    15.     if (!zMotion)
    16.         relativeSpeed.z = 0;
    17.        
    18.     rigidbody.AddRelativeForce (-relativeSpeed, ForceMode.VelocityChange);
    19. }
     
  2. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    If you are using a bicycle would it not be better to use to wheel shapes