x


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?

alt text

Thank you for looking.

more ▼

asked Apr 30 '11 at 10:56 PM

GameCherry gravatar image

GameCherry
3 8 8 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 01 '11 at 03:39 AM

hellcats gravatar image

hellcats
651 8 12 23

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 instead of your wheel with the script you already have.

May 01 '11 at 05:24 AM Joshua

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 Max and reimporting it into Unity.

May 01 '11 at 06:28 PM GameCherry

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.

May 01 '11 at 06:29 PM GameCherry

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.

May 15 '11 at 09:48 PM Volatile
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2249
x747
x602
x63
x35

asked: Apr 30 '11 at 10:56 PM

Seen: 3301 times

Last Updated: Apr 30 '11 at 10:56 PM