Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 evgen4ik007 · Sep 23, 2021 at 04:57 AM · colliderdetectioncolission

Are there any ways to check collision?

I create an object and wanna check collision this new object with others. cause i don't want to place trees too close to each other. So there is a code from one script.

 private IEnumerator GenerateTrees()
     {
         //yield return new WaitForSecondsRealtime(2f);
         for (int i = 0; i < amountTrees; i++)
         {
             var border = sizeX - delta;
             var pos = new Vector3(Random.Range(-border, border), 0, Random.Range(-border, border));
             var tree = GenerateObject(treesPrefabs);
             SetRandomPosition(tree, border);
             yield return new WaitForEndOfFrame();
             //yield return new WaitForSecondsRealtime(2f);
             while (tree.GetComponent<CollisionDetector>().hasCollision)
             {
                 print("while");
                 SetRandomPosition(tree, border);
                 yield return new WaitForEndOfFrame();
                 //yield return new WaitForSecondsRealtime(2f);
             }
         }
     }

And there is from other which is attached for every prefab.

 public class CollisionDetector : MonoBehaviour
 {
     public bool hasCollision;
 
     private void OnTriggerEnter(Collider collision)
     {
         if (collision.gameObject.layer == 6)
         {
             hasCollision = true;
         }
     }
 
     private void OnTriggerExit(Collider collision)
     {
         if (collision.gameObject.layer == 6)
         {
             hasCollision = false;
         }
     }
 }

It seems that code in the second can't update in time. That's why the first script doesn't generate new position for tree. But when coroutine is done, i can see in inspector that hasCollision is true. Maybe there is a way to wait for end of the seconds cript update?

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
0

Answer by HammockHead21 · Sep 23, 2021 at 06:21 AM

Few ways/alternatives I can think of

  1. Use PhysicsScene / PhysicsScene2D

  2. Place trees at grid coordinates where the width/height of a cell are the minimum distance radius

void PlaceTrees(int maxCount, int maxIterations) { bool[,] used = new bool[width, height]; int count = 0; for (int i = 0; i < maxIterations && count < maxCount; ++i) { int x = RandomValue(); int y = RandomValue(); // place tree if (!used[x,y]) { used[x,y] = true; ++count; } } }

  1. Bite the bullet and implement some collision checking yourself. If you aren't placing that many trees you could just nest two for loops bool CanPlaceTree(Vector3 candidatePosition, List trees) { for (int i = 0; i < trees.Length; ++i) { Vector3 delta = candidatePosition - trees[i]; if (delta.sqrMag < minSqrDistance) { return false; } } return true; } You could look up an implementation of a QuadTree to help

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
0

Answer by AlgoUnity · Sep 23, 2021 at 08:15 AM

You want to yield return WaitForFixedUpdate() to wait for the collision checking to happen. If your problem is that you don't want the tree to appear and then teleport once the collision checking happens, you could maybe just make it invisible by disabling the renderer by default and activate it when a valid position has been verified.

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

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

170 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

Related Questions

Detect OnMouseUp on gameobject with different colliders 1 Answer

how to check if one object is colliding with another 1 Answer

Slant Physics Raycast Implementation 0 Answers

What is the most efficient way of detecting an object? Using raycast, rect (screen space) or utilizing a capsule collider? 1 Answer

Have part of mesh change color 0 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