Search Unity

Gravity Help

Discussion in 'Physics' started by ItalicHail, May 22, 2018.

  1. ItalicHail

    ItalicHail

    Joined:
    May 22, 2018
    Posts:
    3
    So im trying to implement a feature where you can toggle gravity on the player for a few seconds, but when i go to test it unity completely freezes and im not sure what ive done wrong. heres the code snippet

    private void Update()
    {
    while (Input.GetButton("Jump"))
    {
    ToggleGravity();
    }
    }
    void ToggleGravity()
    {
    if (GravCharge > 0)
    {
    rb.useGravity = false;
    GravCharge -= (1F / GravCharge_use) * Time.deltaTime;
    }
    else
    {
    rb.useGravity = true;
    }
    }
     
  2. Cremator

    Cremator

    Joined:
    Dec 23, 2015
    Posts:
    11
    first of why is you Update void private? it's just void Update(){ } then I think the issue comes from your while loop, try

    Code (CSharp):
    1. if(input.GetKey(KeyCode.YourButton))
    2. {
    3.  
    4. }
     
  3. ItalicHail

    ItalicHail

    Joined:
    May 22, 2018
    Posts:
    3
    thank you! that stopped the crashes but unfortunately now it wont turn off >.< dunno why i didnt think to change that to an if instead of while
     
  4. Cremator

    Cremator

    Joined:
    Dec 23, 2015
    Posts:
    11
    what do you mean by it wont turn off? can you show your entire class?