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 samqweqwe · Aug 08, 2014 at 05:05 PM · collisionraycastrandom.rangerandom spawn

Detect if an object will collide if it moves to a position

Hi,

I am trying to create a randomly generated level where i am stuck on stopping objects from overlapping. I am trying to detect if an object is in the space that the new object is being positioned. I have had the idea to raycast the area with two rays, one to check the width and one to check if the previous ray has not started inside an object.

 float X = (int)Random.Range (0, 30);
         float Y = (int)Random.Range(-10f, 8f) + 0.5f;
         float Size = (int)Random.Range(1, 20);
         Ray ray = new Ray(new Vector3(X - Size/2 - 0.005f, Y, 0), Vector3.right);
         Ray rayFront = new Ray (new Vector3 (X - Size / 2, Y, -10), Vector3.forward);
         while (Physics.Raycast(ray, Size) || Physics.Raycast(rayFront, 15))
         {
             X = (int)Random.Range (0, 30);
             Y = (int)Random.Range(-10f, 8f) + 0.5f;
             Size = (int)Random.Range(1, 20);
             ray = new Ray(new Vector3(X - Size/2 - 0.005f, Y, 0), Vector3.right);
             rayFront = new Ray (new Vector3 (X - Size / 2, Y, -10), Vector3.forward);
         }
         
         Platform.transform.position = new Vector3(X, Y, 0);
         Platform.transform.localScale = new Vector3(Size, 1, 1);


but objects still seem to overlap. Does anyone know how objects could be detected in the same frame as the Instantiation of the new object?

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 Schneider21 · Aug 09, 2014 at 06:14 PM

What about creating an invisible object with a collider at the new location that matches the object you're trying to instantiate? That invisible object could have a script attached that detects if it's colliding with anything, and if so, prevents the other object from moving there.

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 KyleBlumreisinger · Nov 17, 2017 at 10:11 PM

I believe I have the answer for your 3-year-old question - Physics.BoxCast. Physics.BoxCast is a raycast in the shape of a box. Now I understand you don't want to cast along a ray, you just want to check one position, but we can still use Physics.BoxCast for this. Now, a box cast, and any raycast, WILL NOT DETECT any collider it starts inside of. I thought at first that doing a box cast in your desired location with a travel distance of 0.0f would work, but it doesn't detect anything it starts out colliding with, only things it moves into. So what you have to do is start your box cast right beside/above/adjacent to wherever you want it to detect collisions in, and then have the direction + distance set so that it will move into your desired position and then stop. This should work to detect anything in that space. For example, the code below detects if there is anything in a 1x1x1 cube centered around (0, 5, 0). It starts out in (0, 5, -1) and then moves 1.0f units in the forward (z) direction, to stop at (0, 5, 0).

 RaycastHit hitInfo;
 if (Physics.BoxCast(new Vector3(0, 5, -1), new Vector3(0.5f, 0.5f, 0.5f), Vector3.forward, out hitInfo, Quaternion.identity, 1.0f
 {
       print(hitInfo.collider.name);
 }
 else
 {
       print("Nothing hit");
 }

Now this still won't detect something if that thing was so big that the boxcast, even moved to the side, was STILL inside the object. So you'll have to tweak this method to work with your specific situation.

As for options to detect collisions when the ray does start inside the object, it seems to get more complicated, but here is a page where someone discusses possible ways to do this with a raycast, if you want to look into it yourself. https://forum.unity.com/threads/raycast-not-finding-objects-collider.323109/

Hope that helps the people that see this!

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 KyleBlumreisinger · Nov 18, 2017 at 07:20 AM 0
Share

Oh, I found another thing that might be even better! Physics.CheckBox is a function that does exactly what you want! No foolery, no trouble! I was surprised to come upon it. It checks a box area to see if there are any colliders there. Only problem is, it only gives a bool true/false output, that's it. No reference to the collider, no point of collision, no hitinfo... So, only useful if you want to know if it will hit something, but not what it will hit.

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

23 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

Related Questions

How to check if a raycasthit detects an angle less than 45 degrees and touches the ground 2 Answers

Subframe Collision Detection 0 Answers

making ray ignor certain collider 1 Answer

Obstacle avoidance 1 Answer

Destroy Turret with machine Gun 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