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 Jayboy · Apr 07, 2016 at 07:48 PM · spritesgridarea

make grid of gameobjects between 2 points

I have been trying to do this for a few hours in unity now but cant get my head around it. basically i have 4 points(vector3) 1 each for the 4 corners topleft, topright, bottomleft, bottomright. now i have a sprite that i use as a block what i want to do is basically fit X number of blocks within the four points. see the image below. I cant think of a way to do this right now. i would appreciate any Ideas on how to do this. alt text

grid-in-an-area.jpg (42.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

2 Replies

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

Answer by b1gry4n · Apr 07, 2016 at 11:58 PM

Wanted to make sure this works 100%, so I did a test run.

The Setup: If you want it to line up, offset the mesh/sprite object inside a parent game object by half so that the transform of the parent sits at the bottom left corner of the object. Notice where the transform is in the image alt text

The Result: alt text

 using UnityEngine;
 using System.Collections;
 
 public class ResizeGrid : MonoBehaviour {
 
 
     public Transform pointA;
     public Transform pointB;
     public Transform pointC;
     public GameObject spritePfb;
 
     public int gridWidth = 5;
     public int gridHeight = 5;
 
     void Start()
     {
         GenerateGrid(pointA.position, pointB.position, pointC.position);
     }
 
 
     void GenerateGrid(Vector3 a, Vector3 b, Vector3 c)
     {
         GameObject container = new GameObject();
         container.name = "Grid Container";
         container.transform.position = c;
         float spriteWidth = Vector3.Distance(a, b) / gridWidth;
         float spriteHeight = Vector3.Distance(a, c) / gridHeight;
 
         for (int x = 0; x < gridWidth; x++)
         {
             for (int y = 0; y < gridHeight; y++)
             {
                 GameObject sprite = (GameObject)Instantiate(spritePfb, container.transform.position, Quaternion.identity);
                 sprite.transform.parent = container.transform;
                 sprite.transform.localPosition = new Vector3(x * spriteWidth, y * spriteHeight, 0);
                 sprite.transform.localScale = new Vector3(spriteWidth, spriteHeight, 1);
                 
             }
         }
     }
 }



spritegrida.jpg (19.5 kB)
spritegridc.jpg (21.4 kB)
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
avatar image
0

Answer by Happy-Zomby · Apr 07, 2016 at 08:38 PM

Hi, What part are you struggling with? You will need to instantiate 5 rows of 5 sprites - to do so you use something like this

         for (int i = 0; i < 5; i++)
         {
             for (int j = 0; j < 5; j++)
             {
             //here you put your code to instantiate 1 sprite
             //then you use i and various items to set your Y position 
             //and j to set the X position
             }

You will need to calculate your distance between top left and top right and between top left and bottom left and divide that by 5 then use that to scale your sprite and also to move in on x or y (multiplying it by i or j) depending... you may also need to add an additional half of that 5th if you want it to start with the top left corner in the top left of your first sprite.

Below is some code I used to generate a grid - it may help

 function GenerateGrid ()
 {
 generateGrid = false;
 for(var i : int = -halfWidthX; i <halfWidthX; i++)
     {
     for(var z : int = -halfHeightZ; z <halfHeightZ; z++)
         {
         var tempTile = Instantiate(tile,Vector3(startPos.x+i,startPos.y,startPos.z+z),Quaternion.identity);
         tempTile.transform.name = "Tile";
         tempTile.transform.parent = gridHolder;
         }
     }
 }

hope that helps,

Comment
Add comment · Show 4 · 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 Jayboy · Apr 07, 2016 at 09:26 PM 0
Share

this part is good. i am struggling with scaling the sprite so that they fit next to each other.the way i am doing it is, I take 2 points - left and right. then generate the vectors in between the two points by using lerp in a nested loop to get the full set of vector3 positions for all the game objects. the only problem is, when i place the sprites in position they overlap each other.i need to scale them so that they fit within the 2 points edge to edge. so really the scaling is the only issue. any pointers on that?

avatar image Happy-Zomby Jayboy · Apr 08, 2016 at 07:07 AM 0
Share

Did you try using the local scale of your sprite? transform.localscale? assu$$anonymous$$g you initial block size is 1 x 1 you would change the localscale.x to 1/5 of the distance between your topleft and top right (e.g. if you distance is 8.5) and the distance between top and bottom is 12.

 yourSprite.transform.localScale = new Vector2(8.5/5,12/5);
 //or something like that

does that help?

avatar image Happy-Zomby Happy-Zomby · Apr 08, 2016 at 07:11 AM 0
Share

In any case I believe the answer of @b1gry4n below answers to your needs and is much more complete than $$anonymous$$e

avatar image Jayboy · Apr 08, 2016 at 09:24 AM 0
Share

hey @happy Zombay. yup that answer helped.the resizing method is perfect after i tweaked it to maintain the square shape . will put up the final code i use over here in some time. thank you both for the help appreciate it. :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Want to detect if object is in grid. 1 Answer

How to use the same tilemaps among multiple scenes (e.g. making it a prefab)? 0 Answers

Creating a visual representation based on an ArrayList 0 Answers

Unity 5.6 Creating a Distortion in Sprites? 1 Answer

What resolution should my game be? 2 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