Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question GameObjects not rotating around axis, physics.

Discussion in 'Scripting' started by larswik, Oct 11, 2022.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Hi, getting back in to unity after about 5 years. I scoured the web for the solution first but came up empty. I have an Asteroid that is made out of 8 separate parts. All the 8 parts have Rigidbody and I use the AddExplosionForce. It explodes fine but I want the parts to rotate using a Vector3. But the separate rock fragments are not rotating on their own axis, The axis they are using is offset so it looks like it is orbiting like the moon around the earth. I thought I could reset the axis but no luck. Goal is to have a ship shoot the asteroid and it explode in to its smaller spinning bits. Any ideas on fixing this is much appreciated. I included a photo too, that that it shows any movement. I should also note, I have IsTrigger check box turned on. If I turn it off, the object explodes by itself, but when it explodes by itself, the fragments spin correctly.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RockExplodeScript : MonoBehaviour
    6. {
    7.     public GameObject AxisParent;
    8.     float timer = 0;
    9.     bool hasExploded = false;
    10.  
    11.  
    12.     // Update is called once per frame
    13.  
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.         timer += Time.deltaTime;
    18.         if (timer > 1 && hasExploded == false)
    19.         {
    20.             hasExploded = true;
    21.             ExplodeRock();
    22.         }
    23.     }
    24.  
    25.     private void ExplodeRock()
    26.     {
    27.         foreach(Transform t in gameObject.transform)
    28.         {
    29.             var _rb = t.GetComponent<Rigidbody>();
    30.             if(_rb != null)
    31.             {
    32.                 print("Name: " + t.name);
    33.                 _rb.AddExplosionForce(30f, AxisParent.transform.position, 10f );
    34.                 _rb.ResetCenterOfMass();
    35.                 _rb.AddTorque(new Vector3(2f, 3f, 5f), ForceMode.Impulse);
    36.             }
    37.          
    38.         }
    39.     }
    40. }
    41.  
     

    Attached Files:

    Last edited: Oct 11, 2022
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    This would be due to the center of mass on each Rigidbody not aligning visually with the mesh's center of mass. Make sure the fragments' pivots are correct and that the visual elements of the fragments match up with the colliders as well. Use gizmos to draw the center of mass if you want to see where it is.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    Well, first of all, do not parent rigidbodies inside other rigidbodies. At least don't have them active at the same time. Rigidbodies are generally simulated in worldspace. However if they are still a child of another rigidbody, the movement and rotation of that parent would also affect the child objects.

    So you should unparent those objects when you blow it up. Unity even provides the Transform.DetachChildren method for such situations. Manually unparenting does also work. Though if you want to do it manually, be careful with loops over the children of the parent as you unparent them, the number of children changes.
     
    PraetorBlue likes this.
  4. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    So these 3D objects each have there own fragments that have different pivot points. As I dug more in to it I noticed that I can switch from center to pivot and that showed me the point they rotate around (pivot) is offset from the Center. Doing research it doesn't seem possible to shift the pivot point to the center. But is there ea way to rotate around the center, and not the pivot point of a GameObject?
     
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I figured out the correct approach. I found the 3D assets and took them in to Cinema 4D. I adjusted each fragments axis to be the center and exported out a new fbx file. after re linking textures, creating new colliders it works. Thanks for your help. Also how do you switch from question to resolved?