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 Echo182 · Nov 24, 2017 at 10:43 PM · blendergridblocks

Paint Blocks on a 3D Grid

Hi, I am fairly new to the whole Unity scene, so I have a question about what I am trying to do. Currently, I am trying to make an editor where you can select blocks from a menu on the side of the screen and place/paint them in a 3D grid. I already have the textures for my blocks made, and I'm just not really sure where to go from here. I have done some research already and none of what I found is exactly what I am looking for. If anyone can offer help that would be much appreciated. Thanks!

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 · Nov 24, 2017 at 11:19 PM 0
Share

A very broad question. It would be better to split it into smaller parts for each issue involved.

First of all, you need some kind of class that stores the data of each position in the 3d grid. Let's call this class "Cell".

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class Cell {
 
     public int type = 0;
 }

In this cast, it only had one variable which stores the type as an integer ID. For example, empty would be 0, dirt would be 1, and so on.

Then you need to a way to store and access each cell data. I find it easiest to do it via a Dictionary that takes three integers which are the coordinates of the cell as the key and it returns the cell data as the value. So, since a normal Dictionary doesn't take more than one key, we need a class that holds these three xyz values, like a Vector3 but with simple integers.

 using UnityEngine;
 using System.Collections;
 using System;
 
 [System.Serializable]
 public struct Coordinate3 : IEquatable<Coordinate3> {
     public int x;
     public int y;
     public int z;
 
     
     public Coordinate3() {
         x = 0;
         y = 0;
         z = 0;
     }
     
 
     public Coordinate3(int a, int b, int c) {
         x = a;
         y = b;
         z = c;
     }
 
     public Coordinate3(Vector3 v3) {
         x = $$anonymous$$athf.RoundToInt(v3.x);
         y = $$anonymous$$athf.RoundToInt(v3.y);
         z = $$anonymous$$athf.RoundToInt(v3.z);
     }
 
     public bool Equals(Coordinate3 other) {
 
         if (this.x == other.x && this.y == other.y && this.z == other.z) {
             return true;
         }
         else {
             return false;
         }
     }
 
     public override int GetHashCode() {
         return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
     }
 
 }
 
 
 

Now the dictionary itself:

 Dictionary<Coordinate3,Cell> cells = new Dictionary<Coordinate3,Cell>();
 
 Coordinate3 coords = new Coordinate3(3,1,7);

Null Reference Error-proof example of accessing a stored cell:

 Cell cell = null;
 if(cells.TryGetValue(coords,out cell) == true) {
      cell.type = 1;
 }

Now comes the final part: actually getting a coordinate in the grid. I usually have a large invisible plane with a collider at the current editing level which encompasses the whole grid area. I cast a ray from the current cursor position forward from the camera and the hit.point (if any), converted into a Coordinate3, is the coordinate the cursor is pointing at. The conversion math depends on how large each grid space is.

This would be the "behind the scenes" stuff. To actually see any changes in the game world, you need to build mesh chunks based on the cell data in the grid. there are dozens of examples for $$anonymous$$inecraft-like games made in Unity on the web, including the massive After playing $$anonymous$$ecraft... thread on the Uniy forums.

0 Replies

· Add your reply
  • Sort: 

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

86 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

Related Questions

How can I offset blocks in a tower to build a Jenga-like structure? 0 Answers

Performance tips on grid base game 1 Answer

MaK like building script help... 0 Answers

Problem searching grid using recursive function 2 Answers

Blender To Unity - Parented Mesh Object Being Deformed? 1 Answer


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