Search Unity

How do you change gravity when button is being input?(similar to super smash fastfall)

Discussion in 'Scripting' started by JBjohnnnyboy, Mar 22, 2019.

  1. JBjohnnnyboy

    JBjohnnnyboy

    Joined:
    Aug 31, 2018
    Posts:
    6
    So im currently making a game where are player jump, move right ,and left. But I wanted to add a mechanic where i would fall fast upon inputing a downward input. But cant seem to figure out which code to use.

    My theory of coding it:

    Public void FastFall()
    {
    if input.getKeyDown('s')
    GetComponent.rigidbody2d(addforce)
    }


    im not really good at coding yet but thats what i have in mind. Please help me ill be much appreciated and will make my day
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    If it is using a 2d rigid body, you could just increase the gravity scale.
     
    JBjohnnnyboy likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    Two main methods immediately come to mind. You can modify gravity itself, but since this is global it will affect the physics of every object in the scene.

    https://docs.unity3d.com/ScriptReference/Physics-gravity.html

    The other method is to apply a Constant Force component to an object and modify that.

    https://docs.unity3d.com/Manual/class-ConstantForce.html

    The second one is way more practical than the first. If you create a Constant Force and have a script modify it every frame to point towards a certain spot you can create a spherical gravity effect like the one in Super Mario Galaxy. That was a fun little side project.

    Just keep in mind that global gravity will always be active. If you want to overcome the pull of it you will need a higher force to do so (ie if it's 9.81 down you will need more than 9.81 up to overcome it).
     
    Last edited: Mar 22, 2019
    JBjohnnnyboy likes this.
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Do you need the fall speed to return to normal when the input is released halfway while falling?

    If you do, that might complicate things, probably requiring to manually set the Y axis value.

    If not however, its far simpler to add an impulse force down when the input key is pressed, effectively turning that mechanic into a ground pound.
     
    JBjohnnnyboy likes this.
  5. JBjohnnnyboy

    JBjohnnnyboy

    Joined:
    Aug 31, 2018
    Posts:
    6
    Yeah, that's some of the things I have in mind but I want to change the gravity by inputting a button and then change back to the original gravity when the player touches the ground.

    thanks for your reply tho :)
     
  6. JBjohnnnyboy

    JBjohnnnyboy

    Joined:
    Aug 31, 2018
    Posts:
    6
    Oh alright, ill try that. I'll try updating you once I got it. But thanks for your reply ^_^
     
  7. JBjohnnnyboy

    JBjohnnnyboy

    Joined:
    Aug 31, 2018
    Posts:
    6
    No not on released just a simple fast fall mechanic like in super smash when you press down fall fast while in the air. But alright ill try your suggestion and thanks for the reply :)
     
  8. JBjohnnnyboy

    JBjohnnnyboy

    Joined:
    Aug 31, 2018
    Posts:
    6
    !!! New Update !!!

    So I implemented were I could fast fall while in the air. And im so freaking happy my homies ^_^. Basically what I did is I put a "private float FastFall = -1000f" and set a function of "m_Rigidbody2D.AddForce(new Vector2(0f , FastFall));" and set the input of the button where if a press the button then it will do that. Sorry if im not explaining it as clearly but that's so far I could do so far to do it. But thank you all for the suggestion and help you have given me. And maybe ill have more action to ask in the future. So very much thank you and you have made my day.

    do note that I use a script from a youtube channel "Brackey" from his 2d movement tutorial.
     

    Attached Files: