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 Lemonizer · Dec 05, 2013 at 09:34 PM · functionwrongelse

Faulty use of else within a function?

So I have this script that will send a raycast to check if the player is grounded, it seems to check when the player is grounded, but it seems like it doesn't print out when it's not grounded. I think I am using the else{ in a wrong way.

Here's the script:

 var isGrounded : boolean = false;
 
 
 function Update () {
 
     var down = transform.TransformDirection(Vector3.down);
 
     var chkGround : RaycastHit;
     
         Physics.Raycast(transform.position, down, chkGround, 0.5);
         
             if(chkGround.collider !=null){
             
                 if(chkGround.collider.gameObject.tag == "ground"){
                 
                         Debug.Log("GROUNDED!");
                 
                     isGrounded = true;
                     
                     }
                     
                         else{
                         
                                 Debug.Log("NOT GROUNDED!");
                         
                                 isGrounded = false;
                                 
                                     }                        
     
         }
 
 }


I am not sure what I am doing wrong. Any help is appreciated :)

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 EX_Darius · Dec 05, 2013 at 09:43 PM 0
Share

Does either of the objects have the "is trigger" option enabled in the inspector? I also suggest using the method OnTriggerEnter to handle the check.

2 Replies

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

Answer by clunk47 · Dec 05, 2013 at 10:05 PM

Put your Raycast in an if() statement, so you're only checking the tag of a collider if there is something beneath your object. If tag != ground, grounded is false. If tag == ground, grounded is true. If no raycast hit beneath object, grounded is false. Try this:

 var isGrounded : boolean = false;
 var down : Vector3;
 var chkGround : RaycastHit;
 
 function Update () 
 {
     down = -transform.up;
 
     if(Physics.Raycast(transform.position, down, chkGround, 1))
     {
         if(chkGround.collider.tag == "ground")
             isGrounded = true;
         
         else
             isGrounded = false;
     }
 
     else
         isGrounded = false;
     
     if(isGrounded)
         Debug.Log("Grounded!");
     else
         Debug.Log("Not Grounded!");
 }
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 Lemonizer · Dec 05, 2013 at 10:19 PM 1
Share

That was a much better method, thanks!

avatar image clunk47 · Dec 05, 2013 at 10:21 PM 0
Share

Happy I was able to help ;)

avatar image
1

Answer by violence · Dec 05, 2013 at 09:42 PM

if(chkGround.collider !=null)

Here youre checking that its colliding with something, which means no "Not Grounded" statement. Its not going into the next if statement if it isnt colliding with anything. You have to take the else statement out of the 2nd if statement. Simply add another bracket at line 20. That should do the trick.

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

20 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

Related Questions

what code should i be using if im only wanting a function to happen if a raycast does not return a hit 1 Answer

Need help with If/else statement. 1 Answer

else for an entire function 1 Answer

Possible to play single clips using Mecanim? 0 Answers

C# Function that returns instantiated object 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