atrivedi
atrivedi

Reputation: 358

Rotate an object around its center point in unity 3d

How to rotate a 3d game object around its center point in unity 3d.

Upvotes: 1

Views: 22442

Answers (3)

Goularou
Goularou

Reputation: 188

Just use bounds.center from the renderer

Vector3 position = myGameObject.GetComponent<Renderer>().bounds.center;

myGameObject.transform.RotateAround(position, rotationVector, degreesPerSecond * Time.deltaTime);

where rotationVector is your rotation axis (Vector3)

Upvotes: 5

Leonardo Maia
Leonardo Maia

Reputation: 21

The two common ways to rotate an object are

  1. The rotate attribute in the transform. Using this one you can set the exact coordinates for the target object rotation. However, you'll have to manage the smoothness by yourself if you want to make animations and the values are given through Quaternion type. I recommend to use the static method Quaternion.Euler so you can pass values in a X, Y, Z. The example below set the object to turn 90 degrees clockwise in the Y axis:

    transform.rotation = Quaternion.Euler (0, 90, 0);
    
  2. The second approach uses the Rotation method in the same transform attribute. This method allow you to pass the amount of degrees in which object will rotate and already accept X, Y and Z coordinates instead Quaternion. The example below rotate the object 1 degree clockwise in the Y axis:

    transform.Rotate (0, 1, 0);
    

To best understand the difference between both methods, if you use the first one in an Update method you'll see the object static rotated 90 degrees in the Y axis. The second example used in an Update will make the object spin clockwise in the Y axis (too fast).

Upvotes: 2

ShayanK
ShayanK

Reputation: 1263

use

Transform.rotation

look here for Examples/Documentation : Unity Script Reference

Upvotes: -1

Related Questions