- Home /
Question by
$$anonymous$$ · Jun 09, 2018 at 12:09 PM ·
2d game2d-platformerphysics2djumping2d-gameplay
How do I do a Snappy Jump in 2D with a 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;
}
Comment
Your answer
Follow this Question
Related Questions
Player getting stuck between grounds 0 Answers
OnCollisonEnter2D. 1 Answer
Death Counter dont work when changing scenes 2 Answers
2D platformer squashed 1 Answer