Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question GravityScale can be set to 1 by key press but back to 0 won't work ?

Discussion in 'Scripting' started by Realspawn1, Sep 17, 2020.

  1. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129
    any ideas :)

    Code (CSharp):
    1.     if (Input.GetKeyDown("d"))
    2.  
    3.         {
    4.             rb.GetComponent<Rigidbody2D>().gravityScale = 0;
    5.  
    6.         }
    7.         if (Input.GetKeyDown("a"))
    8.  
    9.         {
    10.             rb.GetComponent<Rigidbody2D>().gravityScale = 0.1f;
    11.  
    12.    
    13.  
    14.         }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I never use strings myself in those getkey methods, rather I use keycode enum. Iirc, are they not meant to be uppercase though?

    Stick a debug.log call in the if statements to check the code is being executed.
     
  3. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129

    it does get executed but somehow the 0 does not work :)

    Code (CSharp):
    1. public void Update()
    2. {
    3.    
    4.             if (Input.GetKeyDown(KeyCode.A))
    5.             {
    6.                 rb.gravityScale = 1;
    7.             }
    8.         if (Input.GetKeyDown(KeyCode.D))
    9.         {
    10.             rb.gravityScale = 0;
    11.             Debug.Log("working");
    12.  
    13.         }
    14.     }
    15.     }
     
  4. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Maybe try setting useGravity to false instead of setting gravityScale to 0?
     
  5. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129
    Nope i don't get it. On key it uses the gravity given 1 so why won't it go back to zero also with a key stroke :)
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    I remember a question on these forums a few months ago with a similar question, and the gist was... they were misunderstanding how gravity works a little... They expected a falling object to suddenly be slowed down when gravity was changed. Hopefully this is not a similar case?

    How are you verifying that the setting doesn't work? Are you logging it to console with Debug.Log()? Are you observing some behavior indicating it did not work?
     
  7. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129
    I used a Debug.log to see if the key push is activated and it is. However the gravity number remains untouched.
    I am still learning but it's somehow weird to see activate it from 0 works but in reverse it does not :)
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    So you did this and it did not print 0?
    Code (CSharp):
    1. Debug.Log("gravity scale before: " + rb.gravityScale);
    2. rb.gravityScale = 0;
    3. Debug.Log("gravity scale after: " + rb.gravityScale);
     
  9. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129
    gravity.jpg Tried it it does what i needs to be before i1 then after is 0
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    From that log it looks to me like the code is working fine. Are you seeing behavior that indicates it is not?
     
  11. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129
    Yes i have a game object that starts falling down as soon as the gravity is set to 1. On 0 it should stop falling but it does not.
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    That's not quite how gravity works. When you set gravity to 0 it will stop accelerating, but any velocity it has picked up, it will keep. Imagine letting go of the gas pedal in your car. Your car doesn't stop immediately, it just stops accelerating.
     
    sparkwd and Joe-Censored like this.
  13. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    129
    so i have to set the velocity somehow to 0 :) and the gravity makes the velocity have effect. Thank you for explaining this to me.

    Code (CSharp):
    1. public void Update()
    2. {
    3.  
    4.             if (Input.GetKeyDown(KeyCode.A))
    5.             {
    6.                 rb.gravityScale = 1;
    7.             }
    8.         if (Input.GetKeyDown(KeyCode.D))
    9.         {
    10.  
    11.             rb.velocity = Vector3.zero;
    12.             rb.gravityScale = 0;
    13.        
    14.  
    15.  
    16.         }
    17.     }
    18.     }
    So now on D gravity is set to 0 but velocity also :)
     
    PraetorBlue likes this.