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 baDa · Jan 19, 2011 at 01:52 PM · rotationparentrubikscube

Rotation of multiple cubes!

Hello there!

I have one big problem, and i can't solve it, i never work with 3D before, i come from cocos2D iPhone and Actionscript 3 Flash! So, the things i need help over there can be realy simple to someone to know 3D, but its killing me!

I have one big cube with many cubes inside then, like that: Cube Example

To create then i make this code:

void createSphere () {

     float startX = -2.5f;
     float startY = -2.5f;
     float startZ = 8f;

     for(int y = 0; y < 6 ; y++)
     {
          for (int z = 0; z < 6; z++) 
          {
             for ( int x = 0; x < 6; x++) 
             {
                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

                 cube.transform.position = new Vector3(x+(0.1f*x)+startX,y+(0.1f*y)+startY z+(0.1f*z)+startZ);

                 int rand = Random.Range(1,4);

                 switch(rand)
                 {
                     case 1:
                         cube.transform.renderer.material.color = Color.red;
                     break;
                     case 2:
                         cube.transform.renderer.material.color = Color.blue;
                     break;
                     case 3:
                         cube.transform.renderer.material.color = Color.green;
                     break;
                 }

             }
          }
     }
 }

What i want to do, is detect when use make a slice over iPhone screen! And make the all cube rotate around itself:

Detect the side of slide:

if(Input.GetMouseButtonDown(0)) { lastMousePos = Input.mousePosition; actualMousePos = Input.mousePosition; press = true; } if(press) {

         if(actualMousePos.x > (lastMousePos.x + 20f))
         {
             cubesHolder.transform.RotateAround(Vector3.up, 0.05f);
         }
         else if(actualMousePos.x < (lastMousePos.x - 20f))
         {
             cubesHolder.transform.RotateAround(-Vector3.up, 0.05f);
         }



         lastMousePos = Input.mousePosition;
     }
     if(Input.GetMouseButtonUp(0))
     {
         lastMousePos    = new Vector2();
         actualMousePos  = new Vector2();
         press           = false;
     }

But he cube start loose himself with some rotations, make sense with 3D, but what is the best way to make this work?

i upload the unity3D file here:

try.html">http://www.2shared.com/file/u7nJbJhW/Unity_try.html

Comment
Add comment · Show 2
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 Jean-Fabre · Jan 19, 2011 at 02:01 PM 0
Share

Are you trying to actually rotate the camera to view your cube from a different angle, or do you really want to rotate the cube in space, this is different and sometimes difficult to grasp when you start with 3d.

avatar image baDa · Jan 19, 2011 at 06:48 PM 0
Share

What i want its move the cube! Rotate the camera gonna make all the calculations relative to the camera go wrong!

1 Reply

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

Answer by Ricardo Arango · Jan 20, 2011 at 02:11 PM

Attach this script to any GameObject. It will create the cubes and make them children of the GameObject. I changed the script so the number of cubes and spacing is changeable.

using UnityEngine; public class Cube : MonoBehaviour { Vector2 lastMousePos, actualMousePos; bool press = false; public GameObject cubesHolder; public Vector3 nCubes = new Vector3(6f, 6f, 6f); public Vector3 spaceBetweenCubes = new Vector3(1f, 1f, 1f); private Vector3 cubeSize; private Vector3 cubeHalfSize; private Color[] colors;

 void Start () {
     colors = new Color[3] { Color.red, Color.blue, Color.green };
     createLotsOfCubes();
 }

 void Update () {
     if(Input.GetMouseButtonDown(0))
     {
         lastMousePos = Input.mousePosition;
         actualMousePos = Input.mousePosition;
         press = true;
     }
     if(press)
     {
         lastMousePos = Input.mousePosition;
         if(actualMousePos.x > (lastMousePos.x + 20f))
             transform.Rotate(Vector3.up, 0.5f);
         else if(actualMousePos.x < (lastMousePos.x - 20f))
             transform.Rotate(-Vector3.up, 0.5f);            
     }
     if(Input.GetMouseButtonUp(0))
     {
         lastMousePos = Vector2.zero;
         actualMousePos = Vector2.zero;
         press = false;
     }
 }

 void createLotsOfCubes ()
 {
     // Actual renderer size
     GameObject tmpCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
     cubeSize = tmpCube.renderer.bounds.size;
     cubeHalfSize = cubeSize/2f;
     DestroyImmediate(tmpCube);

     // There are nCubes - (1,1,1) spaces between the cubes so the total size is
     Vector3 figureSize = new Vector3( nCubes.x * cubeSize.x + (nCubes.x - 1f) * spaceBetweenCubes.x, 
                                                         nCubes.y * cubeSize.y + (nCubes.y - 1f) * spaceBetweenCubes.y, 
                                                         nCubes.z * cubeSize.z + (nCubes.z - 1f) * spaceBetweenCubes.z );
     // Half of the size of the entire set of cubes
     Vector3 figureHalfSize = figureSize / 2.0f;

     for(int y = 0; y < nCubes.x ; y++) 
     {
          for (int z = 0; z < nCubes.y ; z++)  
         {
             for ( int x = 0; x < nCubes.z ; x++)  
              {
                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 // Set this object as the parent object
                 cube.transform.parent = transform;
                  // The position is : -extent + size of cube + spacing + offset to center (half size of cube)
                 cube.transform.position = new Vector3( -figureHalfSize.x + x*(spaceBetweenCubes.x + cubeSize.x) + cubeHalfSize.x, 
                                                                             -figureHalfSize.y + y*(spaceBetweenCubes.y + cubeSize.y) + cubeHalfSize.y, 
                                                                             -figureHalfSize.z + z*(spaceBetweenCubes.z + cubeSize.z) + cubeHalfSize.z );
                 // Change the color
                  cube.transform.renderer.material.color = colors[Random.Range(0, 3)];
             }
          }
     }
 }

}

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

No one has followed this question yet.

Related Questions

Child versus Parent rotations 3 Answers

Ignore parent Rotation 1 Answer

Rotating a parent object so that child is at the closest possible position to another object 1 Answer

Why does the child of a game object not rotate to face mouse position when the parent object is moving? 1 Answer

Rubik's Cube, grouping and rotating problems 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