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 /
avatar image
0
Question by rubenalex367 · May 24, 2018 at 01:44 PM · c#unity 5raycastobstacle avoidance

Obstacle Avoidance in Unity, using raycasts. C#

I am using some rudimentary obstacle avoidance algorithm to avoid obstacles, however i am having issues with the right and left sensors. When both are active. they will cancel their rotations and not rotate at all. Also having a bit of trouble figuring out hit.normals via the front sensor.

  ray = new Ray(transform.position + Vector3.up, transform.forward);
  Vector3 posInicial = transform.position;
  float speed = 10f;
      if (Physics.Raycast(ray, out hit, 55f)) // Front sensor
      {
          if (hit.collider.tag == ("Pick Up")) // If robot detects pick up, it goes towards it
          {
              Debug.DrawLine(ray.origin, hit.point, Color.red);
              transform.position = Vector3.MoveTowards(transform.position, hit.point, Time.deltaTime * speed);
          }
          else
          {
              transform.Rotate(0, -80 * Time.deltaTime, 0); // Rotate if front sensor doesn't detect pick up
              Debug.DrawLine(ray.origin, hit.point, Color.blue);
          }
      }
      else
      {
          transform.position += transform.forward * speed * Time.deltaTime; // Go forward
          Debug.DrawLine(ray.origin, hit.point, Color.white);
      }
      if (Physics.Raycast(posInicial, Quaternion.AngleAxis(45f, transform.up) * transform.forward, out hit2, 20f))
      {
          transform.Rotate(0, -80 * Time.deltaTime, 0); // Rotate left if right detected
          Debug.DrawLine(posInicial, hit2.point, Color.yellow);
      }
      if (Physics.Raycast(posInicial, Quaternion.AngleAxis(-45f, transform.up) * transform.forward, out hit3, 20f))
      {
          transform.Rotate(0, 80 * Time.deltaTime, 0); // rotate right if detected left
          Debug.DrawLine(posInicial, hit3.point, Color.cyan);
      }
  }

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 Harinezumi · May 24, 2018 at 02:46 PM

Not rotating when there is an obstacle both left and right seems correct to me, otherwise you would turn towards one of them... :)

I think the solution is to restructure the logic and put the thinking on a higher level. That is, first gather all the information, then decide what to do. For example, you could first do all 3 raycasts, then decide using the results of all what to do.
Using pseudo-code:

 raycast front, left, and right
 if (pickup in front) { move forward }
 else if (something in front) {
     if (obstacle to left) { turn right }
     else if (obstacle to right) { turn left }
     else { /* it's a wall - turn around? */ }
 }
 else { // nothing in front - check for side obstacles
     if (obstacle to left AND obstacle to right) { /* it's a funnel - turn around? */ }
     else if (obstacle to left) { turn right }
     else { turn left }
 }

Note that because we first always check obstacle to left, the algorithm will tend to steer right. You could randomise which one is checked first to keep it more balanced.
Also, the cases for when there is an obstacle in to left and/or right don't seem to depend on if there is an obstacle in front, so they could probably be merged (refactored).

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 rubenalex367 · May 24, 2018 at 03:15 PM 0
Share

Thanks for the help, asked around in stackoverflow and such and they couldn't address the issue without mentioning the navmesh.

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

200 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 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 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 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 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

Raycast from 2D canvas object to world space 1 Answer

Distribute terrain in zones 3 Answers

Spawned tree not falling 1 Answer

Raycasting not working? 1 Answer

c# Raycast going off at odd angles. Unity 5 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