Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Starleg2 · Oct 18, 2017 at 09:39 PM · spriterandomgenerationrandom gen

How can I make a Terraria Style game from sprites without using an enormous amount of gameobjects?

![alt text][1]Ever since I played the game Terraria I wanted to recreate it in Unity. My idea was instead of having a huge 10000 by 5000 grid of game objects for tiles (which would be insanely laggy) I would instead have a smaller grid of chunks that would load a per say 100x100 grid of tiles all of which would be one game object just when I attempt to mine in the chunk I would be calling a chunk update function in the one chunk I mined in. This could limit the amount of game objects rendering onscreen used for the game world to a sizable two digit number. I understand Perlin noise and how to use it to make the terrain. The main problem I am having is how to make one game object have a sprite renderer that renders a chunk of tiles. I understand how to isolate individual sprites from a sprite sheet but this seems like I want to compile a 100x100 grid of sprites as one sprite to create terrain chunks and then be able the edit the larger sprite to remove or add the smaller tiles in order to create the terraria style mining and building. Any Ideas on how to create this system would be awesome. I work in C# but I am not a professional, I like to give myself a challenge.

The attached image is just a reference image but others can be googled. [1]: /storage/temp/103996-terraria-caratula.jpg

terraria-caratula.jpg (98.5 kB)
Comment
Add comment · Show 2
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 · Oct 18, 2017 at 11:54 PM 1
Share

Stop thinking about sprites. Think about tiles. Your terrain chunks will be gameobjects with a meshfilter and meshrenderer. You generate the mesh by code, tile by tile with simple nested for... loops. Your terrain information is stored in a collection of sorts, for example a two-dimensional array with a Tile class that holds all neccessary information as the array's element.

 public class Tile {
      int contentID = 0;
 }
 
 public Tile[,] tiles = new Tile[16,16];
 for(int x = 0; x < tiles.GetLength(0);x++) {
      for(int y = 0; y < tiles.GetLength(1);y++) {
           tiles[x,y].contentID = random(0,2);//randomly make the content ID 0 or 1.
           //for example, 0 is empty and 1 is earth.
      }
 }
 
 for(int x = 0; x < tiles.GetLength(0);x++) {
      for(int y = 0; y < tiles.GetLength(1);y++) {
           //generate one quad for the mesh by using the x and y values as cordinates for one of the corners
      }
 }

This doesn't uses chunks but shows the basic idea. You could use a tilemap and map each tile's quad's UV coordinates to it so it shows the correct tile graphic.

avatar image MaxGuernseyIII Cherno · Oct 19, 2017 at 01:02 AM 0
Share

Shouldn't this be promoted to be an answer?

2 Replies

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

Answer by Vanilla-Plus · Oct 19, 2017 at 03:13 AM

What you're after is known as a 'procedural mesh'! Basically the trick is to just have one modular mesh (chunk) that contains all your grid/matrix/tile data and then update it as the player edits it. Of course for optimization, you would want to have multiple 100x100 chunks or whichever size works best.

There's a brilliant tutorial by Catlike Coding which will run you through how to make a basic procedural mesh here, but you'll need to be a bit creative when it comes to identifying different tiles.

http://catlikecoding.com/unity/tutorials/procedural-grid/

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 Starleg2 · Oct 19, 2017 at 08:39 PM 0
Share

Thanks for the help, I will be testing the solutions further over the next couple days.

avatar image Starleg2 · Oct 23, 2017 at 11:57 PM 0
Share

I've been testing out the catlike coding thing you messaged and the procedural mesh works great but I'm still fuzzy on how to interact with the mesh and how to apply different textures to different tiles. Anymore help would be great but if you feel that's enough help don't hurt yourself hunting for solutions.

Thanks- @ChiefBreakeverything

avatar image andrew-lukasik Starleg2 · Oct 25, 2017 at 08:17 AM 0
Share

To assign different texture to some triangles you separate them into different mesh (which would use material with different texture) OR you pack all textures into single atlas and just move UVs around.

avatar image
0

Answer by andrew-lukasik · Oct 25, 2017 at 08:44 AM

If you would manage to write your game such every tile lives completely outside the scene, in some big array data format for example, then Graphics.DrawMesh || Graphics.DrawMeshInstanced can help you limit GameObjects needed to render all these tiles to 0 (well ok, 1).

Idea here is to avoid mesh re-construction step every n-frame and just use single mesh (quad), and draw it in world position of every visible tile. Different material can be used here to represent different tile type.

Write your own collisions etc. on top of that and you will need little GameObjects indeed to run your game.

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

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

104 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Somthing about my script i making unity hang if i set the room count any higher that 0 can't figure out what though ? 1 Answer

Randomly Generated Objects inside an area 3 Answers

Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer

how to create random levels. like a golf game 3 Answers

Can I generate a sprite for a specific 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