Double jump feels really off
Hi there
Pretty new to Unity. I'm messing around, trying to make a simple 2D platformer.
I managed to implement basic movement and jumping. My next challenge was to create a double jump. It "works" but feels really off. Basically what happens is that the later I press the jump button again, the less I actually jump. So as I'm falling down, I barely get propelled up anymore, if at all. So basically, double jumping only really has an effect at the beginning of a jump. And even then, the result varies. I want the effect of the jump to be constant and not to be dependant on my current jump arch.
Here's the code that is relevant to moving and jumping. Note: A lot of the code stems from this video right here.
int count = 0;
public Text countText;
public Text winText;
float maxSpeed = 10f;
public float jumpForce = 70f;
bool isFacingRight = true;
private Rigidbody2D rb;
bool grounded = false;
public bool doubleJump = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
Animator anim;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
winText.text = "";
}
// Update is called once per frame
void Update ()
{
if ((grounded || !doubleJump) && Input.GetButtonDown("Jump"))
{
anim.SetBool("grounded", false);
rb.velocity.Set(rb.velocity.x, 0);
rb.AddForce(new Vector2(0, jumpForce));
if (!doubleJump && !grounded) doubleJump = true;
}
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("grounded", grounded);
if (grounded) doubleJump = false;
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
if ((move > 0 && !isFacingRight) || move < 0 && isFacingRight) Flip();
}
Is there any way I can improve my double jump so that it feels more like in other games and isn't affected by how fast down I'm currently going? (i thought rb.velocity.Set(rb.velocity.x, 0); would fis this but apparently not..)
Answer by Jayrab · Mar 19, 2018 at 06:23 PM
So, I found out what the problem was. I replaced
rb.velocity.Set(rb.velocity.x, 0);
with
rb.velocity = new Vector2(rb.velocity.x, 0);
And now the double jump isn't affected by my fall.
Your answer
Follow this Question
Related Questions
Problem with Jumping code in Unity 2D,problem with Jumping code in Unity 2D 0 Answers
how do i add double jumping into my 2d prefab character controlle 0 Answers
Why only the first frame plays in my jump animation 0 Answers
Error: An object reference is required to access non-static member, help? 2 Answers