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 pokedmund · Jul 28, 2014 at 08:29 PM · raycastjumpboolground

The grounded bool of my jump function doesn't become true, using ray cast

Hi all,

Would be grateful for some help.

I've created a capsule with a box collider. I've attached an empty game object to the bottom of the capsule. The game is on a 2D plane.

I've created a jump function using rigid body.addforce. I've also attached a ray cast script onto the empty gameObject at the base of my capsule (the player).

I believe I've set it so that, every time the ray cast detects anything below it (in my case, a plane that I'm using as the floor), it should change a bool value (I've called it Grounded) to true. As long as Grounded is true, it should allow me to access a Jump function on another script to allow my player to jump.

Once my player jumps, I should have a script that allows me to change the Grounded bool value back to false, so that my player can't double jump by accident.

However, after I have jumped once, when my character lands back onto the floor again, the bool value remains on false. Is there something wrong with my script?

The jump function on my playerMove script

 void Update () 
     {
         transform.rotation = Quaternion.Euler (lockPos, lockPos, lockPos);
 
         Vector2 x = Input.GetAxis ("Horizontal") * transform.right * Time.deltaTime * speed;
 
         transform.Translate (x);

 // If "Jump" is pressed and player grounded == true

         if (Input.GetButtonDown ("Jump") && (grounded)) 
         {
             Jump ();
         }
 
     }

 //Player jumps and the grounded bool value becomes false to prevent double jump
     public void Jump()
         {
             rigidbody.AddForce(Vector2.up * jumpForce);
             Debug.Log ("Jump has been pressed");
             grounded = false;
             Debug.Log (grounded);
         }







The ground check script on my empty game object that holds the ray cast on the character

     private float distanceToGround = 0.15f;
     public Transform player; //allows me to access the player move script
  
 
        public LayerMask GroundLayer;
 void Update () 
     {
         
 
     PlayerMove playerScript = player.GetComponent<PlayerMove>();
         
                 RaycastHit hit;
         
                 Ray groundedRay = new Ray (transform.position, -Vector2.up);
         
                 Debug.DrawRay (transform.position, -Vector2.up * distanceToGround);
     
     //If the ray cast hits something below, the grounded bool value becomes true?
                 if (Physics.Raycast (groundedRay, out hit, distanceToGround)) {
                                 playerScript.grounded = true;
                                 Debug.Log ("Player is touching ground");
                 }
             }









Comment
Add comment · Show 6
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 gcoope · Jul 30, 2014 at 10:40 PM 0
Share

Is the Debug.Log("Player is touching ground") getting called at all?

avatar image pokedmund · Jul 30, 2014 at 10:47 PM 0
Share

Yes. I have my player suspended in mid at the start. Once I hit play, the player will fall to the ground. Once it hits the ground, the console does indeed print out that the player is touching the ground. I am then able to hit the spacebar to make the player 'jump'. But when the player lands back onto the floor again, it doesn't register that the player is touching the floor :(

avatar image gcoope · Jul 30, 2014 at 10:51 PM 0
Share

Alright, how often are you calling the Raycast function? From your code it looks like it isn't in Update?

avatar image pokedmund · Jul 30, 2014 at 11:28 PM 0
Share

Apologies gcoope. I forgot to include the Update() in the above script, but in my code, it is definitely in Update, but in the GroundCheck update script that I have attached to an empty game object, which is a child to the player object.

avatar image Yofurioso · Jul 31, 2014 at 12:08 AM 0
Share

Have you tried increasing the distanceToGround variable gradually? $$anonymous$$aybe you just need a longer distanceToGround, say 0.175f

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Darth Futuza · Jul 31, 2014 at 01:40 AM

  PlayerMove playerScript = player.GetComponent<PlayerMove>();
  
                 RaycastHit hit;
  
                 Ray groundedRay = new Ray (transform.position, -Vector2.up);
  
                 Debug.DrawRay (transform.position, -Vector2.up * distanceToGround);
  
     //If the ray cast hits something below, the grounded bool value becomes true?
                 **if (Physics.Raycast (groundedRay, out hit, distanceToGround)) {
                                 playerScript.grounded = true;**
                                 Debug.Log ("Player is touching ground");
                 }
             }

The bolded part is probably causing the problem. Are you sure playerScript.grounded actually writes to grounded? Is grounded a public/private variable?

Comment
Add comment · Show 1 · 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 pokedmund · Jul 31, 2014 at 01:44 AM 0
Share

grounded is a public bool variable on the player$$anonymous$$ove script (which I access from here). But increasing the distanceToGround variable for the ray cast did help a lot! Although it seemed to be only temporary...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RayCast to test if player is grounded 1 Answer

Raycasting not turning off jump. Would really love some help on my code. 2 Answers

Physics2D Raycast stops detecting collision 0 Answers

Booleans are not working 1 Answer

A FLAWLESS way to check if character is grounded? 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