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 /
This question was closed Sep 07, 2017 at 06:09 AM by AshleyGuihot for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AshleyGuihot · Sep 07, 2017 at 05:11 AM · collisionphysicscolliderphysics.overlapsphere

Having trouble using Physics.OverlapSphere

alt textI've made up a simple 3D grid to detect objects within its area, and I'm trying to use OverlapSphere to identify the objects inside. I know this must be something super simple, or perhaps I just don't understand the OverlapSphere method correctly. I hope someone can look at this and give me some insight into what I'm doing wrong.

This is the code I've made for the process:

 public bool walkable = true;
 public LayerMask unwalkableMask; // defined in editor
 public LayerMask playerMask; // defined in editor
 public bool housingPlayer = false;

 private Collider[] coll;
 private Vector3 nodeSize = new Vector3(0.7f, 0.7f, 0.7f);

 void Update() {
     PhysicsChecks();
 }

 private void PhysicsChecks() {
     walkable = !Physics.CheckSphere(transform.position, .1f, unwalkableMask);
     housingPlayer = Physics.CheckSphere(transform.position, .1f, playerMask);
     coll = Physics.OverlapSphere(transform.position, .1f);
 }

 void OnDrawGizmos() {
     Gizmos.color = (walkable) ? Color.white : Color.red; // Will always start white in this test
     if (housingPlayer)
         Gizmos.color = Color.blue;
     if (coll[0].tag == "Player")
         Gizmos.color = Color.green;

     Gizmos.DrawCube(transform.position, nodeSize);
 }

As you can see it's a very short and simple block of code. All it does right now is use the (15, 20, 10) grid to detects when the player is touching one it its nodes via the CheckSphere method. If that returns true, the node turns blue. After that, if a node detects the player and allows access to the gameObject via the OverlapSphere method the node turns green. Simple enough.

The issue is that only one third to one half of the cubes turn green (Are correctly detected by OverlapSphere) when they come into contact with the player. It's a different amount every time, but it's generally around a third. Cubes 1-7ish of each axis work correctly, before they stop reacting after that number. All cubes in the grid react correctly to the code for the blue (CheckSphere) despite being practically the same code.

Below is the code that constructs the grid if that's any help.

     grid = new GameObject[gridWidth, gridHeight, gridDepth];

     for (int x = 0; x < gridWidth; x++) {
         for (int y = 0; y < gridHeight; y++) {
             for (int z = 0; z < gridDepth; z++) {
                 grid[x, y, z] = GameObject.Instantiate(node, new Vector3 (x, y, z), new Quaternion(0,0,0,1), parent);
                 grid[x, y, z].GetComponent<NodeScript>().x = x;
                 grid[x, y, z].GetComponent<NodeScript>().y = y;
                 grid[x, y, z].GetComponent<NodeScript>().z = z;
             }
         }
     }
 }

In summary, please help me figure out why OverlapSphere isn't working correctly on all of my nodes.

ezgif-4-94df342338.gif (489.1 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

1 Reply

  • Sort: 
avatar image
0

Answer by hexagonius · Sep 07, 2017 at 05:45 AM

I would das that not using any mask with OverlapSphere gives you an arbitrarily ordered array with colliders. It's not guaranteed that the player is the first (col[0]). You should use a mask or check all colliders for the player.

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 AshleyGuihot · Sep 07, 2017 at 06:05 AM 0
Share

Thanks friend :)

Follow this Question

Answers Answers and Comments

143 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

Related Questions

Exported Scene in .swf loses 2D colliders ? 1 Answer

Getting "Internal error: Too many pairs created" without any apparent reason 0 Answers

Tossing an object with the correct force at any distance from another so that it collides with the other 1 Answer

Is it necessary to attach a rigidbody/colliders to child objects? 1 Answer

2D Collision not working! (child sprites colliders) 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