Making an object rotate around itself

When I rotate an object using the rotation icon in the editor it rotates around itself, but when I try to do it with a script it appears to be rotating around a different axis (not necessarily around itself)… How can I fix it?

using UnityEngine;
using System.Collections;

public class SimpleRotate : MonoBehaviour {
    void Update () {
        transform.Rotate (Vector3.right * 50 * Time.deltaTime, Space.World);

    }
}

Have you tried rotating it in the Up axis?

 transform.Rotate (Vector3.up * 50 * Time.deltaTime, Space.Self);
4 Likes

That just changes the direction of rotation, but not the axis of rotation.

public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);
Description
Applies a rotation of zAngle degrees around the z axis, xAngle degrees around the x axis, and yAngle degrees around the y axis (in that order).

Changing Space.World to Space.Self didn’t make a different

there only 3, with values are wrong ? how should it rotate how is it rotating, look in your inspector for the values.

I just tried out a little script, a cylinder rotating itself around his middle point in y axis.

public class Rotate : MonoBehaviour {

    void Update()
    {
        transform.Rotate( new Vector3(0, 0.3f, 0) );
    }
}
1 Like

Guys, I think you all misunderstood me, and I finally understood the core of my problem. Since my objects’ points of rotation are not in the middle, unless I do a funny workaround it’s best just to re-export it in a 3D modeling program with the axis in the middle. That ought to solve my problem.

Hey guys. Does anyone knows how to rotate an object just once? Cause i have not idea and im desperate.

An easier solution would be creating an empty object and place the object that you want to rotate inside it

1 Like

Yes. This is exactly how I do it.

1 Like

TLDR: If you have a 3d model on a parent empty game object, make sure the child’s position is 0, 0, 0.

After having this same problem, I figured it out.

My original “player” game object, consisting of a 3d model childed to an empty game object and with similar code to the above, was rotating around a random point for no particular reason
I deleted and rebuilt my player game object with an empty and a simple cube shape. I have the setting to place objects at the world origin when I create them in the hierarchy, so both the empty and 3d object’s positions were the same.

When I reimported my player graphic and parented it to the empty, I noticed it (the child’s) position was not Zero’d, however that was not obvious when just clicking the empty parent game object and viewing it in the inspector.

Thus, make sure the child game object’s position is 0, 0, 0.

2 Likes

Best answer for this … thanks mate !!

This is exactly what I came to figure out. Once I zero’d all the children’s position coordinates, placed the parent object, moved the children in relation to the parent, then it spun around a center anchor point. Thank you so much!

If you add a BoxCollider to the empty object it would tell you exactly where is its pivot, this really helped me to visualize where to put the children objects in relation to their parent.

That’s bad idea considering that’s an active component that has behaviour and is part of a build i.e. a hammer to scratch and itch.

Just use the built-in custom icons in the Inspector window (See: “Assign custom icons to GameObjects, scripts, and Prefabs”). Adding in a small circular coloured dot should suffice?

Note: Please bear in mind that this thread is very, very old so please try to avoid necroing posts.

Thanks.