Downwards Stomp

Hi guys, I’m trying to build a 2D platformer from scratch and I’m trying to make my character do a downwards stomp when down arrow is pressed (think something like dead cells down stomp). I was wondering what is wrong with my script for the stomp.

if (Input.GetKeyDown(KeyCode.DownArrow)) { 
     Debug.Log("DOWN ARROW PRESSED");
     rb.AddForce(Vector2.down*downStompForce);
}

For a downwards stomp, you might get a better effect changing Rigidbody2D.velocity directly:

if (Input.GetKeyDown(KeyCode.DownArrow)) {
    // Gets rid of all previous velocity for a crunchy stomp.
    rb.velocity = Vector2.down * stompSpeed;
}

The AddForce() function has an optional second ForceMode parameter which you might find useful. There are four possibilities:

  1. ForceMode.Force: This will apply your force for the duration of one frame. Your object’s velocity will change by (force / mass) * Time.fixedDeltaTime (I can’t find any documentation on this unfortunately, but it is my best guess based on the fact that physics ticks happen at the rate of FixedUpdate).
  2. ForceMode.Acceleration: This will interpret the force parameter as an acceleration and will apply it for one frame. AddForce(accel, ForceMode.Acceleration) is equivalent to AddForce(accel * rb.mass, ForceMode.Force).
  3. ForceMode.Impulse: This will interpret the first parameter as an “impulse” which is a change in momentum. Your object’s velocity will change by impulse / mass.
  4. ForceMode.VelocityChange: This will interpret the first parameter as a change in velocity. It’s the same as adding to the velocity vector directly (or using AddForce(velocity * rb.mass, ForceMode.Impulse).