how to fix velocity for jump and movement or fall in 2d pltform game with gravity
Hello everybody ,
Please help me and thank you ,
I use fixed update a movement function with rigidBody2D.Velocity to move my character in 2d platform game (with rigidbody2D.velocity = new vector 2 (movement on x, rigidBody2D.velocity.y) and another jump function Except to jump in addition to this function,
The problem is that gravity is either very slow with a simple jump or very fast with a big fall with sometimes the range of the jump that varies,
Please what is the simplest and fastest and most realistic and adequate formula to determine the rigidBody.velocity in this case and what is the most suitable 2d mode force in this case,
thank you infinitely and good day.
Cordially.
Answer by salamander555 · Aug 03, 2017 at 04:50 PM
Is it possible to paste your exact code? becaue I think you are just making a small mistake.
when you add a force to you velocity.y try just doing this
rigidbody.velocity = new Vector2 (rigidbody.velocity.x, "Jump Power")
this is a much easier way and the gravity should work automaticly on the object
Answer by Bioshok7 · Aug 11, 2017 at 06:52 AM
/**Ok,Here is the code below (not complete it also misses may be block of code)*/
" void FixedUpdate ()
{
if ((!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow)) && Input.GetAxis ("L_XAxis_1") == 0 && Input.GetAxis ("DPad_XAxis_1") == 0) { h = 0; rigidBody.velocity = (new Vector2 (h, rigidBody.velocity.y)); anim.SetFloat ("speed", 0f); sons.audioSource.Stop (); } if (!Input.GetKey (modes.boutonX) && !Input.GetKey (KeyCode.Joystick1Button1)) { sons.audioSource.clip = sons.walk; if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) { h = 1; sons.audioSource.Play (44100); anim.SetFloat ("speed", 1f); } if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) { h = -1; sons.audioSource.Play (44100); anim.SetFloat ("speed", 1f); } rigidBody.velocity = (new Vector2 (h moveForce, rigidBody.velocity.y));
anim.SetBool ("X", false); } if (Input.GetKey (modes.boutonX) || Input.GetKey (KeyCode.Joystick1Button1)) { sons.audioSource.clip = sons.run; if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) { h = 1; sons.audioSource.Play (44100); anim.SetFloat ("speed", 1f); } if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) { h = -1; sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
} rigidBody.velocity = (new Vector2 (h maxSpeed, rigidBody.velocity.y));
anim.SetBool ("X", true); } } } Jump (); }
//function flip too in Update ()
void Jump () {
if ((Input.GetKeyDown (modes.buttonSpace) || (Input.GetAxisRaw ("Jump") > 0)) && jump) { if (nombresaut >= 0) {
jumpNumber--; jumpOk = true;
if (up)
rigidBody.AddForce (new Vector3 (0, -h1 * jumpForce * Time.deltaTime, 0), ForceMode2D.Impulse);
jump = false;
}
}
if (Input.GetAxisRaw ("Jump") == 0 && !Input.GetKeyDown (modes.boutonEspace) && !jump) {
jump = true;
jumpOK = true;
}
}
"
Answer by Bioshok7 · Aug 10, 2017 at 04:58 AM
Here is the code below (not complete it also misses may be block of code)`
void FixedUpdate ()
{
if ((!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow)) && Input.GetAxis ("L_XAxis_1") == 0 && Input.GetAxis ("DPad_XAxis_1") == 0) {
h = 0;
rigidBody.velocity = (new Vector2 (h, rigidBody.velocity.y));
anim.SetFloat ("speed", 0f);
sons.audioSource.Stop ();
}
if (!Input.GetKey (modes.boutonX) && !Input.GetKey (KeyCode.Joystick1Button1)) {
sons.audioSource.clip = sons.walk;
if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) {
h = 1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) {
h = -1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
rigidBody.velocity = (new Vector2 (h * moveForce, rigidBody.velocity.y));
anim.SetBool ("X", false);
}
if (Input.GetKey (modes.boutonX) || Input.GetKey (KeyCode.Joystick1Button1)) {
sons.audioSource.clip = sons.run;
if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) {
h = 1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) {
h = -1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
rigidBody.velocity = (new Vector2 (h * maxSpeed, rigidBody.velocity.y));
anim.SetBool ("X", true);
}
}
}
Jump ();
}
//function flip too in Update ()
void Jump ()
{
if ((Input.GetKeyDown (modes.buttonSpace) || (Input.GetAxisRaw ("Jump") > 0)) && jump) {
if (nombrejump >= 0) {
jumpNumber--;
jumpOk = true;
rigidBody.AddForce (new Vector3 (0, -h1 * jumpForce * Time.deltaTime, 0), ForceMode2D.Impulse);
jump = false;
}
if (Input.GetAxisRaw ("Jump") == 0 && !Input.GetKeyDown (modes.buttonSpace) && !jump) {
jump = true;
jumpOK = true;
}
}
Answer by Bioshok7 · Aug 11, 2017 at 06:52 AM
void FixedUpdate ()
{
if ((!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow)) && Input.GetAxis ("L_XAxis_1") == 0 && Input.GetAxis ("DPad_XAxis_1") == 0)
{
h = 0;
rigidBody.velocity = (new Vector2 (h, rigidBody.velocity.y));
anim.SetFloat ("speed", 0f);
sons.audioSource.Stop ();
}
if (!Input.GetKey (modes.boutonX) && !Input.GetKey (KeyCode.Joystick1Button1))
{
sons.audioSource.clip = sons.walk;
if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0)
{
h = 1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0)
{
h = -1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
rigidBody.velocity = (new Vector2 (h * moveForce, rigidBody.velocity.y));
anim.SetBool ("X", false);
}
if (Input.GetKey (modes.boutonX) || Input.GetKey (KeyCode.Joystick1Button1))
{
sons.audioSource.clip = sons.run;
if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0)
{
h = 1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0)
{
h = -1;
sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
}
rigidBody.velocity = (new Vector2 (h * maxSpeed, rigidBody.velocity.y));
anim.SetBool ("X", true);
}
}
Jump ();
}
//function flip and Jump too in Update ()
void Jump ()
{
if ((Input.GetKeyDown (modes.buttonSpace) ||
(Input.GetAxisRaw ("Jump") > 0)) && jump)
{
if (jumpNumber >= 0)
{
jumpNumber--;
jumpOk = true;
}
if(jumpOk)
{
rigidBody.AddForce (new Vector3 (0, -h1 * jumpForce * Time.deltaTime, 0), ForceMode2D.Impulse);
jump = false;
}
if (Input.GetAxisRaw ("Jump") == 0 && !Input.GetKeyDown (modes.buttonSpace ) && !jump)
{
jump = true;
jumpOK = true;
}
}