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
1
Question by SrBilyon · Sep 16, 2012 at 07:57 AM · raycastaiobstaclesteering

Problems with raycast obstacle avoidance

I'm working on a raycast driven obstacle avoidance that will steer characters away from obstructions, but I'm having problems with making the character completely steer away from obstructions.

alt text

The character steers fine when slightly parallel to walls, but makes no attempt to navigate away from obstructions that are in front of it.

P.S: I am avoiding UnitySteer since I'm not really looking into having to use an outside framework.

Here is the code I am using:

     void ObstacleAvoidance(Vector3 dir, Vector3 steering, bool checkObstacles)
     {
         List<Vector3> steeringRays = new List<Vector3>();
         var _holdTheJump = dir.y;
         
         bool left = false;
         bool right = false;
         bool front = false;
         Vector3 adjDirection = dir;
         
         steeringRays.Add(trans.TransformDirection(-steering.x, steering.y, steering.z)); //ray pointed slightly left 
         steeringRays.Add(trans.TransformDirection(steering.x, steering.y, steering.z)); //ray pointed slightly right 
         steeringRays.Add(trans.forward); //ray 1 is pointed straight ahead
         
         RaycastHit hit;
         
         if (checkObstacles )
         {
                 Debug.DrawRay(trans.localPosition, steeringRays[0].normalized * rayLength, Color.cyan);
                 Debug.DrawRay(trans.localPosition, steeringRays[1].normalized * rayLength, Color.cyan);
                 Debug.DrawRay(trans.localPosition, steeringRays[2].normalized * rayLength, Color.cyan);
 
 
                 if (Physics.Raycast(trans.position, steeringRays[0], out hit, rayLength))
                 {
                     if (hit.collider.gameObject.layer != 13 && (!front && !left))
                     {
                         isSteering = true;
                         front=false; right=false; left=true;
                         Debug.DrawLine(trans.position, hit.point, Color.red);
                         trans.forward = new Vector3(dir.x,0,dir.z) + (hit.normal).normalized * Time.smoothDeltaTime;
                         Debug.Log("Steer Left");
                     }
                 }
                 else                
                 if (Physics.Raycast(trans.position, steeringRays[1], out hit, rayLength))
                 {
                     if (hit.collider.gameObject.layer != 13 && (!front && !left)) //Character layer
                     {
                         Debug.DrawLine(trans.position, hit.point, Color.red);
                         front=false; right=true; left=false;
                         isSteering = true;
                         trans.forward = new Vector3(dir.x,0,dir.z) + (hit.normal).normalized * Time.smoothDeltaTime;
                         Debug.Log("Steer Right");
                     }
                 }
                 else 
                 {
                     isSteering = false;
                     left = false; right = false; front = false;
                 }
         }
         //if (isSteering)
         //trans.forward = new Vector3(adjDirection.x, 0, adjDirection.z)  * Time.smoothDeltaTime;
         //Quaternion rot = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
         //trans.rotation = Quaternion.Lerp(trans.rotation, rot, 15f * Time.smoothDeltaTime);
     }


steeringproblem[old]!!!!!!!.png (35.7 kB)
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 Fattie · Sep 16, 2012 at 08:21 AM 1
Share

thank you for posting the only good question for months BTW :-)

avatar image SrBilyon · Sep 16, 2012 at 08:30 AM 0
Share

Thanks for taking the time out to help. I do understand what you're talking about a bit though now.

1 Reply

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

Answer by Fattie · Sep 16, 2012 at 08:14 AM

I'm struggling to follow your code, but it would appear at this line:

for (int i = 0; i < 3; i++)

you are actually doing "all three things".

So in the case of your example image on the right, it will indeed do "all three things". it will try to make it go left, then right, then center. So it all cancels out. You see?

A very simple solution in a "helloSteering" program like this,

always choose one direction (say, left). interestingly those little toys that run around on the floor with a bumper - have the same algorithm!

So what you need to add to your code is this concept:

"Am I FACING a wall?" How to know if you are facing a wall -- well, ALL THREE raycasts will fire.

So if that happens, turn left a little. This will iterate you out of the problem.


If you enjoy that solution, I will tell you the next conceptual step. Look at the right digram. Look at the three orange bars. Look at the LEFT bar length LBL and the RIGHT bar length RBL. In fact, if RBL is bigger than LBL you are looking along a wall and you are looking "to the right". (Same for vice versa of course.) Furthermore you need to look at the ratio between LBL RBL to get a sense of "how much" you are looking away from the wall.If they are very similar, you are "facing a damn wall" and trapped (in that case, choose to turn left, as described above).

I am sure you know steering is incredibly more complicated than this, but, a few awesome heuristics like this can provide amazing power, amazing.

Comment
Add comment · Show 4 · 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 SrBilyon · Sep 16, 2012 at 08:34 AM 0
Share

Thank you! I had a feeling that having the feelers the same length and making detection via a loop would be a disaster. SO basically, I need to pretty much choose one feeler that will take over if both rays hit at the same time [Such as the left ray]?

avatar image Fattie · Sep 16, 2012 at 08:46 AM 0
Share

that is utterly 100% correct !!

it would come down to "if all three hit" .. then choose to turn (say) left

unfortunately in the future you will have to also consider the lengths, perhaps - but the heuristic you state will be awesome, enjoy !

avatar image SrBilyon · Sep 16, 2012 at 10:42 PM 0
Share

This ended up helping alot. I also have my character's raycast checking which layers are being detected as well. They can also jump over certain obstacles now as well.

avatar image Fattie · Sep 16, 2012 at 10:43 PM 0
Share

magnificent! have a great day :) hopefully you have more questions in the future! cheers

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

12 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

Related Questions

Determining rotation for collision avoidance? 3 Answers

Raycast Steering Problem 1 Answer

UnitySteer obstacle avoidance on non Spherical Objects 1 Answer

Keeping an raycasting AI within a patrol area 1 Answer

Help me with AI 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