Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 rkaetano · May 31, 2013 at 04:12 AM · instantiategridtilesmousedrag

Problems creating a grid in Runtime

Hey guys, I'm trying to create a grid using the mousedrag. My code is working more or less cause I've some strange behaviours. Can some one check my code and maybe give some ideas to make this better, and even make this works in a correct way ?

Thanks.

 public class BuildingControl : MonoBehaviour {
     public bool enableConstruction = true;
     public Transform constructionBlock;
     
     private Vector3 _startPoint;
     private Vector3 _endPoint;
     private Vector2 _squareSize;
     private Vector2 _oldSquareSize;
     private bool    _constructionStarted;
     private List<Transform> _cubes;
     
     
     void Start ()
     {
         _cubes = new List<Transform>();
     }
     
     void CreateCubes ()
     {
         if (_oldSquareSize != _squareSize) {
             foreach (Transform cube in _cubes) {
                 Destroy (cube.gameObject);
             }
             _oldSquareSize = _squareSize;
             _cubes = new List<Transform> ();
         }
         
         for (int iCol = 0; iCol < _oldSquareSize.x; iCol++) {
             for (int iRow = 0; iRow < _oldSquareSize.y; iRow++) {
                 Transform newCube = Instantiate (constructionBlock) as Transform;                
                 Vector3 newPosition = Vector3.zero;
                 newPosition.x = (_startPoint.x >= _endPoint.x ? _startPoint.x - iCol : _endPoint.x + iCol);
                 newPosition.z = (_startPoint.z >= _endPoint.z ? _startPoint.z - iRow : _endPoint.z + iRow);
                 newPosition.y = 0.5f;
                 newCube.position = newPosition;                
                 _cubes.Add (newCube);
             }
         }
     }
     
     void OnGUI ()
     {
         if (GUILayout.Button ("Enable Construction")) {
             enableConstruction = !enableConstruction;
         }
         
         GUILayout.Label ("Start Point : " + _startPoint.ToString ());
         GUILayout.Label ("End Point   : " + _endPoint.ToString ());        
         GUILayout.Label ("SquareSize  : " + _squareSize.ToString ());        
     }
     
     void Update ()
     {
         RaycastHit hit;
         
         if (Input.GetMouseButtonUp (0)) {            
             if (!_constructionStarted) {
                 if (Physics.Raycast (Camera.mainCamera.ScreenPointToRay (Input.mousePosition), out hit, 1000)) {        
                     _startPoint = new Vector3 (Mathf.RoundToInt (hit.point.x), 0.5f, Mathf.RoundToInt (hit.point.z));
                     _constructionStarted = true;
                     _squareSize = new Vector2 (0, 0);
                 }                
             } else {
                 _startPoint = Vector3.zero;
                 _endPoint = Vector3.zero;
                 _squareSize = Vector2.zero;
                 _constructionStarted = false;
             }            
         }     
                 
         if (_constructionStarted) {
             if (Physics.Raycast (Camera.mainCamera.ScreenPointToRay (Input.mousePosition), out hit, 1000)) {            
                 _endPoint = new Vector3 (Mathf.RoundToInt (hit.point.x), 0.5f, Mathf.RoundToInt (hit.point.z));
                 _squareSize.x = Mathf.Abs (_endPoint.x - _startPoint.x); 
                 _squareSize.y = Mathf.Abs (_endPoint.z - _startPoint.z);                
             }                        
         }                        
         
         if (_oldSquareSize != _squareSize) {
             CreateCubes ();
         }        
         
     }
         
 }


Ok, you're right.

So, here are some shots.

When I first click, the script creates my first cube: http://imageshack.us/a/img836/9599/capturadetela20130601as.png

If I move down, I can have this doubled: alt text

But when I move to the side, the script removes the first column, and starts to create from the second column: alt text alt text

If I go to the other side, I don't have this behaviour. Ok, not all behaviour, cause I lose the first column, but I think that it's the same problem. I'm trying to find out the real problem for myself, I believe that could be a stupid logic problem, but untill now, I'm still lost... :P

Can someone give me any ideas about this ?

Comment
Add comment · Show 3
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 rkaetano · Jun 01, 2013 at 02:17 PM 0
Share

Should I try this on forum ?

avatar image Graham-Dunnett ♦♦ · Jun 01, 2013 at 02:20 PM 0
Share

Why not take the time to explain what "strange behaviours" means and describe in more detail, or use a screenshot or video, to make your problem easier to understand.

avatar image rkaetano · Jun 01, 2013 at 03:06 PM 0
Share

Done it, Graham

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rkaetano · Jun 01, 2013 at 03:52 PM

Ok guys, I just found the solution for my problem. I just needed to change the cube creation For Loop This is the new loop:

         for (int iCol = 0; iCol < _oldSquareSize.x; iCol++) {
             for (int iRow = 0; iRow < _oldSquareSize.y; iRow++) {
                 Transform newCube = Instantiate (constructionBlock) as Transform;                
                 Vector3 newPosition = _startPoint;
                 newPosition.x += (_startPoint.x >= _endPoint.x ? -iCol : +iCol);
                 newPosition.z += (_startPoint.z >= _endPoint.z ? -iRow : +iRow);
                 newPosition.y = 0.5f;
                 newCube.position = newPosition;                
                 _cubes.Add (newCube);
             }
         }

I just don't know if this is the most performatic way to achieve the same mechanic. :P If someone can provide me any insights, i would be grateful.

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

14 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

Related Questions

How to find a circle (radius) in grid tiles around another tile 1 Answer

Keeping track of which areas remain unexplored 1 Answer

How to instantiate the prefabs on grid in Unity? 1 Answer

how to tell if fence encloses area 1 Answer

Improving Grid Algorithm 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