- Home /
How Rotate Object To Resemble Spinning?
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;
function Update() {
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.
Answer by hellcats · May 01, 2011 at 03:39 AM
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.
Yeah, I'd advice using RotateAround. Or Parenting your wheel to an empty gameObject which is set to the center of your wheel and just rotate ths ins$$anonymous$$d of your wheel with the script you already have.
Thanks for the response. I read up on RotateAround but, after applying it - it did not do what I wanted. I tried parenting the "wheel" to an empty game object but, that didnt do it either. I can get the wheel to spin around empty space but, not in a waterwheel like fashion. I even went back into the 3D and reset my "pivot" point to the center but, that also did not work. I ended up just animating it in 3DS $$anonymous$$ax and reimporting it into Unity.
Perhaps, I don't fully understand rotate, rotation, and/or RotateAround. I used this RotateAround code based on the Unity resource guide:
var vox = 0;
var voy = 0;
var voz = 0;
var vax = 0;
var vay = 1;
var vaz = 0;
function Update() {
transform.RotateAround(Vector3(vox, voy, voz), Vector3(vax, vay, vaz), 20 * Time.deltaTime); } I broke the Vector3's into variables so I could test Vector3.up. forward, left, right, etc... on the fly but, like I stated earlier, I couldnt get it to do what I wanted.
I too am having same problems as GameCherry. Even when resetting the pivot point in 3ds max, the problem of finding the origin/pivot point is still wrong in unity. I also tried using a empty game object as the parent to fix the problem, but no luck. So anybody else care to shed some light on this please.