x


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

more ▼

asked Jun 30 '11 at 07:37 AM

biohazard gravatar image

biohazard
288 36 42 46

I've got exact same issue. In my case, it's on an object coming 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 )

Jun 04 '12 at 09:10 PM Cawas

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 (assuming the scale factors are uniform). So you could use renderer.bounds.center (which is probably what the Editor is doing, if you select "Center" instead of "Pivot" in the upper left)

Jun 04 '12 at 09:23 PM Wolfram

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.

Jun 04 '12 at 09:24 PM Wolfram

@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.

Jun 04 '12 at 09:43 PM Cawas

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.min/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.

Jun 05 '12 at 12:23 AM Wolfram
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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?

more ▼

answered Jun 30 '11 at 08:03 AM

Kacer gravatar image

Kacer
705 15 18 31

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?

Jun 30 '11 at 08:34 AM biohazard

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.

Jun 30 '11 at 08:43 AM Kacer

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?

Jun 30 '11 at 09:29 AM biohazard

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.

Jun 30 '11 at 09:36 AM Kacer

@Kacer any way to do it only with scripts, without going to the "3D application"? There should be some way.

Jun 04 '12 at 09:12 PM Cawas
(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Jun 05 '12 at 12:15 AM

Cawas gravatar image

Cawas
1.5k 31 38 54

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3892
x3419
x2246
x1948
x1864

asked: Jun 30 '11 at 07:37 AM

Seen: 2412 times

Last Updated: Jun 11 '12 at 07:56 PM