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 JuanseCoello · Dec 05, 2014 at 03:22 PM · raycastswitchresetisgrounded

How can I reset a function (boolean), like ON and OFF?

I have a boolean calles isgrounded, what I want to do in the function is to reset it, so that it turns true or false, if (hitInfo.distance < 0.5f)or (hitInfo.distance > 0.5f).

Somehow it is not working, but the raycasting works fine, I just dont know how to reset it. Because the order of the code only reads the last condition (if) and assumes it is true or false, but never resets it.

This is my function:

 void IsGrounded()
 {

     RaycastHit hitInfo;
     if (Physics.Raycast(rb.transform.position + Vector3.down * 1f, Vector3.up * 5f, out hitInfo, Mathf.Infinity))
     {

         if (hitInfo.distance > 0.5f) // Change .1f to what you need
             Debug.DrawRay(rb.transform.position + Vector3.down * 1f, Vector3.up * 5f, Color.blue);
         
         isgrounded = false;

         if (hitInfo.distance < 0.5f)
             Debug.DrawRay(rb.transform.position + Vector3.down * 1f, Vector3.up * 5f, Color.red);

         isgrounded = true;
     } 
  } 
Comment
Add comment · Show 2
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 Sessional · Dec 05, 2014 at 03:36 PM 0
Share

Are you trying to set isgrounded to false when hitInfo.distance > 0.5f and to true when hitInfo.distance < 0.5f?

Right now both your isgrounded sets will be called every time in that function because they are not truly inside the if statement.

avatar image JuanseCoello · Dec 05, 2014 at 03:43 PM 0
Share

thanks, so how do i fix it? can you put it on code please?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Baste · Dec 05, 2014 at 03:43 PM

Add braces to move the assignments inside the if-statements:

 if (Physics.Raycast(rb.transform.position + Vector3.down * 1f, Vector3.up * 5f, out hitInfo, Mathf.Infinity)) {
 
     if (hitInfo.distance > 0.5f) { // Change .1f to what you need
         Debug.DrawRay(rb.transform.position + Vector3.down * 1f, Vector3.up * 5f, Color.blue);
 
         isgrounded = false;
     }
 
 
     if (hitInfo.distance < 0.5f) {
         Debug.DrawRay(rb.transform.position + Vector3.down * 1f, Vector3.up * 5f, Color.red);
 
         isgrounded = true;
     }
 }

You should also do some more coding tutorials, this is rather basic.

Comment
Add comment · Show 2 · 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 JuanseCoello · Dec 05, 2014 at 03:47 PM 0
Share

thanks, it works, I am perplex, how do you know that? It is not that basic. But thanks. I am kinda stupid you know :)

avatar image Megahanuta · Dec 05, 2014 at 03:59 PM 0
Share

Only the first instruction after an if, for, while etc. will be executed when not enclosed in curly bracktes { } ;)

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

27 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

Related Questions

How to develop a script that substitute a gameobject position with my character? 0 Answers

Help with raycast from a ragdoll 0 Answers

Raycast2D Only returns true 1 Answer

Gravity makes player slide on any slope!!! Check if rigidbody Isgrounded not working 0 Answers

ground check isnt working...HELP! 2 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