Search Unity

Keep adding Thrust

Discussion in 'Scripting' started by PixelCloudInc, Feb 12, 2019.

  1. PixelCloudInc

    PixelCloudInc

    Joined:
    Dec 26, 2017
    Posts:
    134
    I'm trying to make a flight controller and I've got everything but the forward movement. i tried using the 'Auto Move And Rotate' script from the standard assets but it work that great.
    So I tried using AddForce but the problem is It just adds force once and then my aircraft slowly loses speed ( like kicking a ball) and stalls.
    Is there a way to keep adding velocity/thrust?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,671
    When using AddForce for something like aircraft thrust you don't just call it once. You'd call it with a small force applied each FixedUpdate until the player shuts off the engine. You'd use one of the two continuous force modes. Calling AddForce just once is for instances like when you want to fire a cannonball (and in that case you'd probably want force mode impulse and a single large force applied).

    https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
    https://docs.unity3d.com/ScriptReference/ForceMode.html
     
    SparrowsNest likes this.
  3. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,725
    Well, a real aircraft of anykind will slowly lose velocity on the horizontal axis due to air resistance and gain velocity down up until terminal velocity(the speed where drag and gravity is equal for an object of a given shape and weight) due to gravity.

    A plane doesn't get a kick on the runway and glides the rest of the way(thats a glider, hehe) he uses engine to add trust.

    I don't think the standard "Auto Move And Rotate" is want you want because I assume it uses transform changes rather than rigidbody, but I don't really know, never used it.

    ofc there's a way to keep adding velocity, what you want is basically this

    Code (CSharp):
    1. float trust = whatever;
    2. Rigidbody rb; //attached rigidbody
    3.  
    4. void FixedUpdate(){
    5.  
    6. if(Input.GetKey(KeyCode.w)){
    7. rb.AddRelativeForce(0f,0f, trust, ForceMode.Force);
    8. }
    9. }
    the FixedUpdate as you should know is called once every physics tick, the if statement will return true as long as the key 'w' or whatever you say is held down, this is the only time you can take input in FixedUpdate, if you use the other versions GetBlahDown or GetBlahUp (it applies to all the input versions) you should use it on the Update and set a flag that you read in FixedUpdate.
    Code (CSharp):
    1. bool flag = false;
    2.  
    3. void Update(){
    4. if(Input.GetKeyDown(KeyCode.x))
    5. flag = true;
    6. }
    7.  
    8. void FixedUpdate(){
    9.  
    10. if(flag){
    11. //Do stuff
    12. flag = false;
    13. }
    14. }
    the reason is that you can miss an input because it runs by the visual updates and if you have two frames before a physics tick and you clicked a button on the first frame the second is gonna override what you did and you won't detect said input

    "AddRelativeForce" adds a force relative to your coord system, so no matter where you face you will add a forward force(at least with the snippet i gave you where you add force only on the Z axis), also you don't have to say "ForceMode.Force", it's the default one if you don't specify.

    edit: you also have this kajigger: https://docs.unity3d.com/Manual/class-ConstantForce.html
    but I never really found a use for it, not saying it ain't good for nothing though.
     
  4. PixelCloudInc

    PixelCloudInc

    Joined:
    Dec 26, 2017
    Posts:
    134
    Thing is the thrust isn't really controlled by the player it's "auto thrust" if you know what I mean.
    P.S - If you know anything about aircraft the plane the player controls is a mig 21 so a mig 21 glider is... um...
     
  5. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,725
    I dont know what you mean, if you mean its always on you can skip the 'if' check is my example or use the constant force component.
    A mig 21 is a super sonic craft from a quick googling, what of it of?
    I dont see a question here.
     
  6. PixelCloudInc

    PixelCloudInc

    Joined:
    Dec 26, 2017
    Posts:
    134
    Oh you don't know about aircraft sorry...
    A mig 21 glider sounds funny to me
    So basically remove the input.keydown stuff?
     
  7. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,725
    I never suggested anything along the lines of a mig 21 glider.

    just put this in fixed update (with out the 'if' checking for input)
    Code (CSharp):
    1. rb.AddRelativeForce(0f,0f, trust, ForceMode.Force);
    2.  
    or use the constant force component.
     
  8. PixelCloudInc

    PixelCloudInc

    Joined:
    Dec 26, 2017
    Posts:
    134
    Ok i tried the method you stated but my plane is reaching speeds of 14,000+ KMPH but the plane's max speed is supposed to be 2229 KMPH anyway to clamp it?
    PS - after like 1 minute this thing enters the 4th dimension and attempts to warp the thing just melts into a giant blob of polygons it looks scary
     
    Last edited: Feb 13, 2019