• 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 Robstao · Mar 24 at 08:39 PM · transformgrid based game

having a object create itself within the constraints of another objects size

I'm trying to build a level with 'stages' or zones that the players can play within. I've still learning and I followed a grid system tutorial that works quite well with object snapping and grid resizing, but I've run into this issue in the past of wanted something to be created or stuck within the constraints of another object, and I have no idea how to return the actually world space location (length/width for example) and use them as variables.

Here is the grid system, it's simple.

 public class Grid : MonoBehaviour
 {
     [SerializeField]
     private float size = 1f;
 
     // Start is called before the first frame update
     public Vector3 GetNearestPointOnGrid(Vector3 position)
     {
         position -= transform.position;
         int xCount = Mathf.RoundToInt(position.x / size);
         int yCount = Mathf.RoundToInt(position.y / size);
         int zCount = Mathf.RoundToInt(position.z / size);
         Vector3 result = new Vector3(
             (float)xCount * size,
             0f,
             (float)zCount * size);
         result += transform.position;
         return result;
     }
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.white;
         for (float x = 2; x < 27; x += size)
         {
             for (float z = 2; z < 27; z += size)
             {
                 var point = GetNearestPointOnGrid(new Vector3(x, 1f, z));
                 Gizmos.DrawCube(point, new Vector3(1.0f, 0.5f, 1f));
             }
         }
     }
 }

under the draw grizmo's, manually change the location of my playing field to see the grid. I understand the grid is drawn globally almost, where it's actually a fairly massive object.

Lets say I have a cube, that's 25,1,25. (25 units wide and 25 units long). If I move this game object, how would I contain the grid within it, making it possible to spawn this cube anywhere and have a building grid on it?

alt text

capture.png (69.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Mar 24 at 11:28 PM

I’m not sure I know your exact intentions from your explanation in your question, but I’m going to take a stab at it. So, assuming you have one cube of 25,1,25 in size and you know how you want to space the grid above or within. Let’s say for argument sake, you want a grid of cubes to lay on top like you have in your picture. Let’s assume that your spacing of the cubes within the grid is 1 unit in each direction. So, you have a grid of 25 x 25 cubes on top of your large cube that stretches the width and length of the grid. On the script that is attached to the large cube, you can store the grid positions relative to wherever it is in world space:

 int columns = 25;
 int rows = 25;
 float spacing = 1; //leave 1 unit between each grid object
 Vector3 startPos = new Vector3(gameObject.transform.position.x - (gameObject.transform.lossyScale.x * 0.5f + (spacing * 0.5f)), gameObject.transform.position.y + gameObject.transform.lossyScale.y, gameObject.trabsform.position.z - (gameObject.transform.lossyScale.z * 0.5f + (spacing * 0.5f)));
 Vector3[,] grid = new Vector3[columns, rows];
 
 for(int i = 0, i < columns, i++){
 for(int j= 0, j < rows, j++){
 Vector3[ i, j] = new Vector3(startPos.x + (i * spacing), startPos.y, startPos.z + (j * spacing));
 //you can instantiate a grid cube at this position here to build the physical grid 
 }
 }

The above will build a new grid based on the location of the game object and its size. Now I can make the grid at the start of the game or if I move the large cube at some point of the game, just have to run this code, and the grid is the exact same size and in the same location in relationship to the large parent cube.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 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 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

Why does my character still move! 2 Answers

Decrease size of grid with prefabs? 0 Answers

javascript to respawn player in 2d platformer not working 1 Answer

Make camera 1 transform equal to camera 2 transform 1 Answer

Modify the scale of an another transform 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges