# Modify Rigidbody2D Jump distance

I’m using rigidbody2D.velocity to move my character around based on keyboard input. Is there a way to modify the distance portion of the velocity when the character jumps? Right now, the character jumps way too far. I’m currently using addforce, but that doesn’t seem to get me what I want. I pretty much want to half the distance while keeping the time the same.

EDIT: Here’s my current code for the character controller:

``````using UnityEngine;
using System.Collections;

public class AvaController : MonoBehaviour
{
public float maxSpeed = 5f;
private bool facingRight = true;

Animator anim;

bool grounded = false;
public Transform groundCheck;

private float move;

public  bool jumping = false;
public Vector2 jumpForce = new Vector2(120f, 330f);

void Start()
{
anim = GetComponent<Animator>();
}

void Update()
{
print (rigidbody2D.velocity.x);
print (jumping);
// Allow jump in FixedUpdate
JumpUpdate();

// Play crouching animation
if( grounded && Input.GetKey(KeyCode.S) )
{
anim.SetBool("Crouching", true);
}
else
{
anim.SetBool("Crouching", false);
}
}

void FixedUpdate()
{
// Check to see if the player is touching a "ground"
anim.SetBool("Ground", grounded);

Jump();

// If player is not on the ground, player cannot control the character
if (!grounded)
return;

if (anim.GetBool("Crouching") == false)
{
move = Input.GetAxis("Horizontal");
anim.SetFloat("speed", Mathf.Abs(move));

rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
}

if( (move > 0) && (!facingRight) )
Flip();
else if( (move < 0) && (facingRight) )
Flip();
}

void JumpUpdate()
{
if (!jumping) {
if( (anim.GetBool("Crouching") == false) && grounded && Input.GetKeyDown(KeyCode.Space) )
{
jumping = true;
}
}
}

void Jump()
{
if(jumping)
{
anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
anim.SetBool("Ground", false);

if(rigidbody2D.velocity.x != 0)
{
print (rigidbody2D.velocity.x);
}
else
{
rigidbody2D.velocity = new Vector2(0,rigidbody2D.velocity.y);
}

if(rigidbody2D.velocity.y == 0)
jumping = false;
}
}

void Flip()
{
facingRight = !facingRight;
Vector3 avaScale = transform.localScale;
avaScale.x *= -1;
transform.localScale = avaScale;
}
}
``````

This works perfectly if I comment out the rigidbody2d velocity in my horizontal movement code, but then they character can’t move left or right.

You’re jumping on Update but moving on FixedUpdate? AddForce on FixedUpdate as its a physics calc
You can check for the jump button on Update then action it on FixedUpdate.

For mass of 1.

force = mass * acceleration

force = 1 x acceleration.

adding force will accelerate the rigid body over time.
In the y vector that speed will be restrained by the negative acceleration applied to the rigid body by gravity.

Going up at 330units / sec / sec, but decelerating at 9units / sec / sec.
eventually gravity wins and the speed is 0 and you have your max height before you start falling.
The max height is set by the time relationship between your jump force and gravity.
If you decreased the jump force.y you wouldn’t get as high, but wont travel as far because you would be in the air for less time.

You want it to jump as high but not as far?

decreased the jumpForce.x

remember that the jumpForce.x is in addition to your current speed (velocity) so adding force will add acceleration to the velocity of the rigidbody horizontally.

note: (You can try negating the current speed by saying

``````rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
``````

in the beginning of your jump function. Unless you want to jump further when traveling faster)
I’ve not played too much with using AddForce to move a controllable rigidBody but the logic here makes sense.

It will accelerate slower forwards meaning that by the time the jumpForce.y has taken you up to your height and back down you’ve traveled forward less. Then when you’re grounded the movement calc takes care of slowing you down by setting your velocity.x.

With a bit of trial and error with setting your jumpForce.x you can set a distance in x the rigidbody will move during its rise and fall.
There are equations that will allow you to work out → for any given amount of seconds, when going from a speed of 0, how many meters will you travel if constantly accelerating at N meters per sec per sec. But i don’t know them off by heart.

A bit long winded but maybe that helps?
{"ft_lang_label":"__label__en","ft_lang_prob":0.611611,"math_prob":0.91070485,"size":2527,"snap":"2023-40-2023-50","text_gpt3_token_len":659,"char_repetition_ratio":0.14110187,"word_repetition_ratio":0.0,"special_character_ratio":0.2738425,"punctuation_ratio":0.24669604,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96351594,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-24T21:43:52Z\",\"WARC-Record-ID\":\"<urn:uuid:5556597a-b7ec-4d25-958e-bc407dc183cb>\",\"Content-Length\":\"24786\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ce2c7f97-29f1-4cf2-9493-e33b17a93d5b>\",\"WARC-Concurrent-To\":\"<urn:uuid:3bbf6b7a-c092-4b63-aae2-a2c740ead294>\",\"WARC-IP-Address\":\"184.104.178.75\",\"WARC-Target-URI\":\"https://discussions.unity.com/t/modify-rigidbody2d-jump-distance/97277\",\"WARC-Payload-Digest\":\"sha1:DEN5ZQPHJWFSVMG7W23AH7K4XB2QIEBS\",\"WARC-Block-Digest\":\"sha1:VSOOZHPTYYT5OH5BGX4ISHSG7CPZKKJI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506669.30_warc_CC-MAIN-20230924191454-20230924221454-00017.warc.gz\"}"}