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
1
Question by wsimm101 · Jul 28, 2018 at 09:31 PM · pathfinding

2D alternative to Physics.CheckSphere

Hi,

I'm trying to convert the following method from a pathfinder script to something that would work in a 2D game with 2D objects.

 void CreateGrid() {
     grid = new Node[gridSizeX,gridSizeY];

             Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x/2 - 
             Vector3.forward * gridWorldSize.y/2;

     for (int x = 0; x < gridSizeX; x ++) {
         for (int y = 0; y < gridSizeY; y ++) {
             Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + 
                           nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);

                 **bool walkable = !(Physics.CheckSphere(worldPoint,nodeRadius,unwalkableMask));**

                  grid[x,y] = new Node(walkable,worldPoint, x,y);
         }
     }
 }

I have tried to use;

 void CreateGrid()
 {
     grid = new Node[gridSizeX, gridSizeY];
     Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - new Vector3 (0,1,0) * gridWorldSize.y / 2;

     for (int x = 0; x < gridSizeX; x++)
     {
         for (int y = 0; y < gridSizeY; y++)
         {
             Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + 
             new Vector3 (0,1,0) * (y * nodeDiameter + nodeRadius);

             **BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
             bool walkable = !(Physics2D.IsTouchingLayers(bc, unwalkableMask));**

             grid[x, y] = new Node(walkable, worldPoint, x, y);
             print(walkable);
             
         }
     }
 }

currently bool walkable is returning true ( or false if i remove !) so the bc boxcollider is not touching unwalkable layer.

I'm well out my depth here. Trying to convert a pathfinder script to 2D. I can give more info but I need some help if poss. Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 28, 2018 at 10:30 PM

The 2d equivalent of CheckSphere / OverlapSphere would be OverlapCircle. The 2d physics system doesn't have a CheckCircle method. However you just have to check the return value of OverlapCircle against null

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
avatar image
1

Answer by wsimm101 · Jul 28, 2018 at 11:46 PM

Thanks so much. I tried;

 void CreateGrid()
 {
     grid = new Node[gridSizeX, gridSizeY];
     Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - new Vector3 (0,1,0) * gridWorldSize.y / 2;

     for (int x = 0; x < gridSizeX; x++)
     {
         for (int y = 0; y < gridSizeY; y++)
         {
             Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + new Vector3 (0,1,0) * (y * nodeDiameter + nodeRadius);
             //BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
             box = new Vector2(nodeDiameter - 0.1f, nodeDiameter - 0.1f);
             bool walkable = !(Physics2D.OverlapBox(worldPoint, box, 90, unwalkableMask));
             //bc.transform.position = worldPoint;
             grid[x, y] = new Node(walkable, worldPoint, x, y);
             print(box);
             
         }
     }
 }

and it worked nicely. just gotta get my player to follow the path now! The full script is here if any one is interested.

https://github.com/SebLague/Pathfinding/tree/master/Episode%2003%20-%20astar/Assets/Scripts

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 iamgabrielma · Jul 29, 2018 at 09:03 AM 1
Share

I don't seem to be able to make this work for a 2d top down game. As you, I've also changed the worldBottomLeft vectors to be (0,1,0) as well as used OverlapBox ins$$anonymous$$d of OverlapCircle, however the nodes keep showing up as walkable (the grey zone, ins$$anonymous$$d of showing up red):

Screenshot: http://cld.wthms.co/6qArZ0

$$anonymous$$y terrain is based in 2 types of tiles: - Floor, layer: default, does not have box collider 2d - Walls, layer: unWalkable, has box collider

Any idea?

Edit: Fixed - The problem was that I was calling the CreateGrid() method on Start(), but in my game the levels are procedurally generated and are not finished until later in execution. $$anonymous$$oving the method call to later on made it work just fine.

avatar image wsimm101 · Jul 29, 2018 at 02:11 PM 0
Share

There is also the method;

      public Node NodeFromWorldPoint(Vector3 worldPosition)

in which the following needs to be changed from;

     float percentY = (worldPosition.z + gridWorldSize.y / 2) / gridWorldSize.y;

to;

     float percentY = (worldPosition.y + gridWorldSize.y / 2) / gridWorldSize.y;

there might be one other bit that needs changing to, can't remember.

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

92 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

Related Questions

A* pathfinder - next target 1 Answer

Modification of AIFollow.cs Pathfinding 0 Answers

How to edit navmesh at a runtime? 1 Answer

Pathfinding Returned Path dips AI into the ground 0 Answers

What is the best AI system for pathfinding on a moving platform 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