Hello All. I'm trying to make my water-wheel spin counter-clockwise without animation. I've tried several variants of transform.rotate but all without success.
var rvar = 100;
var pvar = 100;
transform.Rotate(Vector3.forward * Time.deltaTime * rvar);
transform.position = (Vector3(1, 1, 0) / Time.deltaTime / pvar);
The wheel will rotate but, around some invisible point/axis that brings it around half the screen as opposed to staying stationary and just spinning.
I included a transform.position to counter this but, it does not work correctly.
I've included this jpeg to better illustrate my problem.
As I manually rotate the wheel around the Z rotation - the X and Y position moves in tandem, which is good because it keeps the wheel stationary....How do I code this?
Thank you for looking.
In-game transform-based rotation is always around the pivot point of the object (as opposed to the "center"). The pivot point is just the origin of the local coordinate system of the object, while the "center" is the geometric average of all the vertices of the mesh on the object (note that the center is not actually stored but is computed by Unity as needed). Unity lets you rotate objects using the center, but it does this by translating the center to the origin, rotating, then translating back. You can see this in the editor by watching the position coordinates while you rotate a mesh that isn't aligned with the pivot point. In game however, Unity won't do this extra translation for you, which causes the problems you are seeing.
There are two ways to fix this. The first is to change the model so that the mesh is built around the origin you want to rotate around. The other is to use Transform.RotateAround(Vector3 origin, Vector3 axis, float angle) which does the magic that the editor does to rotate around the center.