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 dustingunn · Sep 15, 2013 at 07:24 PM · rotationgridrotate object

Rotating an object in a 3D grid

I have a system working for building a custom object out of blocks in a 3D grid environment. I manually set the size of each piece in a Vector3 and then add it like so:

     public void AddToGrid (Vector3 gridPos, GunPart _part) {
         //adds a reference to an object for every space it occupies on the grid
         int x = (int)gridPos.x;
         int y = (int)gridPos.y;
         int z = (int)gridPos.z;
             
         for(int sx = 0; sx < _part.size.x; sx++){
             for(int sy = 0; sy > -_part.size.y; sy--){
                 for(int sz = 0; sz < _part.size.z; sz++){
                     //adding the part at XYZ
                     gunGrid[sx + x, sy + y, sz + z] = _part.gameObject;
                 }
             }
         }
     }

If I want to be able to rotate the pieces, I'm not sure how to take the euler angles and apply it to that size to accurately shift the X,Y and Z of the size. The origin of each piece is in the top/left/back so I'd also need to know where it's orienting and how to move along the grid accordingly. I've tried to google this but found nothing.

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
2
Best Answer

Answer by Benproductions1 · Sep 15, 2013 at 11:18 PM

Hello,

This is the main reason why Quaternions are amazing!

If you create a quaternion rotation:

 var Q:Quaternion = Quaternion.Euler(0, 90, 0);

You can rotate a Vector3 around the origin (`Vector3.zero`):

 var V:Vector3 = Q*vector;

It really is that simple.

 var V:Vector3 = Quaternion.Euler(0, 180, 0)*Vector3.forward;
 //V will equal Vector3.backward

It''s leave the "apply to situation" to you :)

Hope this helps,
Benproductions1

Comment
Add comment · Show 7 · 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 dustingunn · Sep 16, 2013 at 04:36 PM 0
Share

This randomly makes the numbers slightly off and negative but those are easy to fix with $$anonymous$$athf functions. Other than that, thanks! The only thing I'm struggling with now is since the origin is no longer in a fixed place, how would I fill or remove from the grid? Not sure how to adjust the nested loop to it.

Edit: I guess the rotations making the size negative could actually be useful for this!

avatar image Benproductions1 · Sep 17, 2013 at 09:12 AM 0
Share

@dustingunn There is a accuracy trade off when using any rotation values. There is no need to round off the values because that error is so $$anonymous$$ute it's unnoticeable. I don't understand what you mean by "It makes the numbers negative". It allows you to rotate a Vector3 around (0, 0, 0). The angle is irrelevant and the values are correct :)

avatar image dustingunn · Sep 17, 2013 at 08:12 PM 0
Share

I do have to round it because I'm working with integers and 0.99999 would still equal 0. Array positions can't be float.

avatar image Benproductions1 · Sep 18, 2013 at 07:37 AM 0
Share

lol, I was assu$$anonymous$$g you were using Vector3's for positions (like you should be). Its no reason you should be making 3 more variables

avatar image dustingunn · Sep 18, 2013 at 08:24 AM 0
Share

Like I just said, array positions can't be a float. I have no idea what you're talking about. I'm not positioning anything in the world, I'm storing objects in a 3D grid.

Show more comments
avatar image
0

Answer by JamieSinn · Sep 15, 2013 at 10:32 PM

Usually you are going to want to use float's for positions, but it may not be as useful if there is just a grid. Have you looked into using _part.gameObject.transform.Rotate() ?

Comment
Add comment · Show 3 · 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 Benproductions1 · Sep 16, 2013 at 07:24 AM 0
Share

float's for positions... What the hell are you doing? A one dimensional game? Positions are either a Vector2 (2D) or a Vector3 (3D) and in any game engine always should be some sort of Vector class.

avatar image JamieSinn · Sep 16, 2013 at 01:03 PM 0
Share

float x = somevector3.x; etc.

avatar image Benproductions1 · Sep 17, 2013 at 09:27 AM 0
Share

@JamieSinn again how is that usefull? Just do somevector3.x. Why go though the trouble of defining another variable that's really just going to take up space and confuse people reading your code.

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

17 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

Related Questions

Rotate object based on another rotation above a certain threshhold 1 Answer

gameobject stops moving correctly when rotating 1 Answer

Smooth rotation about global axis instead of local axis. 1 Answer

The object turn but the axis dont (SOLVED) 3 Answers

I need Some Info About Rotation And Pvot 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