Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jetpacks Gravity Help (C#)

Discussion in 'Scripting' started by Hydrax, Sep 24, 2014.

  1. Hydrax

    Hydrax

    Joined:
    Aug 2, 2014
    Posts:
    18
    I need a bit of help with making a simple jetpack. I have the jetpack part sorted (code below) I just have problems with the gravity part.

    Basically when I start the game I fly upwards to space and if I press shift to use the jetpack logic I stop and then won't fall back down to earth (picture below).

    If anyone can help me with this I will greatly appreciate it!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Jetpack : MonoBehaviour {
    5.  
    6.     public float forceAmount;
    7.  
    8.     // Update is called once per frame
    9.     void FixedUpdate ()
    10.     {
    11.         if (Input.GetKey (KeyCode.LeftShift))
    12.         {
    13.  
    14.             rigidbody.AddForce (Vector3.up * forceAmount);
    15.         }
    16.     }
    17. }
     

    Attached Files:

  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Does the object with the Jetpack on it have gravity applied?
     
  3. Hydrax

    Hydrax

    Joined:
    Aug 2, 2014
    Posts:
    18
    Yes. The script is attached to the standard first person controller and that has a rigidbody attached with gravity unchecked as that would give the aforementioned effect of blasting into space until you press shift.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Um. I'm confused. I asked if gravity is applied, and you said both "yes" and "gravity unchecked." These are contradictory claims.

    I also don't understand how having gravity would ever have the effect of "blasting into space." I normally think of space as up, and gravity accelerates things down... have you built your world upside down, or what?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,503
    Generally for a jet pack effect, you leave the gravity ON, then when the player hits the power button, you add more force upwards than gravity (the jet pack's thrust) and you accelerate up. Try that and I bet it will feel right. Do not turn off gravity, just put more up force than gravity (which is what amjetpack actually does) and you should go upwards.

    Kurt
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I found jetpacks worked better if you turned gravity off while it was under power.
     
  7. Hydrax

    Hydrax

    Joined:
    Aug 2, 2014
    Posts:
    18
    Sorry I should have explained myself a bit better. I have a rigidbody component if I have the gravity on the player will fly off into nowhere. If I turn it off I stay on the ground and no I didn't build my world upside down.

    Basically now the player can fly upwards but won't fall down. Is there a better way to do this rather than using the rigidbody?

    Thanks a lot for your help.
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Not sure about others, but that still seems odd. Where would your player fly off? Why would he fly off? Is your jetpack not attached to the player?
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I agree. I think it's time to back up a step. If you make a cube, place above the ground (a plane or terrain), and add a rigidbody, it should fall until it hits the ground, and then just sit there.

    If you see it instead "fly off into nowhere" then something really really strange is going on, and I think you should start by figuring out what that is.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    sounds like your gravity has been reversed.

    you been messing with the project gravity settings?
     
  11. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    The standard first person controller has no rigidbody and use a CharacterMotor script with a default field Gravity = 20.
    Probably you have to disable that script when using jets (or modify it in same way) and reenable when you want to go down.
     
  12. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    If you change line 419 in CharacterMotor.js like this, you can jumpjet with the default fpscontroller. Eventually adjusting Jumping parameters in the inspector too.
    Code (JavaScript):
    1. if (true/*grounded*/) {
     
  13. Hydrax

    Hydrax

    Joined:
    Aug 2, 2014
    Posts:
    18
    It does sound strange I give you that and I honsetly have no clue what was going on. I did think it might have something to do with the rigidbody conflicting with the one of the other scripts on the player, then I could wrong.

    I am now using the new First Person Controller from the sample assets pack. It all ready has a rigidbody on it and It works fine with my script in my first post. I just had to change the input key from Left Shift to Left Control as left shift is used for sprinting in the new controller.

    Thank you to everyone for their replies and putting up with my poor explanation of the situation.