I’m trying to calculate the rotation point of an object but can’t figure it out.
The object is a domino shape, so it should be easy. I want to be able to rotate around one corner of the shape but I can’t work out how to calculate the rotation point in code. The rotation then needs to be around the z-axis.
I’ve noticed that the RotateAround method has been deprecated. Although I’ve seen posts suggesting that the same can now be achieved with the Rotate method I’ve yet to see a definitive answer to it. There doesn’t seem to be an overload that takes a ‘point’ - the position around which to rotate around.
First, there are two versions of the RotationAround() function. Only the two parameter version is depreciated. The three parameter version is not depreciated:
As for figure out the corner, there are a number of choices. The best will depend on your needs and how the object is authored. Three I see reference most often:
Place an empty game object at the corner and make it a child of the object. This position of this object will always be the corner’s position in world space.
Use the mesh.bounds to calculate the local corner and use Transform.TransformPoint() to convert this position into world space.
Calculate the local position of the corner…the position when the domino has a scale of (1,1,1), rotation of (0,0,0), and position of (0,0,0)…and convert this into world coordinates using Transform.TransformPoint().
If you are always going to be rotating it around the same corner, consider putting an empty game object at that corner, making the visible game object a child, then putting the rotation code on the empty parent. Or alternately change the mesh so that the pivot point is at that corner…either in a 3D authoring program or using an editor script. With the pivot point at the corner, you can use Rotate() on the parent or any of the other many ways to rotate your object. You are not ‘stuck’ using RotateAround.