- Home /
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.
if it is a rigidbody, then you should use rigidBody.AddForce(Vector3.up, Force$$anonymous$$ode.Impulse);
Answer by ReCoF · Jul 21, 2015 at 11:42 AM
Hi,
You should keep a look at RigidBody.AddForce()
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
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.
Yes, it is affected by gravity. Only problem is that he is a sprite. Should I have given him a 2d rigidbody rather than a 3d rigidbody? Because the gravity does not affect him at all. If you look at the script, I am using Vector 3 for jumping. Should I be using that?
And yes, I did look at that before, but I do not know how to use it. I know unity can give you examples on how to use it, but sometimes they are not very helpful.
Answer by spalmer · Jul 21, 2015 at 03:39 PM
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
If it isn't too much to ask, could you use my script to show how to put your way in? I have seen many problems like this, but I don't know where to put it.
Also, do you know what that other nonstandard code is? (just out of curiosity...)
Sorry I'm not privy to details about their movement.
In your Awake(), just add playerRigidbody.is$$anonymous$$inematic = false;
I don't understand what your old Jump method is doing. Stuff like testing if v == 6 mixed with an assignment is poor style, modifying your input parameters is just confusing. I wish I had time to just write you a class.
Well, actually, I don't $$anonymous$$d...
In that case, could you try a completely new script ins$$anonymous$$d of $$anonymous$$e?
Answer by jonathanhuo11 · Jul 23, 2015 at 01:28 AM
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:
Ground Checking - is my character on the ground? If so, allow him to jump. If not, do not allow him to jump.
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
Answer by ThePhantomPsychic · Jan 27, 2016 at 03:05 PM
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Jumping in 3D 1 Answer
Jumping in only the direction of the last key pressed 0 Answers