Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Konomira · Oct 11, 2015 at 10:18 PM · c#2draycastai

How to detect if floor exists using Raycast

I'm trying to make an NPC that will switch directions at the end of a platform, so I decided to try and detect the platform using Raycast. However, when the ray reaches an area where there is no floor, it jolts back and forth erratically. I'm not sure what causes this.

 void Movement()
     {
         float speed = idleSpeed * Time.deltaTime;
 
         Vector2 v = GetComponent<Rigidbody2D>().velocity;
         v.x = speed;
         
 
         RaycastHit2D[] floorDetectRight;
         RaycastHit2D[] floorDetectLeft;
 
         Vector2 leftRay = new Vector2(GetComponent<Transform>().position.x - enemy.bounds.size.x, GetComponent<Transform>().position.y);
         floorDetectLeft = Physics2D.RaycastAll(leftRay, -Vector2.up, enemy.bounds.size.y);
 
         Vector2 rightRay = new Vector2(GetComponent<Transform>().position.x + enemy.bounds.size.x, GetComponent<Transform>().position.y);
         floorDetectRight = Physics2D.RaycastAll(rightRay, -Vector2.up, enemy.bounds.size.y);
 
 
         if (GetComponent<Rigidbody2D>().velocity.x > 0 && floorDetectRight.Length < 1)
         {
             v.x = -speed;
         }
 
         if (GetComponent<Rigidbody2D>().velocity.x < 0 && floorDetectLeft.Length < 1)
         {
             v.x = speed;
         }
         GetComponent<Rigidbody2D>().velocity = v;
     }

Another problem with this code is that it won't trigger if something other than the floor is there. It will only trigger if nothing is there. I did try if (GetComponent<Rigidbody2D>().velocity.x < 0 && floorDetectLeft.collider.tag != "floor") but when I had it like that, it didn't trigger at all...

If you can see what's wrong with the code, please let me know. As far as I'm aware, this should work. Thanks in advance!

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
Best Answer

Answer by Konomira · Oct 12, 2015 at 11:57 PM

Managed to fix it by adding a detect floor function and shuffling some things around.

     void Movement()
     {
         
         Vector2 v = GetComponent<Rigidbody2D>().velocity;
 
         if (DetectFloor())
         {
             goRight = !goRight;
             StartCoroutine(WaitSecond());
         }
         
         if(goRight)
         {
             v.x = idleSpeed * Time.deltaTime;
             GetComponent<Rigidbody2D>().velocity = v;   
         }
         else 
         {
             v.x = -idleSpeed * Time.deltaTime;
             GetComponent<Rigidbody2D>().velocity = v;
         }
 
 
 
     }
 
     bool DetectFloor()
     {
         RaycastHit2D[] floorDetectRight = new RaycastHit2D[1];
         RaycastHit2D[] floorDetectLeft = new RaycastHit2D[1];
 
         Vector2 leftRay = new Vector2(GetComponent<Transform>().position.x - enemy.bounds.size.x, GetComponent<Transform>().position.y);
         Physics2D.RaycastNonAlloc(leftRay, -Vector2.up, floorDetectLeft, enemy.bounds.size.y);
 
         Vector2 rightRay = new Vector2(GetComponent<Transform>().position.x + enemy.bounds.size.x, GetComponent<Transform>().position.y);
         Physics2D.RaycastNonAlloc(rightRay, -Vector2.up, floorDetectRight, enemy.bounds.size.y);

         if (floorDetectLeft[0].collider == null)
         {
             return true;
         }
 
         if (floorDetectRight[0].collider == null)
         {
             return true;
         }
 
         return false;
     }
 
     IEnumerator WaitSecond()
     {
         yield return new WaitForSeconds(1);
     }
 }
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

41 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

Related Questions

On collision x or y 2d c# 1 Answer

Two Raycast2D on object cancel each other out 0 Answers

Pseudo 3D like in Wolfenstein 3D 1 Answer

Physics2D.Raycast problems with vectors 1 Answer

Problems disabling an AI and giving control to the Player 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