- Home /
pretty weird issue with rotating a sphere
Hey everyone, i've stumbled upon a pretty weird issue when rotating a sphere.
Even though the sphere has absolutely neither parents nor children, it seems to be rotating around something...
here's the rotation code :
transform.Rotate(touchDeltaPosition.y * speeed,-touchDeltaPosition.x * speeed, 0,Space.World);
Also, i've already tried to attach a rigidbody and freeze x,y and z positions, no success
I've got exact same issue. In my case, it's on an object co$$anonymous$$g from a FBX. Space.world
or Space.self
makes no difference on its rotation whatsoever! On the editor, if I toy around with rotating on inspector, the center is clearly dislocated. If I use editor's rotation (`E` on keyboard), then it's the only place it rotates around itself. Couldn't find any kind of combination it would work on script, including making empty parents or rigidbodies. ( http://answers.unity3d.com/questions/8599/how-do-i-rotate-and-object-around-its-center-in-sc.html )
You generally have no way of knowing where your pivot is supposed to be. However, since we're talking about a sphere, the intended pivot is obviously in the center (assu$$anonymous$$g the scale factors are uniform). So you could use renderer.bounds.center (which is probably what the Editor is doing, if you select "Center" ins$$anonymous$$d of "Pivot" in the upper left)
Oh, wait, I was referring to the original question - @Cawas, please don't continue old questions with your own question. But if you want to achieve the Editor 'E' effect, use my suggestion.
@wolfram sorry about the confusion, but I'm not adding "my own" question here. It is (probably) the exact same issue, and I was hoping Eric would confirm it. Otherwise, it may be time to open another question to be "forever" unanswered (I've got many of those). I've tried your suggestion as I hinted on the link. It doesn't work. I even tried using Instantiate, to create a new object from the imported one, and that works, but I can't make the instantiated object to begin in the right position and I can't find that original position anywhere! That should tell us this is all really messed up and sounds to me like Unity bugs. - oh wait, I read it wrong as well! You said renderer
not collider
. I'll try it right now, but I don't think it will help given the case.
RotateAroundLocal is an internal and/or deprecated function you shouldn't use anyway. renderer.bounds.center does have a problem if your object consists of several objects/children, since it olny gives the center of the renderer you are accessing, not recursively. You'd hvae to write a function collecting bounds.$$anonymous$$/max recursively of your subhierarchy and find the center yourself. Using the parent to shift the pivot in the opposite direction always works, for any internal or external model, no matter where their original pivot is. Unless you're not setting it up correctly. Read the various answers explaining it.
Answer by Kacer · Jun 30, 2011 at 08:03 AM
Have you tried using space.self, instead of space.world?
though im not sure it would help.
If that doesnt work you can try the following; spawn a new gameobject, then move said gameobject to the center of your sphere, set the sphere as a child of the gameobject, then apply the rotation to the gameobject instead.
If that doesnt work, then im out of ideas.
just out of curiosity, where is the pivot of the sphere located?
if you mean the XYZ rotation bows with pivot, they are in the sphere's middle, i've already tried to change the space line, that didnt help either...
how do i get a gameobject exactly to my sphere's center?
set the gameobject as a child of the sphere, then move the gameobject to 0,0,0.
Or you can move both objects to world origin.
this didn't work, for some weird reason the sphere (its not a generated one from unity) has it's center way too low which causes this issue...
is there a way to recalculate the center of a gameobject?
Ohhh, now it makes more snese, what program did you use to make the sphere? (you could have told me to begin with, that you werent using the sphere from unity :P)
you just need to realign the pivot in your chosen 3D application, just move the pivot to the center of the sphere and it should be fixed.
@$$anonymous$$acer any way to do it only with scripts, without going to the "3D application"? There should be some way.
Answer by cregox · Jun 05, 2012 at 12:15 AM
Wolfram did offer a nice solution, mostly already given elsewhere: use renderer.bounds.center
along with `RotateAround`:
transform.RotateAround(renderer.bounds.center, new Vector3(1, -1, 0), speeed); // <-- why 3 e's speed?
The only problem in this solution is that we can't set the rotation to a specific value.
Here's another Wolfram's idea that will use the same renderer.bounds.center
but with a parenting GameObject
instead:
GameObject myContainer = new GameObject();
myContainer.name = "rot_" + transform.gameObject.name;
myContainer.transform.position = renderer.bounds.center;
myContainer.transform.parent = transform.parent;
transform.parent = myContainer.transform;
// now rotate myContainer at your will
myContainer.transform.Rotate(touchDeltaPosition.y * speeed,-touchDeltaPosition.x * speeed, 0,Space.World);
Your answer
Follow this Question
Related Questions
How not to make a RigidBody not go into ground when tilted foward? 1 Answer
Orbit cam and Rigidbodies aiming 0 Answers
Unity Script causing crash 1 Answer
Rigidbody.MoveRotation() Towards an object? 1 Answer
Errors with gravity switching... 1 Answer