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 oliver-jones · May 15, 2014 at 08:47 AM · raycastcolliderloopgrid

Which Method is Optimal/Fastest - Raycast Vs Collider

Hello,

I have a grid of 100 x 100 empty gameObjects that I spawn on my scene, each empty has a collider attached to it, with a size of 1 x 1. So when the grid is laid out - they all fit snug against one another.

The grid is used to show where the user can, and can't deploy a building (in the future, the grid will have a white texture to show the user can deploy here, and a red to show this cell is unsuitable for deploying).

I'm currently using a box collider to on my building, that when collides with my grid colliders - sends a 'IsHighlighted' message. Which returns info about the cell - if its a good cell, or a bad one.

My question is -

I'm I better using a Box Collider, as I am doing - and using OnTriggerEnter to get all the cells under the building - which could be in the 100s count.

Or

As all my buildings are square, or rectangle - I could use a raycast on each of the corners of the building - that fires down to retrieve the cell. So my raycast will pull 4 cells in total (each corner of the building), and then use a for loop to find the cells within these four points, based on their Vector3.

Which way is better? In terms for performance and optimisation?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by DawdleDev · Sep 03, 2018 at 08:20 PM

For you guys visiting this page in the future, RayCasting is WAY more performant than unity's physics system, but these guys are right in saying that this user needs to look for a better, more performant way to do things. Physics really should be used as a last resort (unless they are used as actual physics, but even then it might be better to modify the original system than use what they have).

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
2

Answer by korbul · May 15, 2014 at 09:18 AM

Having hundreds of collides is not the way to go for solving a problem like that. I recommend you hold a matrix in memory of the size you need (ex: placement[100][100]). This matrix will hold 1 if that position can have building placed on and 0 otherwise (or vice versa).

Now when you want to place a building you get it's position and check that with the placement matrix. So lets say you want to place a building at x 45, y 56. If placement[45][56] == 1, place building else, don't place building

having a big matrix like that can be very daunting to fill manually, so I recommend you make or use some kind of editor.

Using the technique above should yield greater performance that your original idea.

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 oliver-jones · May 15, 2014 at 09:28 AM 0
Share

Okay - that makes sense if my building was 1 x 1. But what happens if my building is 5 x 10. It will only check the 'placement' for the centre axis of my building object, which will result in a return of one placement. Eg: Building wants to be placed at (20,50). Building is 3 x 3 - thus my centre axis is at (2,2) - how would I check the 'neighbouring' cells within the 'placement' that my building is overlapping?

avatar image korbul · May 15, 2014 at 12:06 PM 2
Share

Well you already provided a solution for this :) You mentioned that you can get the position of the building's corners.

Lets take this example your building is 3 x 5. You want to place it at x: 10 y: 10. The center of the building will be at 10 10. but the corners will fall at {(9,8),(11,12)} (you only need to points to describe a rectangle). now we only have to check the placement matrix in between those 2 positions.

{(9,8),(10,8),(11,8),(9,9), .... ,(10,12),(11,12)}

if any of those positions is 0(not buildable zone) in the matrix, then you can't place the building

avatar image
0

Answer by Johanny · May 15, 2014 at 12:56 PM

Why not put a collider only on placed buildings? Thus you can check for collision with the building you try to place. Then you just have to check for collision between placed buildings and the building you try to place. If it collides, you display it as "not placeable here" and in the other case, you build it. Could it solves your problem?

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 korbul · May 15, 2014 at 01:39 PM 0
Share

But what if you want certain areas of the map to be unbuildable?

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

22 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

Related Questions

Rect from two points issue 0 Answers

How to use a Raycast to see the distance traveled within a collider. 2 Answers

Move player to specific location and avoid obstacle. 2 Answers

Im trying to create a word game with a twist 0 Answers

Raycast not detecting ANY HITS AT ALL when starting inside a collider. 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