Search Unity

Rigidbody walker erraticly speeding up

Discussion in 'Scripting' started by morgansaysthis, Jun 16, 2007.

  1. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    im having nothing but problems today.... anyways this is the rigidbody FPS script from here http://forum.unity3d.com/viewtopic.php?t=3222&highlight=rigidbody modified very slightly because im making a side-scrollingish game


    the problem is as soon as it touchs another rigidbody it shoots off in w/e direction it was going 10 times as fast as its supposed to walk with or without my small modifications, what is causing that??
    Code (csharp):
    1. var speed = 8.0;
    2. var gravity = 10.0;
    3. var maxVelocityChange = 10.0;
    4. var inAirControl = 0.1;
    5. var canJump = true;
    6. var jumpHeight = 2.0;
    7. private var grounded = false;
    8. private var groundVelocity : Vector3;
    9. private var capsule : CapsuleCollider;
    10.  
    11. @script RequireComponent(Rigidbody, CapsuleCollider)
    12.  
    13. function Awake ()
    14. {
    15.    rigidbody.freezeRotation = true;
    16.    rigidbody.useGravity = false;
    17.    capsule = GetComponent(CapsuleCollider);
    18. }
    19.  
    20. function FixedUpdate ()
    21. {
    22.    if (grounded)
    23.    {
    24.       // Calculate how fast we should be moving
    25.       var targetVelocity = new Vector3(0, 0, Input.GetAxis("Horizontal"));
    26.       targetVelocity = transform.TransformDirection(targetVelocity);
    27.       targetVelocity *= speed;
    28.        
    29.       // Apply a force that attempts to reach our target velocity
    30.       var velocity = rigidbody.velocity;
    31.       var velocityChange = (targetVelocity - velocity) + groundVelocity;
    32.       velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    33.       velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    34.       velocityChange.y = 0;
    35.       rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    36.    
    37.       // Jump
    38.       if (canJump  Input.GetButton("Jump"))
    39.       {
    40.          rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    41.       }
    42.        
    43.       grounded = false;
    44.    }
    45.    else
    46.    {
    47.       // Add in air
    48.       targetVelocity = new Vector3(0, 0, Input.GetAxis("Horizontal"));
    49.       targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;
    50.        
    51.       rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
    52.    }
    53.    
    54.    // We apply gravity manually for more tuning control
    55.    rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
    56. }
    57.  
    58. function TrackGrounded (col : Collision)
    59. {
    60.    var minimumHeight = capsule.bounds.min.y + capsule.radius;
    61.    for (var c : ContactPoint in col.contacts)
    62.    {
    63.       if (c.point.y < minimumHeight)
    64.       {
    65.          if (col.rigidbody)
    66.             groundVelocity = col.rigidbody.velocity;
    67.          else
    68.             groundVelocity = Vector3.zero;
    69.          grounded = true;
    70.       }
    71.    }    
    72. }
    73.  
    74.  
    75. function OnCollisionStay (col : Collision)
    76. {
    77.    TrackGrounded (col);
    78. }
    79.  
    80. function OnCollisionEnter (col : Collision)
    81. {
    82.    TrackGrounded (col);
    83. }
    84.  
    85.  
    86. function CalculateJumpVerticalSpeed ()
    87. {
    88.    // From the jump height and gravity we deduce the upwards speed
    89.    // for the character to reach at the apex.
    90.    return Mathf.Sqrt(2 * jumpHeight * gravity);
    91. }
    92.  

    and id like to apologize for using the forum so much lately
     
  2. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    The problem seems to be in the "TrackGrounded" function.
    I tested it, and when the capsule hits the other object, it thinks this object is the ground, because the collision happened in the lower part of the capsule.

    Since the script uses the velocity of the "ground" as a starting point for its own velocity, the "ground" has no chance of getting away from the capsule, which will chase it with increasing speed.

    So I suggest that you find a better way of classifying something as "ground"
     
  3. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    ahhh thank you very much