Search Unity

Jetpack

Discussion in 'Scripting' started by darthevil, Sep 21, 2010.

  1. darthevil

    darthevil

    Joined:
    Sep 6, 2010
    Posts:
    13
    Hi i'm trying to make a jetpack for my character and i've come across two problems.

    1) The script doesn't work on my player but does on other objects. I think it's because i've got a rigidbody on my player. How can I temporarily disable it?

    2) When the script is working it only goes up a small amount every time a key is pressed. I need it to be a smoother movement.

    Here's my code:
    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. function Update () {
    5.    
    6.     if(Input.GetKeyDown("left shift")) {
    7.     target.position += Vector3.up;
    8.     }
    9. }
    10.  
    General ideas on how to make the script better would be appreciated.
     
  2. Ullukai

    Ullukai

    Joined:
    Aug 24, 2010
    Posts:
    746
    maybe you could use the plane script on it ? or modify it to the keys u want to use for it ... its somewhere in the forums here that plane script ....or just add more force to your script there ? example Vector3.up * 200
    or so some other number
     
  3. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    You want to be adding velocity, not teleporting upwards
    (".position =" is == teleporting)
    And you want to be doing it constantly, not only once when you press the key

    Code (csharp):
    1. var boostFactor : float = 100; // or whatever
    2.  
    3. function Update () {
    4.  
    5. if ( Input.GetKey("left shift") ) {
    6.  target.rigidbody.velocity += target.transform.up * boostFactor;
    7. }
     
  4. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  5. darthevil

    darthevil

    Joined:
    Sep 6, 2010
    Posts:
    13
    @Vicenti

    Sorry but the script isn't working, I'm not getting any errors but when I press the key nothing happens.

    @ivkoni

    I did look at that but in that it works as a higher jump. I want my player to stay in the air, sorry if that wasn't clear.
     
  6. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    You must have something else influencing the rigidbody's velocity or position, or you've set the boostFactor too low.
     
  7. turtle_jugernaut

    turtle_jugernaut

    Joined:
    Jun 15, 2010
    Posts:
    14
    i am finding the script from vicenti doesn't work unless you remove the character controller :/ but if i do that my moving around script wont work...
     
  8. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    maybe you could use something like
    Code (csharp):
    1. transform.rigidbody.velocity = Vector3(0, 1, 0);
    2. transform.rigidbody.useGravity = false;
    and then when you reach max height, set y velocity to 0. then if you release the jetpack button, you apply gravity and stop with the velocity. you can set up a max y height, and then make it so it cant go any higher
     
  9. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    You may also set the following: rigidbody.isKinematic = true; I think that you should look at the solution which ivkoni has suggested. You have to modify it to make it work the way you want it.
     
  10. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    The character controller overrides physics movement already. You have to handle the character's Y-axis velocity yourself inside the script that's already handling movement.

    That is, somewhere you have ".Move( amount )" (or maybe .SimpleMove)

    with amount being a vector3. Keep track of a "local gravity" for the player and do something like

    Code (csharp):
    1. if ( jetpackOn )
    2.   localGravity += jetpackAcceleration * Time.deltaTIme;
    3. else
    4.   localGravity -= 9.81 * Time.deltaTime;
    5.  
    6. charactercontroller.Move( Vector3( x, localGravity, z ) );
     
    Last edited: Jan 6, 2011