• 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
Question by Komak57 · Jul 14, 2014 at 05:18 AM · rotationgameobjectgrid

Grid Increment by Rotation

Working on generating a 3D GameObject Array of cubes grid-locked and angled to match the initial rotation. Although all of my objects spawn in the correct rotation, the grid itself is world-aligned.

alt text

The grid is populated by the bottom-left most grid piece and then placed accordingly.

Noteworthy Global Variables:

 public float GridLength = 3.5f;
 GameObject[,,] Grid;
 Vector3 Rotation = Vector3.zero;
 Vector3 PointA = Vector3.zero;
 Vector3 PointB = Vector3.zero;

Important Snippet (in Update)

 // Gridlock scale to point B
                                 int a = Mathf.RoundToInt((PointB.x-PointA.x)/GridLength),               // difference in X length
                                         b = Mathf.RoundToInt((PointB.y-PointA.y)/GridLength)+1, // difference in Y length
                                         c = Mathf.RoundToInt((PointB.z-PointA.z)/GridLength);           // difference in Z length
                                 if (Grid.Length != a*b*c && (Mathf.Abs(a*b*c) > 0)) {
                                         Debug.Log("Size Changed. Grid["+a+","+b+","+c+"]");
                                         // clear grid
                                         foreach(GameObject o in Grid) {
                                                 Destroy (o);
                                         }
                                         // repopulate grid
                                         Grid = new GameObject[Mathf.Abs(a),Mathf.Abs(b),Mathf.Abs(c)];
                                         for (int k = Mathf.Min(0, c+1); k < Mathf.Max(c, 0); k ++) {
                                                 for (int j = Mathf.Min(0, b+1); j < Mathf.Max(b, 0); j ++) {
                                                         for (int i = Mathf.Min(0, a+1); i < Mathf.Max(a, 0); i ++) {
                                                                 try {
                                                                         Grid[Mathf.Abs(i),Mathf.Abs(j),Mathf.Abs(k)] = GameObject.Instantiate(Prefab, PointA+new Vector3(i*GridLength, j*GridLength, k*GridLength), Quaternion.Euler(Rotation)) as GameObject;
                                                                         Grid[Mathf.Abs(i),Mathf.Abs(j),Mathf.Abs(k)].transform.GetComponent<BoxCollider>().enabled = false;
                                                                 } catch {
                                                                         Debug.LogError("Error creating Grid["+Mathf.Abs(i)+","+Mathf.Abs(j)+","+Mathf.Abs(k)+"]");
                                                                 }
                                                         }
                                                 }
                                         }


I need to align this based on the Rotation vector (EulerAngles).

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Komak57 · Jul 14, 2014 at 11:38 PM

There were actually 2 problems aligned with this question, but your solution would surely work for one. First, the Grid jumps are world locked, not local. Next, the gameobjects were being placed on world jumps, not local.

The first solution was given on #Unity3D:

 Grid[Mathf.Abs(i),Mathf.Abs(j),Mathf.Abs(k)] = GameObject.Instantiate(Prefab, PointA+new Vector3(i*GridLength, j*GridLength, k*GridLength), Quaternion.Euler(Vector3.zero)) as GameObject;
 Grid[Mathf.Abs(i),Mathf.Abs(j),Mathf.Abs(k)].transform.RotateAround(PointA, Vector3.up, Rotation.y);

This generates the item at world grid coordinates, and then pivots off of the start point. Your option would certainly work in this scenario as well. The next problem was with A,B,C. I have to walk North in jumps to increase Z count, East for X, and vertical for Y. Well, there's no rotation on Y, so that was fine there. What I got so far was something like this:

 float dist = Vector3.Distance(PointB, PointA);
 float angle = Mathf.Atan2(PointB.z - PointA.z, PointB.x-PointA.x);
     //angle += Rotation.y;
 Vector3 diff = new Vector3(
     (Mathf.Cos(angle)*dist),
     PointB.y-PointA.y,
     (Mathf.Sin(angle)*dist));
 // Gridlock scale to point B
 int a = (int)Mathf.Round(diff.x / GridLength),        // difference in X length
     b = (int)Mathf.Round(diff.y / GridLength),    // difference in Y length
     c = (int)Mathf.Round(diff.z / GridLength);        // difference in Z length
 if (Mathf.Abs(a) < 1) a = 1; if (Mathf.Abs(b) < 1) b = 1; if (Mathf.Abs(c) < 1) c = 1;

First, grab the distance from start to end. Next, reverse check for the angle. Use trig, using distance as the Hypotenuse, to find the X/Y offset on those coordinates. This swaps North/East to Forward/Right. Still some minor tweaks to perform before I can move forward.

Comment

People who like this

0 Show 0 · 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

Answer by robertbu · Jul 14, 2014 at 07:46 AM

You can handle this in a couple of different ways. Given a pivot point for all of your quads called 'pivotPoint', you can change the position this way:

 Vector3 newPos = Quaternion.Euler(-Rotation) * (oldPos - pivotPoint) + pivotPoint;

You can either do this to the transform.position of each object after you instantiate the object, or you can do it to the position you calculate before the Instantiate() call. I'm using '-Rotation' because you want the whole group to be rotated in the opposite direction you are using for the individual quads. For example using your code above, you could do:

 Vector3 pos = PointA+new Vector3(i*GridLength, j*GridLength, k*GridLength);
 pos = Quaternion.Euler(-Rotation) * (pos - pivotPoint) + pivotPoint;
 Grid[Mathf.Abs(i),Mathf.Abs(j),Mathf.Abs(k)] = GameObject.Instantiate(Prefab, pos, Quaternion.Euler(Rotation)) as GameObject;

Another solution would be to place an empty game object at the pivot point, then make all the objects children of the empty game object. By rotating the empty game object, all the children will be rotated.

Comment
Komak57

People who like this

1 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

A node in a childnode? 1 Answer

Rotate GameObject using Coroutine 2 Answers

C# Rotate GameObjects Regardless of List Size 2 Answers

C# GameObject Reverses Z Rotation 0 Answers

Unity OnMouseDrag too fast for Update function 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