Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Wolfdog · Jul 24, 2015 at 11:34 AM · scripting problemgrid

Grid on uneven terrain

I made a grid using Quads. I added a texture to each and wrote a script for the quads' parent.

Here is the scene before the script: alt text

and here is the scene after the script was applied: alt text

This is my script:

     Ray ray;
     RaycastHit hit;
 
     public float aboveAmount = 0.5f;
 
     void Start () {
         foreach (Transform c in transform) {
             if (Physics.Raycast (c.transform.position, -Vector3.up, out hit)) {
                 c.transform.position = new Vector3 (hit.point.x, hit.point.y + aboveAmount, hit.point.z); // set position
                 
             } else if (Physics.Raycast (c.transform.position, Vector3.up, out hit)) {
                 c.transform.position = new Vector3 (hit.point.x, hit.point.y + aboveAmount, hit.point.z); // set position
                 
             } else {
                 Debug.Log ("couldn't hit anything");
             }
         }
     }

It casts a ray from every tile and puts the tile on the terrain (a fixed amount above it).

I would like the deform the quad's mesh to make it sit on the ground. I idea would be this: cast 9 rays from every quad (corners and inbetween) and deform the mesh to place every point onto the ground. But how would I achieve this in reality?

If there is a better way of creating a dynamic grid, then I would love to know about it. I need every tile to be a prefab, which can be clicked on. This isn't a problem yet, as I'm using box colliders.

after-script.jpg (209.5 kB)
before-script.jpg (170.0 kB)
Comment
Add comment · Show 1
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 Cherno · Jul 24, 2015 at 02:03 PM 0
Share

Witha grid of this size, you will soon run into performamnce problems if every tile is a sperate gameobject. You better use one mesh with multiple quads and handle the tile selection programmatically (as opposed to collision-based).

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Torigas · Jul 24, 2015 at 02:18 PM

i agree with Cherno, this might cause huge performance issues. Optimizations might include showing / generating the grid in just a small area around the player.

Nonetheless, one way to do this would be to create a Mesh for each grid element and do a raycast or do a terrain height sample at the corner positions.

here they explain on how to create a custom mesh object. Simply put, you have to create 4 Vertices at the Terrain position. http://docs.unity3d.com/ScriptReference/Mesh.html

An alternative would involve a whole lot more scripting but here's the basics of a possible solution:

Given a terrain with a certain size you know the width and height. (float w, float h) You also know the density of your grid --- so you know the size of one tile (float tileW = tileH) So you could technically create a two dimensional array with your grid tiles as a logical element. You also know where the user clicks (raycast and check where it hit). Taking into account the width and height and the size of one tile you can write a function that returns the grid tile which was clicked. (something like

 Tile[,] grid;
 
 Tile getTile (Vector3 clickpos)
 {
    return grid[(int)(clickpos.x/w), (int)(clickpos.y/h)];
 }

To get the visualization you can create one "huge" mesh with the method explained above. Just create a new Mesh, take the Vertex positions by sampling the Terrain height at their positions and set the UV coordinates of the vertices to match your tile texture. Meaning the bottom left vertex of a tile should be like 0,0 -- the bottom right at 1,0 and so on.

Comment
Add comment · Show 3 · 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 Wolfdog · Jul 24, 2015 at 07:31 PM 0
Share

Thanks, however I will stick to the multiple-mesh way. I need to be able to select each tile and replace its material at run time. I don't see how I would be able to do that with I mesh. Here is what I mean: alt text

here-is-what-i-mean.jpg (217.5 kB)
avatar image Cherno · Jul 25, 2015 at 12:53 AM 0
Share

You can get the world position of the point the mouse cursor is at with RayCasting. This in turn gets you the coordinates of the tile.

avatar image Torigas · Jul 27, 2015 at 12:30 PM 0
Share

If you take a look at this: http://answers.unity3d.com/questions/15832/how-to-create-a-procedural-mesh-with-multiple-mate.html

You can actually use that. So just assign two materials to the huge mesh etc.

However, considering the picture you just posted, i think you're good for performance. 256 GameObjects doesn't seem too bad.

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

Access a specific element in GridLayoutGroup by script ? 1 Answer

How to generate an occupancy grid of the scene? 1 Answer

How to debug the object position in the grid of objects? 0 Answers

Why the player is keep moving sometimes out of the grid area ? 0 Answers

Cannot set currentSelectedObject in OnMouseDown 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