- Home /
Issue with an object rotating strangely
So I've created a script that creates a forest, with that each tree is procedurally generated though only being slightly different (the variances could be slightly taller by having one or two middle log bits,change in rotation and of course change in position). For this example I have an AI controller cube that walks up to trees and cuts them down, then cuts them into logs.
At the moment everything with that works fine, however the tree itself being made up of three types of 3D models (trunk, middle and top) when the tree the cut down, the middle logs can be cut further into it's smaller parts. Which can be cut further into planks.
However the issue revolves around the first middle log that being the one closest to the trunk, when it separates from it's child object, it starts to randomly rotate to be standing up right. I made a Gif of the problem:
[link text][https://imgur.com/lhXszZ7]
The remaining children objects are completely fine, they don't have the issue that the first log did, I'm not doing anything to specifically cause the object to do this weird rotation.
Just for reference how the tree is made, there is an empty game object being the parent of the whole tree, then followed by the trunk and so on. I.E. like this
-EmptyGameObject
-child of empty Game object:Trunk
-child of Trunk: Log1
-child of Log1: Log2
-child of Log2: TreeTop
The game objects are very straightforward, they include a Mesh renderer, mesh collider. The rigid body is added when the object is cut down (to all children).
I've tried searching up anything related to this bug but I can't seem to find anything similar to it.
[1]: https://imgur.com/lhXszZ7
Sorry about the horrible setup, I'm still learning the whole markdown thing. I'll try my best to edit it until it looks nice enough to read.
Also if someone needs to see the code I use, I can provide snippets it just depends on the specific bits since there is a couple scripts.
Answer by FlaSh-G · Oct 30, 2017 at 10:01 PM
Seeing that gif, I can tell that the chunk has an incorrect center of mass.
The center of mass is a Vector3 that defines the relative center of mass of a rigidbody. A thing that is made of a heavier material on one side than the other will have its center of mass moved towards that side. Rigidbody.centerOfMass allows you to implement that in your game.
In your case, the center of mass seems to be outside the physical shape of the object, causing it to rotate around its edge, and eventually to stand up.
Your primary course of action should be to find out why the center of mass is set inappropriately. If you should then happen to find out that there's not much you can do about it, consider using Rigidbody.ResetCenterOfMass after cutting to fix it.
Thanks! I will definitely try it out, but it seems like it's the proper answer to this annoying problem, I think it maybe because the objects are parented that it sees all the objects as part of one object. Again thank you. I guess any update to the rigid body seemed to reset it but yes definitely finding the real reason behind it all helps a lot.
Answer by f03n1x · Oct 30, 2017 at 09:54 PM
So I've figured out a way to get around this issue, any modification to the rigid body at all causes it to act normally. So in this case I changed the angular drag to 1.0f.
It isn't the most Ideal answer but it gets the job done.
If anyone has any better ideas please tell me, I'd rather do a proper way or at least try to understand what the underlying problem is here.