- Home /
How do I do a Snappy Jump in 2D with my 2D Jump Script?
I have a working Jump Script, but the Jump is not as Snappy as I need. I want the Player to Jump immidiately, not Float up a bit and then Jump, like in Version 0.0.2 of the Game I'm speaking.
The Jump Script has only the Jump Functions and not the Walk Functions, because I separated the Jump and Walk Scripts. Here is the Jump Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Test2PlayerJump : MonoBehaviour {
public Rigidbody2D rb2d;
public float JumpForce;
public float MaxTime;
private float CurrentTime;
private bool Grounded;
public bool CanHoldJump;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
JumpForce = 30;
MaxTime = 0.25f;
CurrentTime = MaxTime;
Grounded = true;
CanHoldJump = true;
}
// Update is called once per frame
void Update () {
if (CanHoldJump == true)
{
if (CurrentTime >= 0)
{
rb2d.AddForce(new Vector2(0, Input.GetAxis("Jump")) * JumpForce * Time.deltaTime, ForceMode2D.Impulse);
CurrentTime -= Time.deltaTime;
}
}
else if (Grounded == false)
{
CanHoldJump = false;
}
if (Grounded == true)
{
CurrentTime = MaxTime;
}
}
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
Grounded = true;
CanHoldJump = true;
}
Answer by _samuel12345 · Jun 07, 2018 at 04:41 PM
Just change the player's position like this:
transform.position = new Vector2(transform.position.x, transform.position.y + jumpHeight);
This should teleport the player up immediately.
@_samuel12345
Oops. I accidentally imported the Archived Jump Script into this Question.
Now I fixed it so you now see the new Jump Script I'm actually using. :)
This Answer was before the fixed question, where the Script looked like this, which is wrong because the not working archived script, not the working one I'm actually using:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerJump : $$anonymous$$onoBehaviour {
Rigidbody2D rb2d;
public float jumpHeight;
public float jumpStart;
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
void Update () {
//$$anonymous$$ove Player according to Input:
transform.position = new Vector2(transform.position.x, transform.position.y + jumpStart);
rb2d.AddForce(new Vector2(0, (Input.GetAxis ("Jump")) * jumpHeight * Time.deltaTime ), Force$$anonymous$$ode2D.Impulse);
}
}