Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jaybaloyi · Feb 20, 2021 at 05:50 PM · jumpendless runner

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;
     }
 }




}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Feb 20, 2021 at 06:20 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jaybaloyi · Feb 22, 2021 at 06:52 AM 0
Share

@ 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.

avatar image DevManuel jaybaloyi · Feb 22, 2021 at 07:00 AM 1
Share

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;
      }
  }
 

avatar image jaybaloyi DevManuel · Feb 22, 2021 at 07:57 AM 0
Share

Thanks a bunch.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges