- Home /
Can only jump once when game starts Unity
Hope you doing well I just started game dev, in fact, this is my first game. Yesterday before making my pc sleep my player scripts were working correctly and the character could jump every time I pressed space. Today I switched on my pc and looked for a way to add stamina to my guy when he drinks coffee, and it works but then I realized that I could only jump once after the game started. I tried deleting the coffee loot but the problem persists.
I would be greatly appreciated If someone can help, Thank you.
Here is the code
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine.SceneManagement; using UnityEngine;
public class Movement : MonoBehaviour { Rigidbody2D rb; float speed = 0.5f;
public float jumpforce = 2.0f;
public Vector3 jump;
public bool isGrounded;
float stamina = 100f;
float maxStamina = 100f;
private Animator myAnimator;
public Slider slider;
// Start is called before the first frame update
void Start()
{
jump = new Vector3(0f, 2.0f, 0f);
rb = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
rb.transform.Translate(5f *speed * Time.deltaTime, 0f, 0f);
Jump();
slider.value = stamina;
if (stamina <=maxStamina && speed <= 0.5f)
{
stamina -= 10f * Time.deltaTime;
}
if (stamina <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
if (speed < 0.5f)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
//myAnimator.SetBool("isjumping",false);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space)&& isGrounded)
{
rb.AddForce(jump*jumpforce,ForceMode2D.Impulse);
isGrounded = false;
myAnimator.SetBool("isJumping", true);
}
myAnimator.SetBool("isJumping", false);
}
private void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision2D.gameObject.tag == "Coffe")
{
stamina = stamina + 10f;
}
}
}
I highly suspect that your ground check is the issue. I don't see the part of your code that looks for ground and sets IsGrounded to true. Could you post that part please? @jaybaloyi
Answer by MUG806 · Feb 20, 2021 at 10:28 PM
You have it so the jump can only happen when the character is grounded. When the character jumps you are marking them as not grounded, and then you aren't ever marking them as grounded again. Unless you are setting "isGrounded = true" elsewhere.
@ MUG806 thanks, it works now, but then my character can jump more than once now. If you can help with that too that would be great but otherwise thank you.
You have to check when the player reaches the ground:
private void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision.collider.gameObject.layer == LayerMask.NameToLayer("ground")) // layer-name
{
Debug.Log("Player is on groundl");
isGrounded = true;
}
}
Your answer
Follow this Question
Related Questions
How Do I Create Random Platforms in a 2d Game? 0 Answers
Only Collide When Certain Animation is Not Playing 0 Answers
Enable/Disable CharMotor jumping 1 Answer
Camera follow player jumping? 2 Answers
3D Jump Colission Detection 1 Answer