Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by woollykat · Oct 07, 2018 at 11:52 PM · jumpjumpingjumping objectgroundedisgrounded

isGrounded not working,is grounded stops players from jumping

When I add in isGrounded to this "if (Input.GetButtonDown ("Jump") && isGrounded) ".

It stops the player from jumping. Below is the player controller, I have added in everything when following the video.

The "Is grounded" automatically unticks when I play the game. I think this might be a problem but I am not sure why this unticks?

My code looks like this. My player stops jumping s soon as I add in && isGrounded. I have no idea what the issue might be. Anyone any help please??

using UnityEngine;

using System.Collections.Generic;

using System.Collections;

public class PlayerController : MonoBehaviour

{

 public float moveSpeed;

 private Rigidbody2D myRigidbody;

 public float jumpSpeed;



 public Transform groundCheck;

 public float groundCheckRadius;

 public LayerMask whatIsGround;



 public bool isGrounded;



   // Use this for initialization

 void Start()

 {

     myRigidbody = GetComponent<Rigidbody2D>();

 }

 // Update is called once per frame

 void Update()

 {

     isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround); 

     if (Input.GetAxisRaw("Horizontal") > 0f)

     {

         myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);

     }

     else if (Input.GetAxisRaw("Horizontal") < 0f)

     {

         myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);

     }

     else

     {

         myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y, 0f);

     }

     if (Input.GetButtonDown ("Jump") && isGrounded)

     {             myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);

     }

 }

}

alt text

,When I add in isGrounded to this "if (Input.GetButtonDown ("Jump") && isGrounded) ".

The "Is grounded" automatically unticks when I play the game. I think this might be a problem but I am not sure why this unticks?

My code looks like this. My player stops jumping as soon as I add in "&& isGrounded". I have no idea what the issue might be. Anyone any help please??

using UnityEngine; using System.Collections.Generic; using System.Collections;

public class PlayerController : MonoBehaviour

{

 public float moveSpeed;

 private Rigidbody2D myRigidbody;

 public float jumpSpeed;



 public Transform groundCheck;

 public float groundCheckRadius;

 public LayerMask whatIsGround;



 public bool isGrounded;



   // Use this for initialization

 void Start()

 {

     myRigidbody = GetComponent<Rigidbody2D>();

 }

 // Update is called once per frame

 void Update()

 {

     isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround); 

     if (Input.GetAxisRaw("Horizontal") > 0f)

     {

         myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y, 0f);

     }

     else if (Input.GetAxisRaw("Horizontal") < 0f)

     {

         myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y, 0f);

     }

     else

     {

         myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y, 0f);

     }

     if (Input.GetButtonDown ("Jump") && isGrounded)

     {             myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);

     }

 }

}

alt text

screen-shot-2018-10-05-at-164457.png (50.2 kB)
screen-shot-2018-10-05-at-164457.png (50.2 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiGiaCom-Tech · Oct 12, 2018 at 10:02 PM

Not an expert on this but ... if this is physics .. shouldn't this code be within a 'FixedUpdate'? However, it looks like the function you are calling to set 'IsGrounded' returns a Collider not a Boolean so you should be checking if the collider returned is null or not and then setting the 'IsGrounded' flag accordingly. That is, assuming if you are in collision with the 'Ground' then true else false would be something like...

 isGrounded = (Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround) != null);

The only other thing that comes to mind is the 'What Is Ground' setting of 'Ground' ... perhaps 'Ground' isn't what you think it is.

Comment
Add comment · 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

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

156 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 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

Jump, if I touch button,Jump if I pres the touch button 0 Answers

Making an object jump (without rigidbodies), while using enum + gravity 0 Answers

Unity 2D Enemy jump Question (Frog jump) 1 Answer

Can't jump/buggy when standing next to an object. 0 Answers

Need help with Character Jump! 0 Answers


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