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 /
This question was closed Jun 23, 2015 at 02:35 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JuanseCoello · Apr 12, 2015 at 01:32 PM · gameobjectrigidbodylayercast

Help with layermask and raycast!!! HELP

I am trying to set a mask, the numeber 8 one, and I have put an object there, the floor, so when the raycast collides with it detects the floor. But I dont know what happens, it doesnt recognize the floor object. The ray works perfectly by the way, that is not the problem.

Here is the code:

    GameObject Piso = GameObject.Find("Floor"); // this is the gameobject floor
    int layerMask = 1 << 8; // this variables go at the begining

     void IsGrounded() // this is my function that casts the raycast 
 {

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


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

      }
         

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

         isgrounded = false;
     } 
  } 

Am I missing something? By the way, I placed the object Floor in the scene in the layer number 8, but somehow it doesnt recognize the raycast. HEEELP!!!

By the way, the raycast is casted from a rigidbody called rb inside the player.

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Apr 12, 2015 at 03:40 PM

Use

 LayerMask layerMask = 1 << 8;

instead of

 int layerMask = 1 << 8;

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 · Apr 13, 2015 at 01:21 AM 0
Share

oh thanks!

avatar image meat5000 ♦ · Apr 14, 2015 at 07:00 AM 0
Share

No problem. Click the tick to accept the answer :)

avatar image
0

Answer by hbalint1 · Apr 12, 2015 at 02:58 PM

Is it possible, that you used the braces wrong? I think you are missing one '{' in the 12th row:

if (hitInfo.distance > 0.1f) {

if the distance is < 0.5f the grounded will always be false, because it looks like this:

 void IsGrounded()
  {
      ...
 
      if (Physics.Raycast..)
      {
          if (hitInfo..) // I think here you are missing a '{', because the other if is out of the Raycast's if
      }
          
      if (hitInfo.distance < 0.5f){  // this will always be executed, so as I said above: if the distance less, than 0.5f, the isGrounded will always be false
          ...
      } 
   } 

I hope it's understandable. So:

 void IsGrounded() // this is my function that casts the raycast 
  {
  
      RaycastHit hitInfo;
      if (Physics.Raycast(rb.transform.position + Vector3.down * 3f, Vector3.up * 5f, out hitInfo, Mathf.Infinity, layerMask))
      {
  
          if (hitInfo.distance > 0.1f)
          { // Change .1f to what you need
              Debug.DrawRay(rb.transform.position + Vector3.down * 3f, Vector3.up * 5f, Color.blue);
          
              isgrounded = true;
  
          }
  
          if (hitInfo.distance < 0.5f)
          {
              Debug.DrawRay(rb.transform.position + Vector3.down * 3f, Vector3.up * 5f, Color.red);
  
              isgrounded = false;
          } 
      }
 
   } 
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

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

"Some objects were not cleaned up when closing the scene" 1 Answer

Why does my rigidbody slows down at time ? 1 Answer

why is part of my script being ignored? 2 Answers

My rigidbody is floating away, its a child of a parent 0 Answers

Mesh renderer does not move with parent rigid body 5 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