How to Rotate a GameObject around its center, from script?

Penjimon

After countless searches, I don't understand how this is possible, in the Unity editor window, this is very easy.

How do you Rotate an object, that exists at any point in the world, where it perfectly rotates around it's own center point?

I don't want to add a "parent" gameobject, and I don't want to necessarily rely on the object's MeshRenderer. (due to mismatching naming conventions)

Here is the latest code I have tried. (I had the most success with this)

public float deg = 0;
public GameObject go;
public Vector3 center;
public Rigidbody rb;

void Start ()
{
    deg = 0;
    go = this.gameObject;

    rb = go.GetComponent<Rigidbody>();
    center = go.GetComponent<BoxCollider>().center;
}

private void FixedUpdate()
{
    deg += 1f;

    if (deg >= 360f) deg = 0;


    Vector3 cen = go.transform.position; // + center;

    go.transform.RotateAround(cen, Vector3.up, deg);


}

The behavior I'm seeing is that either it rotates around the corner of the object, or it rotates around the center, but moves the object all over the screen. In the code revision above, it rotates around the corner.

Ruzihm

The problem is that center = go.GetComponent<BoxCollider>().center; gives you the center's position in local space (the center's position relative to go's position and rotation), but if you're using RotateAround, the first argument needs to be in global space (the center's position relative to the origin point with no rotation).

So, to fix the problem, we need to convert from local space to global space. We can use Transform.TransformPoint to do this:

public float deg = 2f;
public GameObject go;
public Vector3 cenLocal;
public Rigidbody rb;

void Start ()
{
    go = this.gameObject;

    rb = go.GetComponent<Rigidbody>();
    cenLocal = go.GetComponent<BoxCollider>().center; // Center (local space)
}


private void FixedUpdate()
{    
    Vector3 cenGlobal = go.transform.TransformPoint(cenLocal); // Center (global space)

    go.transform.RotateAround(cenGlobal , Vector3.up, deg);
}

As an aside, you may already know this, but if go == this.gameObject always, you can just use transform.xyz instead of go.transform.xyz. Simply using transform is the more conventional way of referring to this.gameObject.transform.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to scale a shape from its center as origin?

分類Dev

How to rotate a Line or an image from its bottom to 180 degree

分類Dev

How to rotate a Sprite around the circle in LibGDX?

分類Dev

How to rotate a table from MySql

分類Dev

I want to rotate image which is generated from its thumbnail

分類Dev

Run a script on a gameobject once

分類Dev

How to rotate a TextView in a ConstraintLayout along with its constraint bounds

分類Dev

How to run a script from a mounted Google drive. The issue is that the google drive folder has a space in its name

分類Dev

Revolving nested element around a center

分類Dev

How do I rotate a Quaternion with 2nd Quaternion on its local or world axes without using transform.Rotate?

分類Dev

rotate imagebutton around itself without moving it

分類Dev

Clone/duplicate an existing GameObject and its children

分類Dev

How to stop an image returning to its starting point using CSS3 rotate3d

分類Dev

How to remove Media Center from Windows 8

分類Dev

Rotating a Svg path around center using matrix

分類Dev

R ggmap center around 180 degrees longitude

分類Dev

Center image around user clicked position

分類Dev

How to keep rotating GameObject on gazing?

分類Dev

Three.JS Rotate Camera around object by moving the device

分類Dev

How to center an absolutely positioned element within its parent when the child element's height is unknown

分類Dev

How do I get my canvas image to rotate 90 degrees each time, its only rotating 180 degrees

分類Dev

How do i tell java its closer to go 350 -> 355 -> 360 -> 05 degrees instead of all the way around

分類Dev

How to identify the terminal from a script?

分類Dev

How to rotate in R?

分類Dev

How to rotate desktop background?

分類Dev

Google 3D Earth How to find the center point from all the markers and set center view

分類Dev

How to center images in gif using convert from imagamagick?

分類Dev

Rotate a bitmap using render script android

分類Dev

Rotate affinity values in a batch script for loop

Related 関連記事

  1. 1

    How to scale a shape from its center as origin?

  2. 2

    How to rotate a Line or an image from its bottom to 180 degree

  3. 3

    How to rotate a Sprite around the circle in LibGDX?

  4. 4

    How to rotate a table from MySql

  5. 5

    I want to rotate image which is generated from its thumbnail

  6. 6

    Run a script on a gameobject once

  7. 7

    How to rotate a TextView in a ConstraintLayout along with its constraint bounds

  8. 8

    How to run a script from a mounted Google drive. The issue is that the google drive folder has a space in its name

  9. 9

    Revolving nested element around a center

  10. 10

    How do I rotate a Quaternion with 2nd Quaternion on its local or world axes without using transform.Rotate?

  11. 11

    rotate imagebutton around itself without moving it

  12. 12

    Clone/duplicate an existing GameObject and its children

  13. 13

    How to stop an image returning to its starting point using CSS3 rotate3d

  14. 14

    How to remove Media Center from Windows 8

  15. 15

    Rotating a Svg path around center using matrix

  16. 16

    R ggmap center around 180 degrees longitude

  17. 17

    Center image around user clicked position

  18. 18

    How to keep rotating GameObject on gazing?

  19. 19

    Three.JS Rotate Camera around object by moving the device

  20. 20

    How to center an absolutely positioned element within its parent when the child element's height is unknown

  21. 21

    How do I get my canvas image to rotate 90 degrees each time, its only rotating 180 degrees

  22. 22

    How do i tell java its closer to go 350 -> 355 -> 360 -> 05 degrees instead of all the way around

  23. 23

    How to identify the terminal from a script?

  24. 24

    How to rotate in R?

  25. 25

    How to rotate desktop background?

  26. 26

    Google 3D Earth How to find the center point from all the markers and set center view

  27. 27

    How to center images in gif using convert from imagamagick?

  28. 28

    Rotate a bitmap using render script android

  29. 29

    Rotate affinity values in a batch script for loop

ホットタグ

アーカイブ