1
\$\begingroup\$

I want to rotate my game object around a point in the space instead of the origin, so how can I do it ?

\$\endgroup\$
1
  • \$\begingroup\$ While you already have answers, I highly recommend fleshing out your question more. Can you provide links or images of what you are trying to accomplish? Not only will a better question provide better answers, it's more likely to help others that come to GD.SE with similar issues. \$\endgroup\$ Commented Nov 2, 2016 at 15:52

4 Answers 4

1
\$\begingroup\$

Create a new cube object that has the object you want to rotate as a child (or set your object's parent to this object with this.parent = object) and its point of origin as the point you want to rotate around. Any details on parents can be found on the Unity Scripting page. Essentially do this:cube+sphere example

Now rotating the cube will rotate your object around the middle point of your cube. Of course, you don't want this cube to be seen so remember to remove the mesh renderer. When you're done, disconnect the sphere from the cube (this.parent=null) then remove the cube that you added (name it something like RotationCube so that you can easily find it or maybe from a tag).

Mesh renderer disabled example In this test "turnturn" script has the line and as the invisible cube turns, the sphere turns around its origin:

Update(){
transform.Rotate(Vector3.left * Time.deltaTime*15);
}
\$\endgroup\$
1
\$\begingroup\$

Your question is very short and I don't know if I'm getting it correctly, but you should check RotateAround and then do something like:

transform.RotateAround(targetTransform.position, Vector3.up, speed * Time.deltaTime);
\$\endgroup\$
3
  • \$\begingroup\$ I want to change the pivot point of the object so that I can rotate the object around it instead of its center. I tried what you said but it didn't work properly. \$\endgroup\$
    – ABE_Mark45
    Commented Nov 1, 2016 at 19:05
  • \$\begingroup\$ yourObject.transform.RotateAround(targetPointTransform.position, yourObject.transform.up, speed * Time.DeltaTime running this in the update will rotate your object (yourObject) aournd the target point, using the "up" vector from your object. \$\endgroup\$
    – Daniel
    Commented Nov 1, 2016 at 19:15
  • \$\begingroup\$ Unluckily, it didn't work. What happens is that my GameObject goes to the specified point and rotates around its center. \$\endgroup\$
    – ABE_Mark45
    Commented Nov 2, 2016 at 11:27
0
\$\begingroup\$

The basic theory is to translate your object to the world origin, perform your rotation as usual, then translate it back to the same position it was before.

\$\endgroup\$
0
\$\begingroup\$

Make your object the child of an empty GameObject that is located at your desired center of rotation. Then, rotate the new object instead of your existing (visible) object.

You can get fancy and do this in script, but this approach maintains the scene graph and keeps everything nice and editor-friendly.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .