Vector2.up stops working after jumping a few times?
This is my first project on Unity and I'm making a 2d platformer. The script works fine for the first few tries then my character just stops jumping. When jumping stops working
Debug.Log(numJumps + " notGroundedJump");
is still being called when I press the W key even though my character is grounded.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
//walk
public float speed;
public float jumpForce;
private Rigidbody2D rb;
private float moveInput;
//jump
public Transform groundCheck;
public float checkRadius;
public LayerMask defineGround;
public static int numJumps;
public static int jumpResets;
private bool isGrounded;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, defineGround);
moveInput = Input.GetAxisRaw("Horizontal");
float walkSpeed = moveInput * speed;
rb.velocity = new Vector2(walkSpeed, rb.velocity.y);
}
void Update()
{
if (isGrounded == true)
{
numJumps = jumpResets;
}
if ((Input.GetKeyDown(KeyCode.W)) && (numJumps > 0))
{
rb.velocity = Vector2.up * jumpForce;
numJumps--;
Debug.Log(numJumps + " notGroundedJump");
}
else if ((Input.GetKeyDown(KeyCode.W)) && (numJumps == 0) && (isGrounded == true))
{
rb.velocity = Vector2.up * jumpForce;
Debug.Log(numJumps + " groundedJump");
}
}
}
you should leave all the physics code to the fixedupdate dont mix update and fixedupdate for rigidbody velocitys, vector2.up is a constant is always (0,1) is imposible to change its value, debug.log the jumpforce inside the if in case you are setting up to 0 anywhere. the notgrounded is being logged because when the object is ground you set up numJumps to jumpReset value never taking into account if its grounded or not, the last if will never be called because it will never be grounded AND numJumps be 0.
Your answer
Follow this Question
Related Questions
Moving and jumping 1 Answer
audio keeps playing despite oneShot 1 Answer
I have gotten all these errors when trying to script ? 2 Answers
Player jumping issue 2 Answers