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 T27M · Apr 30, 2012 at 09:45 PM · c#instantiateraycastprefabgrid

Grid Placement In Game

So i'm looking for some input to see if i'm going about this the most efficient way. I created some code that would spawn a grid of objects, I tried this with cubes,planes and emptys each with the renderer.enabled = false. I am trying to create a grid for building placement.

The intial attempt I set the size to 5x5 and spawned them to fill the entire terrain which was 512x512. It was roughly 1000 objects I left a slight margin around the edge of the terrain. There was a slight delay on startup around 5 seconds. I thought of creating a prefab with the grid rather than instantiating it, but unity would just freeze up any time I tried to move that many objects into a prefab.

Second attempt I created a larger grid with empty objects with a box collider attached, this was pritty much instant and I added that to a prefab and left it in the scene. Then created a smaller grid of 0.5 x 0.5 squares with a 10x10 grid that I would instanciate if a certain boolean was true and the players mouse was in that section of the grid and then destroy it. The smaller grid had a renderer on it but would only enabled if the player was placing a building over that tile.

So my questions ..

Is there a better way todo it ?

Should I just instantiate everything and add some sort of loading screen?

I had some strange results with changing the size of the box collider and the scale. Should I just change the scale and leave the collider at the default ?

Is there an easy way to make the grid follow the height of the terrain, maybe apply something like a height map to it ?

The function that called the instantiate on the grid would happen if the raycast hit a tag which was the "grid", the smaller grid would rather than appear in the single section would just spawn a grid in each section. Is this todo with the empty I'm using to hold them ?

A real pain I had is going through and reseting the script so I could actualy chose the object I wanted to instantiate or forgot to set the correct tag on the object. Is there a quick way of reseting/setting multiple scripts/tags on objects ?

Hope this is as clear to everyone else as I think it is :P

Thanks

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 -hiTo- · May 01, 2012 at 12:55 PM

I think you're going about this the wrong way.

You know how big your map is. Just divide that number with how big you want your grids spaces to be. Then move your buildings in increments of that number. => Grid.

To get a visual representation, do you really need to see the grid for the entire map at the same time? You could just make a small texture that displays the grid near your mouse, and move that in increments, just like the buildings. If you do want the entire map enclosed in a visual grid, you could just reuse your first method, but doing it in a coroutine. That way it will not stall the rest of the program.

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 T27M · May 01, 2012 at 02:08 PM 0
Share

Sounds like an idea I'll give it a go. I don't need to see the entire grid thats why I tried to make it in sections so it would only appear when the mouse was near that section. I'll play around and see what I can do. Thanks

avatar image T27M · May 01, 2012 at 08:41 PM 0
Share

So I couldn't implement the way you said todo it , I did however get the grid method working seems my lag was co$$anonymous$$g from other code and not the grid creation. I'd still like to see how you would code it if you could post it ?

Thanks.

avatar image
0

Answer by adept22 · May 09, 2012 at 01:50 AM

Hi I use the belowcode solution to get a grid position based on a vector.

public static float gridSpacing = 1f; (this is a 1unit x 1unit size grid)

//Pass your mouse position to the below function and it will return the nearest grid co-ord to you.

public static Vector3 GetGridPosition (Vector3 convertPosition) {

float newx = Mathf.Round (convertPosition.x / gridSpacing) * gridSpacing;

float newz = Mathf.Round (convertPosition.z / gridSpacing) * gridSpacing;

convertPosition = new Vector3 (newx, (convertPosition.y), newz);

return convertPosition;

}

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 T27M · May 09, 2012 at 09:08 AM 0
Share

Great stuff, Ill give that a try and post back.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating an Object with a string C#! How? 1 Answer

How to instantiate multiple clones of the same prefab ? 1 Answer

How to randomly instantiate other prefabs parallel? 1 Answer

Instantiate prefab within parameters?(Javascript) 1 Answer

Destorying prefab and instanstiating again? 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