Search Unity

jetpack style vertical force

Discussion in 'Scripting' started by chubbspet, Sep 16, 2010.

  1. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Hi guys, it's Friday!!!

    i need to apply a jetpack-style vertical force to my spaceship. The problem is I need it to be applied for as long as the shift key is held IN and not like a jump, for only one frame. I can't figure this out...
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    I got this to work with:

    Code (csharp):
    1. if (Input.GetKeyDown("left shift"))
    2.     {
    3.         constantForce.force = Vector3.up * 8000;
    4.     }
    5.    
    6. if (Input.GetKeyUp("left shift"))
    7.     {
    8.         constantForce.force = Vector3.up * 0;
    9.     }
    Please, if someone have a better way, maybe better performance, please let us know.[/code]
     
  3. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
  4. LoTekK

    LoTekK

    Joined:
    Jul 19, 2010
    Posts:
    136
    Vicenti:
    Depends on how he plans to implement. The way he's set it up works fine. Using GetKey (with ConstantForce) would still require two conditionals, one to set the ConstantForce, and another to set it back to 0.
     
  5. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    But there would only be one check. Slightly faster. :)

    Code (csharp):
    1. if ( Input.GetKey("left shift") )
    2.  constantForce.force = 8000 * Vector3.up;
    3. else
    4.  constantForce.force = Vector3.zero;
    5.  
     
  6. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Thanks guys and btw, I'm retarted lol. my heading was suppose to read it's ALMOST Friday!!! :oops:

    Thanks again
     
  7. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    I put that script on my first person shooter and it gave me this error

    "MissingComponentException: There is no 'ConstantForce' attached to the "FirstPerson Player" game object, but a script is trying to access it.
    You probably need to add a ConstantForce to the game object "FirstPerson Player". Or your script needs to check if the component is attached before using it.

    jet pack 2.Main () (at Assets\jet pack 2.js:4)"

    Any ideas? Thanks!
     
  8. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    In the editor add the ConstantForce component to your game object (Component > Physics > ConstantForce)
     
  9. ant123

    ant123

    Guest

    Joined:
    May 31, 2011
    Posts:
    13
    I've tried all steps mentioned but the worm player i have is still not flying upwards: this is my first code, what am i missing?

    ANSWER: i didnt uncheck kinematic, and the character controller component overrides the constant force physics
     
    Last edited: Jun 19, 2011