- Home /
What's the function to replace the obsolete RotateAroundLocal ?
Hi, I've actually gotten this script to work. It's basically for rotating an object (a bird in my case) around a center of rotation while also rotating the bird locally so that its feet will always be below its head (or the world space rotation along the X axis will always be around 0 as I am rotating it around the z axis globally). Like I said, this script actually works. But I use a deprecated RotateAroundLocal in this script's Update function. The Update snippet is as follows:
void Update () {
transform.RotateAround(centerOfRotation, Vector3.forward, angularSpeed * Time.deltaTime);
if (birdPosition == eBirdPositionY.TOP && transform.position.y < centerOfRotation.y + rotateThreshold)
{
isRotate = true;
}
else if (birdPosition == eBirdPositionY.BOTTOM && transform.position.y > centerOfRotation.y - rotateThreshold)
{
isRotate = true;
}
if (isRotate)
{
if ( birdOrientation == eBirdOrientation.IN && transform.localRotation.eulerAngles.y < 180.0f && transform.localRotation.eulerAngles.y > 90.0f )
{
isRotate = false;
birdOrientation = eBirdOrientation.OUT;
Vector3 eulerAngles = transform.localRotation.eulerAngles;
transform.localRotation = Quaternion.Euler(eulerAngles.x, 90.0f, 0.0f); //eulerAngles.z);
if (birdPosition == eBirdPositionY.TOP)
{
birdPosition = eBirdPositionY.BOTTOM;
}
else
{
birdPosition = eBirdPositionY.TOP;
}
}
else if ( birdOrientation == eBirdOrientation.OUT && transform.localRotation.eulerAngles.y > 180.0f && transform.localRotation.eulerAngles.y > 270.0f )
{
isRotate = false;
birdOrientation = eBirdOrientation.IN;
Vector3 eulerAngles = transform.localRotation.eulerAngles;
transform.localRotation = Quaternion.Euler(eulerAngles.x, 270.0f, 0.0f);// eulerAngles.z);
if (birdPosition == eBirdPositionY.TOP)
{
birdPosition = eBirdPositionY.BOTTOM;
}
else
{
birdPosition = eBirdPositionY.TOP;
}
}
else
{
transform.RotateAroundLocal(Vector3.up, 10.0f * Time.deltaTime); // <<--- THIS ONE!
//transform.RotateAround(transform.position, Vector3.up, 10.0f * Time.deltaTime);
//transform.Rotate(transform.up, 10.0f * Time.deltaTime, Space.Self);
//transform.Rotate(Vector3.up, 100.0f * Time.deltaTime, Space.Self);
}
}
}
As you can see, I use this line to rotate smoothly over time ( based on the delta time ): "transform.RotateAroundLocal(Vector3.up, 10.0f * Time.deltaTime); // <<--- THIS ONE!". And all the rest of the lines that I've commented are those that failed to produce anything similar to the RotateAroundLocal one.
So what I'm trying to ask is, what is the correct way to replace the obsolete RotateAroundLocal? Thanks in advance.
The last one should spin the bird around it's own center, like a weather vane (Space.self isn't needed. And why *100 when the rest are *10?) That isn't what you want?
Is the bird itself moving while it spins (like the earth?)
Eh, right. Didn't notice the 100 value. But then again, it also didn't work. And you are correct. The bird orbits and moves around a center (like the earth's revolution) and it itself rotates around its up vector on certain angle. But unlike the earth, the rotation and revolution angles are perpendicular.
Answer by Owen-Reynolds · Nov 26, 2013 at 06:29 PM
I think RotateAroundLocal was retired since the new version, Rotate, is much easier to use.
A rotation has an axis, which can be local or world, and a center point. The "AroundLocal" in RotateAroundLocal means the center point is your local 000. That's redundant, and thus confusing, since everyone knows the "local 000" rule from using spin tools. So it was trimmed to just Rotate
.
So now it's Rotate
(normal) and RotateAround
(orbit something.)
The old RotateAroundLocal (which is really just a simple rotate) takes radians as spin-amount, and takes only a world axis for rotation (again, confusing, since Local is in the name.) The new Rotate
takes degrees for spin-amount and allows you to pick local/world axis for the spin. Default is local axis (because, look at the orbit tool in Unity -- most of the time most people use it in Local.)
So (tested), RotateAroundLocal(Vector3.up, spinAmt);
is the same as Rotate(Vector3.up, spinAmt*Mathf.Rad2Deg, Space.World);
The spin amount seems funny (Rad2Deg?) but if you aren't translating from the old RAL, you'll never need it. Say you want 1/2-spin every second. SpinAmt will be 180*Time.deltaTime
.
Thanks a lot. It did work. However I don't really understand why we use Space.World ins$$anonymous$$d of Space.Self. AFAI$$anonymous$$, the world space's Vector3.up is always (0, 1 , 0) whereas the local space's Vector3.up (or the transform.up vector) isn't always (0, 1, 0) depending on the rotation of the object (and its parent) itself, right? So from what I can understand from the answer above, the rotation axis is always (0, 1, 0) because we use Space.World. In my $$anonymous$$d it should've been wrong because when I make the bird orbit its center, its up vector != the world's up vector due to the orbit rotation. I'm confused. :/
Anyway, checking the answer as the correct one. Thanks a lot. :)
Well, yes, a better version of the code above would probably have the last 55 lines replaced by a single local Rotate (or maybe a LookAt? I still can't visualize what the orbit looks like.)
The existing RotateAroundLocal is using world Y. Why was that working? Reading and setting individual eulerAngles doesn't work properly (which is noted in the docs.) So, all those if's look like a kludge, where each section of the orbit has different code. I assume the RAL command is in a section where world y is close enough.
I see. So it's probably because of my "setting the localRotation" code. However I needed it to clamp the rotation to be exactly either 90 degree or 270 degree after it is rotating. How do you suggest I clamp the angle without setting the localRotation directly then?
Directly setting rotation like you are doing (or localRotation, but that only matters if you have a rotated parent) is fine, and using Quat.Euler() is fine. Reading from eulerAngles.x is the dicey part. It's like 12:45 vs. Quarter to One.
I can't visualize the goal of the code (feet always world down? feet always movement-down like a rocket?) If you're doing more of this stuff, you might just describe the end result in a new Q and ask if there's a simple solution.
Yes, you are right. The goal is to make the bird's feet always world down. Anyway, since it's actually working now, I think the answer is sufficient for now. Except if there would be a performance issue because of that code, I would probably be asking it anyway. Thank you again.
Your answer
Follow this Question
Related Questions
Rotate object in the direction where it is going 1 Answer
rotate object around another object 1:1 1 Answer
Smooth Camera Rotation relative to World [SOLVED] 1 Answer
instant rotate an object 3 Answers
rotation script in unity 3 Answers