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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by xChris6041x · Apr 12, 2015 at 08:31 AM · c#rotationrotatearoundrotatearoundpivot

Rotating a Transform around another Transform on the X, Y, and Z Axis

I've been playing with Unity 5 for about a two weeks now and I'm starting to get the hang of it. The one thing I don't have a complete understanding of is rotation outside using Euler angles.

So what I built is a way to fracture a cube into many tiny cubes of the same relative scale. I am trying to make the transition between one cube to many as seamless and possible. Here is the code I have.

 public class Destructor : MonoBehaviour {
     
     /// <summary>
     /// Creates the same number of cuts on each axis.
     /// </summary>
     /// <returns>The fratured cubes.</returns>
     /// <param name="cube">Cube that will be fractured</param>
     /// <param name="velocity">The speed which the fractures are moving.</param>
     /// <param name="cuts">The amount of fractures per axis there will be.</param>
     public static GameObject[] Fracture(GameObject cube, Vector3 velocity, int cuts){
         GameObject[] fracts = new GameObject[cuts * cuts * cuts];
 
         // Get parent information
         Rigidbody parent = cube.GetComponent<Rigidbody> ();
         Vector3 scale = cube.transform.localScale;
 
         // Get information used by all fractures.
         Vector3 fScale = new Vector3 (scale.x / cuts, scale.y / cuts, scale.z / cuts);
         Vector3 shift = new Vector3 ((fScale.x - scale.x) / 2 , (fScale.y - scale.y) / 2, (fScale.z - scale.z) / 2);
         Vector3 angle = cube.transform.eulerAngles;
 
         // Create  and configure base
         GameObject baseFracture = GameObject.CreatePrimitive (PrimitiveType.Cube);
         baseFracture.transform.localScale = fScale;
         baseFracture.transform.position = cube.transform.position + shift;
         baseFracture.GetComponent<Renderer> ().material = cube.GetComponent<Renderer> ().material;
         baseFracture.GetComponent<BoxCollider> ().material = cube.GetComponent <BoxCollider> ().material;
 
         // Configure physics.
         Rigidbody body = baseFracture.AddComponent<Rigidbody> ();
         body.mass = parent.mass / fracts.Length;
 
         int i = 0;
         for (int x = 0; x < cuts; x++) {
             for(int y = 0; y < cuts; y++){
                 for(int z = 0; z < cuts; z++){
                     // Create fracture and move it to the right position.
                     GameObject obj = Instantiate(baseFracture);
                     obj.transform.position += new Vector3(fScale.x * x, fScale.y * y, fScale.z * z);
 
                     // Rotate
                     obj.transform.RotateAround(cube.transform.position, Vector3.right, angle.x);
                     obj.transform.RotateAround(cube.transform.position, Vector3.up, angle.y);
                     obj.transform.RotateAround(cube.transform.position, Vector3.forward, angle.z);
 
                     // Apply physics
                     obj.GetComponent<Rigidbody> ().velocity = velocity;
 
                     // Add it to the array
                     fracts[i++] = obj;
                 }
             }
         }
 
         // Destroy the base and the initial cube.
         Destroy (baseFracture);
         Destroy (cube);
 
         // Give back all the fractures.
         return fracts;
     }
 
 }


If you need any comprehension, let me know. So my problem is these lines of code here.

 obj.transform.RotateAround(cube.transform.position, Vector3.right, angle.x);
 obj.transform.RotateAround(cube.transform.position, Vector3.up, angle.y);
 obj.transform.RotateAround(cube.transform.position, Vector3.forward, angle.z);


I expected them to rotate all the fragments around the center to look like the original, which it does until I start combining rotations. Such as having the Y at 45 degrees and the Z at 45 degrees for some reason rotates the X at 45 degrees. I could test all the possibilities and see what happens then offset the axis which are wrong, but I feel like there is a better way to go about this.

Any help would be greatly appreciated :)

Update I almost have a solution. I changed the Z axis from Vector.forward to

 new Vector3 (Mathf.Sin (Mathf.Deg2Rad * angle.y), -Mathf.Sin (Mathf.Deg2Rad * angle.x), Mathf.Cos (Mathf.Deg2Rad * angle.x) * Mathf.Cos (Mathf.Deg2Rad * angle.y));

This works great when doing two axis at once, like XZ and YZ. But when combining all X, Y and Z rotation it moves slightly. So either I have a completely wrong solution or a math error :).

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Apr 12, 2015 at 08:32 AM

If you just want the rotation of the fragments to match the rotation of the original cube, replace those three lines with:

 obj.transform.rotation = cube.transform.rotation;
Comment
Add comment · Show 1 · 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 xChris6041x · Apr 12, 2015 at 01:53 PM 0
Share

Sorry, I should have been more specific. I need the fragments to be rotated around the center of the cube I am fragmenting so all the fragments together look like the original until it falls apart.

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

2 People are following this question.

avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotate Around by 90 Degrees 1 Answer

Rotate around moves object out of position, why? 1 Answer

How would I go About making a flying player move forward, but also Around another object? 0 Answers

Distribute terrain in zones 3 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