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 Ricewind1 · Jun 12, 2014 at 01:55 AM · collisioncolliderrandomgenerationruntime-generation

Collision Detection is too slow

I'm currently working on code to auto-generate levels/maps/prefabs/whatever. The auto-generation part works, and it works perfectly. The only problem I came across recently was when checking if the next space is empty or not. To do this I create a cube with the same size as the bounds of the original object. Add a rigidbody, freeze the position and add the script. The code below is the code I use for the cube (I call it testObject).

     public GameObject previousPrefab;
     public bool Collided;
 
     void OnCollisionEnter(Collision col)
     {
         if (col.transform.root.gameObject != previousPrefab && col.transform.parent.name != "LevelKey")
         {
             Collided = true;
             Debug.Log("Falsetestobject");
         }
     }

The following code is the code I use for placing this object and checking what value I get back from the "Collided" field.

     public bool availableSpace(CustomGameObject go)
     {
         //Instantiates a new testobject
         GameObject newprefab = go.getBoundsClone();
         //Sets the previousprefab field to match the last prefab used in level generation
         newprefab.GetComponent<testObject>().previousPrefab = lastUsedPrefab;
         //adjusts the position and rotation of the testobject
         newprefab.transform.eulerAngles = rotateGameObject(lastUsedPrefab, newprefab);
         newprefab.transform.position = determinePosition(lastUsedPrefab, newprefab);
         newprefab.transform.position -= childToParentDistance(newprefab, findBeginKey(newprefab));
 
         Debug.Log(newprefab.GetComponent<testObject>().Collided.ToString());
         if (newprefab.GetComponent<testObject>().Collided)
         {
             Debug.Log("false");
             return false;
         }
         return true;
     }
 }

All this code does work perfectly except for one thing. The testObject should notify me when it's being placed over any object that isn't either it's own levelKey object or the previous placed object and return false in the availableSpace method. The problem is is that I first get the debug message "False" from the availableSpace method (at the bottom) and after that "Falsetestobject" from the testObject class a couple of times (once for every collision). I know this is because collision only happens after 1 frame has passed, so basically the placement is faster than the collision detection.

Is there ANY workaround or solution for this? Waiting after the placement of every testObject isn't really an option.

Comment
Add comment · Show 14
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 Ricewind1 · Jun 16, 2014 at 05:50 PM 0
Share

Still haven't found a way around this myself. Hoping someone who knows will see it this time around!

avatar image meat5000 ♦ · Jun 16, 2014 at 05:54 PM 0
Share

Quick question... is there anywhere that Collided becomes False again?

avatar image giantkilleroverunity3d · Jun 16, 2014 at 06:04 PM 0
Share

I too have this 'Escape' problem. Even tho I slow down rotations the object skips through the boundarary. I believe it could be a high torque value even though nothing is moving fast. I check this out tonight.

avatar image Ricewind1 · Jun 16, 2014 at 06:10 PM 0
Share

@meat5000, no. It doesn't become false again. If collision it true it means that the prefab was instantiated in another prefab. This means that there's no place and the game-object will be destroyed and replaced. Furthermore, the collision function is located on a temporary prefab that will always be destroyed even if it returns false. It's just a check.

@giantkiller I'm not moving or rotating anything in between frames. The problem I have is that before 1 frame has passed (to detect collision) there have already been a numerous number of new prefabs placed. In other words the code you see here finishes well before the first collision frame ever has a chance of taking place.

avatar image MikeNewall · Jun 16, 2014 at 06:16 PM 0
Share

If you have a box which represents the bounds of the existing level you could check if the bounds of the new object intersect with it. That way you can be sure you check for intersection during the same frame that it's instantiated.

     bool CheckIfIntersects(Bounds a, Bounds b){
         if(a.Intersects (b)) return true;
         return false;
     }
Show more comments

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

24 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

Related Questions

Make sure objects are not instantiated on each other 2 Answers

FPS Character keeps randomly sliding and rotating 2 Answers

Prevent randomGenerated Structures from colliding / clipping 1 Answer

Object Collision PLEASE HELP!!! 1 Answer

Very simple collision death 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