简体   繁体   中英

In unity script, how to “rotate” and “rotate to” around pivot position

in unity editor, when i enable "Pivot", gameobject will rotate around "pivot point" position, when i enable "Center", gameobject will rotate around "center point"

but if i use script to rotate, it always rotate around "center point", for ex, here is my scene:

在此输入图像描述

I use following code:

    void Start()
    {
//        transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
        transform.RotateAround(transform.position, new Vector3(0, 0, 1), 90);
    }

it rotate around "center point"

在此输入图像描述

if object has collider, i can get pivot point with colider.bounds, if not, how should i do?

and even worse, in some case, i hope rotate to like set rotate in unity editor inspector, i use following code:

transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90))

I have no idea to adjust above code to rotate around pivot point

update

I think if i can "rotate", i can also impl "rotate to", so the key point is, how should i get "pivot point" that is used in RotateAround, to object has collider, I can get with colider.bounds with transform.position, to no collider object, how should i do?

The pivot point is the-same as the transform.position you're already using. If you don't like where the pivot point is or the location the rotation is rotating against, you have two options:

1 . Create new empty GameObject. Move it to the position you want your GameObject to rotate around. Once satisfied by that location, make this new GameObject a child of the GameObject you will be rotating around. Use the position of this new GameObject as the pivot rotation point for the transform.RotateAround function.

Drag the empty GameObject to the customPivot slot in the script below. That should give you a new pivot point to rotate your GameObject around.

public Transform customPivot;

void Update()
{
    transform.RotateAround(customPivot.position, Vector3.up, 20 * Time.deltaTime);
}

2 . Open a 3D application and change the pivot point of your object, then save and re-import it into Unity.

Since this is just used to rotate object around, I suggest #1 . If the problem is that the pivot point is not centered, I would suggest #2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM