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 vitelksnys · Apr 12, 2021 at 09:00 PM · raycastai

Implementing BUG2 algorithm

Hello! I'm trying to implement robotic BUG algorithm. My starting position is point A and my finish is point B. Robot should always follow straight line to the point B and if any obstacles are encountered robot must turn left. I already have reposition algorithm which I need for robot to get back into straight line. I have everything I need but I don't know how to make a robot go smoothly around obstacles. Raycasting does not seems to work as I expected because at the corner of the obstacle it starts to move half through the obstacle, like this: alt text This is how robot should move according to algorithm: alt text This is my code:

 public Vector3 pradzia;
     public Vector3 tikslas;
     public Vector3 startCoords;
     public float linearFunc;
     public float divisor;
     public float dist;
     RaycastHit2D E;
     RaycastHit2D N;
     RaycastHit2D W;
     RaycastHit2D S;
 
     // Start is called before the first frame update
     void Start()
     {
         startCoords = GameObject.Find("Robotukas").transform.position;
         tikslas = GameObject.Find("Tikslas").transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         // tiese p1(-24;0), p2(24;-5)
         //dabartinis -2; 2
         //dist > 0.004
 
         pradzia = GameObject.Find("Robotukas").transform.position;
 
         var dir1 = Quaternion.Euler(0, 45, 0) * transform.forward;
         var dir2 = Quaternion.Euler(0, 135, 0) * transform.forward;
         var dir3 = Quaternion.Euler(0, 225, 0) * transform.forward;
         var dir4 = Quaternion.Euler(0, 315, 0) * transform.forward;
         E = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.right), 1f);
         N = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.up), 1f);
         W = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.left), 1f);
         S = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.down), 1f);
         RaycastHit2D NE = Physics2D.Raycast(transform.position, dir1, 1f);
         RaycastHit2D SE = Physics2D.Raycast(transform.position, dir2, 1f);
         RaycastHit2D SW = Physics2D.Raycast(transform.position, dir3, 1f);
         RaycastHit2D NW = Physics2D.Raycast(transform.position, dir4, 1f);
 
 
         linearFunc = Math.Abs((startCoords.y - tikslas.y) * pradzia.x + (startCoords.x - -pradzia.x) * pradzia.y + ((startCoords.x * tikslas.y) - (tikslas.x * startCoords.y)));
         divisor = (float)Math.Sqrt(Math.Pow((startCoords.y - tikslas.y), 2) + Math.Pow((tikslas.x - startCoords.x), 2));
         dist = linearFunc / divisor;
 
         if (E && !N)
         {
             transform.Translate(Vector3.up * Time.deltaTime * 3f);
         }
         else if (S && !E)
         {
             transform.Translate(Vector3.right * Time.deltaTime * 3f);
         }
         else if (N)
         {
             if (!W)
                 transform.Translate(Vector3.left * Time.deltaTime * 3f);
         }
         else if (W)
         {
             if (!S)
                 transform.Translate(Vector3.down * Time.deltaTime * 3f);
         }
         else
             transform.position = Vector3.MoveTowards(pradzia, tikslas, Time.deltaTime * 3f);

screenshot-2021-04-12-101113.jpg (2.7 kB)
untitled.jpg (66.0 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

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

AI Field of vision 1 Answer

Another Raycast Issue. 0 Answers

Enemy raycast detection 0 Answers

2D Field of View 2 Answers

Keeping an raycasting AI within a patrol area 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