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 Danisuper · Jul 03, 2016 at 11:16 AM · c#2draycastingraycasthit2d

RaycastHit2D not detecting a specific gameobject in a for (for path smoothing in A*)

Hello all,I'm making a 2d top down game and I'm making an A* AI for the enemies based on a node system. I made a very simple function too smooth the path found by reducing the number of nodes to follow, given the node i if i "sees" i+2 delete i+1. that's the code:

 void SmoothPath()
     {
         for(int i=0;i<closed_list.Count;i++)
         {
             if (i + 2 < closed_list.Count)
             {
                 RaycastHit2D ray = Physics2D.Raycast(closed_list[i].transform.position, closed_list[i + 2].transform.position,
                     Mathf.Infinity);
 
                  if (ray.collider != null)
                 {
                     if (ray.collider.gameObject==closed_list[i+2])
                     {
                         closed_list.Remove(closed_list[i + 1]);
                         Debug.Log(ray.collider.gameObject.name);
                     }
                 }
             }
             closed_list[i].GetComponent<SpriteRenderer>().color = d;
         }
     }

The problem is that the if is never true even if i+2 has surely been hit because in this list of nodes (closed_list) every node, as you can see in the cycle, casts a ray towards the node after its adjacent node. But it doesn't execute the if. Instead if I use ray.collider.gameObject.tag=="node" the if is executed but it doesn't do what it should do. I also tried with GetInstanceID() but nothing. I'm pretty sure it's a stupid logic error I'm making. What's wrong? Thanks in advance :)

Comment
Add comment · Show 1
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 Danisuper · Jul 06, 2016 at 07:48 PM 0
Share

So I'm back. I found a 2 ways but I couldn't get them work. The first is a pseudocode I found here (go in "Smoothing the A* Path" section and you'll find it), I don't know how to "translate" in C# the while part and also the ->next but I think it means i+1 in a for... The second way is something that came to my $$anonymous$$d some time ago, it works like this.

 -The first node of the closed list cast a ray towards all the other nodes 
 
 -if the ray collides with something that is not a node add the last node to another list (wich will be the list of our final path)
 
 -else repeat 

I made an implementation of this:

         GameObject check_node = closed_list.First();
         for (int i = 0; i < closed_list.Count; i++)
         {
             RaycastHit2D[] ray = Physics2D.RaycastAll(check_node.transform.position, closed_list[i].transform.position, $$anonymous$$athf.Infinity);
 
             for (int j = 0; j < ray.Length; j++)
             {
                 if (ray[j].collider != null && ray[j].collider.gameObject.tag != "node")
                 {
                     if (j - 1 > 0)
                     {
                         if (!smooth_nodes.Contains(ray[j - 1].collider.gameObject))
                         {
                             if (ray[j - 1].collider.gameObject.tag == "node")
                             {
                                 smooth_nodes.Add(ray[j - 1].collider.gameObject);
                                 check_node = ray[j - 1].collider.gameObject;
                             }
                         }
                     }
                     else
                     {
                         if (!smooth_nodes.Contains(ray[j].collider.gameObject))
                         {
                             if (ray[j].collider.gameObject.tag == "node")
                             {
                                 smooth_nodes.Add(ray[j].collider.gameObject);
                                 check_node = ray[j].collider.gameObject;
                             }
                         }
                     }
                 }
             }
         }

but it doesn't work. What should I do?

Thank you very much guys for your help and time

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dutchysaurus · Jul 03, 2016 at 06:25 PM

Hey there. I typed up a response to this and then accidentally refreshed the page. Fun :P

First things first, you for loop can be combined with your first if statement to make things a bit clearer:

 for (int i = 0; i + 2 < closed_list.Count; i++)

Anywho. I believe your problem stems from the fact that your raycast begins inside of your node and thus is hitting this whenever it is drawn. This is why your if (ray.collider != null) statement is returning true, while if (ray.collider.gameObject==closed_list[i+2]) is returning false. This is because you are always hitting closed_list[i]. To fix this, you simply need to incorporate layer masks, and assigning them during the loop so that the raycast can ignore the originating object and hit everything else. To do this, you have to setup a new layer, for example, "Originating Node". If this is your only layer, make it layer number 8, otherwise, the next available layer is fine. Next comes the code

     void SmoothPath()
     {
         //Simplified for loop
         for (int i = 0; i + 2 < closed_list.Count; i++)
         {
             //Adjusting the node we start from to be ignored by the raycast.
             closed_list[i].layer = 8;
             //For completions sake, you could also add "closed_list[i+1].layer = 8" to this 
             //line, which would make sure that if the (i+1) node is inbetween the raycast, 
             //it will be ignored
 
             //The only Adjustment here is adding the layermask '~(1 << 8)' Link for layermasks is 
             //down below. This will allow our raycast to pass through only objects that are on
             //this layer, and hit everything else.
             RaycastHit2D ray = Physics2D.Raycast(closed_list[i].transform.position, closed_list[i + 2].transform.position,Mathf.Infinity, ~(1 << 8));
 
             if (ray.collider != null)
             {
                 if (ray.collider.gameObject == closed_list[i + 2])
                 {
                     //We no longer need to ignore the originating node (i) so we can reset 
                     //it's layer to the default so that we don't mess up further iterations
                     closed_list[i].layer = 1;
                     //"closed_list[i+1].layer = 1;" reset the nodes layer
                     closed_list.Remove(closed_list[i + 1]);
                     Debug.Log(ray.collider.gameObject.name);
                 }
             }
             //At the end of each loop, resetting to the default layer
             closed_list[i].layer = 1;
             //"closed_list[i+1].layer = 1;" If the (i+1) node wasn't removed, reset it's layer
         }
         closed_list[i].GetComponent<SpriteRenderer>().color = d;
     }

You can read about what i've done with the layers here: https://docs.unity3d.com/Manual/Layers.html

I hope that solves your problem, any follow ups, feel free to send them through.

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 Danisuper · Jul 03, 2016 at 10:41 PM 0
Share

Hi, yes me too, I pressed accidentaly backspace while writing the question and it deleted everything. Unity should really make an autosave system. In my code I took in account also the layers, like you wrote but it didn't work. I don't think the problem is with the layers or the raycast that collides with the collider of the gameobject that casted it because in Project Settings > Physics2D I disabled "queries start in colliders" and I also tried to make the default layer (the layer that contains everything) and the node layer (the layer to use in the for,it's the number 8) ignore the collision between themselves but it didn't work. Anyway thank you very much for your help :D.

avatar image Dutchysaurus Danisuper · Jul 04, 2016 at 05:31 AM 0
Share

If it's not that, I would go through and chuck debugs all through the code to see everything working in action. Double checking all the nodes have colliders to hit. That's all I got, good luck.

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

179 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

Related Questions

Multiple Cars not working 1 Answer

Cast multiple rays from an object between two angles? 0 Answers

Distribute terrain in zones 3 Answers

How do I create a wide beam thats visible to the player and is stopped by barriers in my 2D stealth game? 0 Answers

2D Raycast not working 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