How do I set up a Jump Distance on unity ?

So I am trying to make a platformer right and i followed a lot of tutorials but mostly this one : Recorded Video Sessions on 2D in Unity 4.3 - Unity Learn . With it I learned a lot of things and i can make my character run and jump now, but id like to know if there is a way to set a particular jump distance so it can be more precise and controllabe. Here is the script : PlayerControllerScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControllerAstronaut : MonoBehaviour {

public float maxSpeed = 10f;
bool facingRight = true;

Animator anim;

bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700;

void Start () {

anim = GetComponent();

}

void FixedUpdate () {

grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool(ā€œGroundā€, grounded);

anim.SetFloat(ā€œvSpeedā€, GetComponent().velocity.y);

if (!grounded) return;

float move = Input.GetAxis(ā€œHorizontalā€);

anim.SetFloat(ā€œSpeedā€, Mathf.Abs(move));

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

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

void Update ()
{
if(grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool(ā€œGroundā€, false);
GetComponent().AddForce(new Vector2(0, jumpForce));

}

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

I am new so most of this stuff can seems easy to do but i dont know how to do it :confused: I tried to set it up myself but it still didnt work out.

P.S.
My character sometimes trip on the ground and fall making him strange to control since he is moving on his back/belly depending on which side i move him. I dont know how to fix this but I think its because of the colliders, maybe ?

I found how to do the jump distance but i am still tripping.
New Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControllerAstronaut : MonoBehaviour {

public float maxSpeed = 10f;
bool facingRight = true;
public float jumpDist = 5f;

Animator anim;

bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700;

private Rigidbody2D playerRB;

void Start () {

anim = GetComponent();
playerRB = GetComponent();

}

void FixedUpdate () {

grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool(ā€œGroundā€, grounded);

anim.SetFloat(ā€œvSpeedā€, GetComponent().velocity.y);

if (!grounded) return;

float move = Input.GetAxis(ā€œHorizontalā€);

anim.SetFloat(ā€œSpeedā€, Mathf.Abs(move));

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

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

void Update ()
{
if(grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool(ā€œGroundā€, false);
GetComponent().AddForce(new Vector2(0, jumpForce));
playerRB.AddForce(Vector2.up * jumpDist);

}

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

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == ā€œScreenEdgeā€)
{
Destroy(gameObject);

}

if (GetComponent().gameObject.tag == ā€œenemyā€)
{
GetComponent().Damage ();
}
}
void Hit()
{
if (Random.value >= 0.5f)
{
playerRB.AddForce(Vector2.up * (jumpDist * 1.3f));
playerRB.AddForce(Vector2.left * (jumpDist * 1.3f));
} else
{
playerRB.AddForce(Vector2.up * (jumpDist * 1.0f));
playerRB.AddForce(Vector2.left * (jumpDist * 1.0f));
}
}
}

If you don’t want your character to rotate, you can set the rigidbody constraints to lock Z rotation.

Thank you !

1 Like