Search Unity

maxVelocity

Discussion in 'Scripting' started by User340, Mar 30, 2007.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Is there a command, or a simple way, that limits the velocity of an rigidbody?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Code (csharp):
    1.  
    2. var maxVel : float = 1.0;
    3.  
    4. function FixedUpdate() {
    5. if (rigidbody.velocity.magnitude > maxVel) {
    6. rgidbody.velocity = rigidbody.velocity.normalized * maxVel;
    7. }
    8. }
    9.  
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    It works great, thanks. I don't understand what the word float does though.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It makes the variable of type float. In other words, floating point compared to integer. Note however that there's no difference between using "var maxVel : float = 1.0;" and "var maxVel = 1.0;" because using 1.0 automatically makes maxVel of type float. On the other hand, using "var maxVel : float = 1"; makes maxVel a float, whereas "var maxVel = 1;" makes it an integer, since a number without a decimal is interpreted as an integer unless you say otherwise. I'm normally in the habit of explicitly defining types all the time, just in case I use "1" instead of "1.0" (or whatever), and I want it to be a float. Saves some potential aggravation, at the expense of some possibly unneccesary extra typing.

    --Eric