# I need help with a jump.

I am coding a jump, and having a heck of a time trying to figure out how to make it work. It’s a sprite with a 3d rigidbody, because I am making a fan paper mario game. I have been trying to get him off the ground, which I have successfully done, but it’s a bit choppy. What I haven’t been able to do is get him up to about 2 float above the ground, and use gravity to bring him back down.

Here is my coding for all of his movement:

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

public class MarioMovement : MonoBehaviour {
public float speed = -6f;            // The speed that the player will move at.

Vector3 movement;                   // The vector to store the direction of the player's movement.
Animator anim;                      // Reference to the animator component.
Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
public float up = 0f;

void Awake ()
{
// Set up references.
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
playerRigidbody.useGravity = true;
}

void Update ()
{
// Store the input axes.
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
bool u = Input.GetKey (KeyCode.Space);

// Move the player around the scene.
Move (h, v, up);

// Animate the player.
Animating (h, v, up);

Jump (h, v, up, u);
}

void Move (float h, float v, float up)
{
// Set the movement vector based on the axis input.
movement.Set (h, up, v);

// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;

// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);
}

void Animating (float h, float v, float up)
{
// Create a boolean that is true if either of the input axes is non-zero.
bool Running = h != 0f || v != 0f;

// Tell the animator whether or not the player is walking.
anim.SetBool ("Is Running", Running);
anim.SetFloat ("v", v);

if (Running = v == 6f)
anim.SetBool ("Is Facing Up", true);
else
anim.SetBool ("Is Facing Up", false);

}

void Jump (float h, float v, float up, bool u)
{
if (u == true) {
up = -7f;
if (transform.position.y >= 1f)
up = 7f;
}

if (u == false && transform.position.y >= 1f)
up = 7f;

if (up != 0f)
anim.SetBool ("Is Jumping", true);
else
anim.SetBool ("Is Jumping", false);

movement.Set (h, up, v);

// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;

// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);
}
}
``````

It is all in c#, as you now can see, so if anyone can help, please respond with answers in c#. Thank you.

Hi,

You should keep a look at RigidBody.AddForce()

then be sure that your player if affected by the gravity and you just have to find to correct value for your jump in order to be as perfect as you wish.

The main problem you had originally is you were trying to micromanage the jump, and since you call MovePosition each frame, overrides any gravity the physics engine applies. Using AddForce is correct. If you want to calculate how much force to add to get your character to achieve a certain height off the ground at the apex of the jump, you can do that using elementary physics given the character’s mass m and the desired height h and gravity force Vector3 g = Physics.gravity, something like:
` rigidbody.AddForce(-Mathf.Sqrt(2.01f * h * g.magnitude) * g.normalized, ForceMode.VelocityChange);`
I believe the nintendo character has nonstandard gravity physics; he doesn’t obey standard Newtonian rules, has some extra lift force involved with how you’re pushing the button which makes him float slightly.

One more thing: You must set the character’s rigidbody.useGravity = true and rigidbody.isKinematic = false

Hi,

I’ve been using Unity for its 2D mode for a while. If you want a proper jump, then there are a few things to consider:

1. Ground Checking - is my character on the ground? If so, allow him to jump. If not, do not allow him to jump.
2. Jump mechanism - how will I make my character jump? There are many ways you can make a character jump.

So after considering these things, I think the best option for you would be to use layermasking to control your ground collision test. The best way to do so would be to use `Physics.OverlapSphere(Vector3 point, float radius, LayerMask layermask)` to test if a sphere, with a center point located at `point` (point is presumably the location of the character’s feet), and radius of `radius`, overlaps any objects with the layermask of ground. If so, then allow player to jump, because they are on the ground. To provide thrust for the jump, use `Rigidbody.ApplyForce(Vector3 force)`, and apply the necessary force to lift the player off the ground.

If my wording was confusing, or you didn’t get some of the concepts, watch the 2D character controller video. Although it is referring to 2D sprites, the basic concept is the same, and all you need to do is convert to 3D rigidbodies and such.

Jon

Alright, I figured this out on my own, sort of. For starters, is kinematic was on, and second up, my drag and angled drag values were set to infinity.