- Home /
Rotate around obsolete - missing Rotate overloads???
On Unity 2019.3 I get the comment to replace transform.rotatearound with transform.rotate, but I don't think any of the 6 overloads allow me to do the following: Rotate object 1 around object 2 by an angle on the Y-axis only. How would you re-write the following using transform.Rotate?
obj1.transform.RotateAround(obj2.transform.position, Vector3.up, 90);
I'm starting to get pretty annoyed with the amount of stuff unity is making obsolete for no seemingly good reason. What was so bad about rotate around?
Answer by Bunny83 · Feb 11, 2020 at 02:19 AM
Well, RotateAround is not black magic. You can simply create your own method like this:
public static class TransformExt
{
public static void RotateAroundPoint(this Transform aTrans, Vector3 aPoint, Vector3 aAxis, float aAngle)
{
var dir = aTrans.position - aPoint;
var q = Quaternion.AngleAxis(aAngle, aAxis) ;
aTrans.position = aPoint + q * dir;
aTrans.rotation = q * aTrans.rotation;
}
}
Put that class somewhere in your project and you can use it just like RotateAround. So if you have a Transform called obj1 you can do
obj1.RotateAroundPoint(obj2.transform.position, Vector3.up, 90);
However keep in mind that any kind of relative rotation of the worldspace position of an object can result in a slight drift over time. I wouldn't recommend using such an approach. Based on your comment you seem to have a really strange approach. Why is your camera / obj2 a child of obj1 if you want to move / rotate obj1 as if it's a child of obj2? Such a strange relationship will cause additional issues with jitter and precision. If you do a single instant 90° rotation it's probably not that of an issue. However it's still a really strange approach.
Since you talk in abstract terms like obj1, obj2 and we don't know the relative orientation of the objects nor the purpose of the rotation we can't really suggest suitable alternatives. We don't play the guessing game here.
ps: I just had a look at the 2019.3 decompiled code and the RotateAround overload you use does not have the Obsolete attribute attached. Only the overloads which do not take a reference point and therefore do the same as Rotate. They essentially just renamed the RotateAround overloads which take an axis and an angle to Rotate. The RotateAround with 3 parameters should still work and is still documented. Here's the method definition in the current reference code and here's the one that has been deprecated
Answer by Tripleganger · Feb 10, 2020 at 04:23 PM
@gp1234567 When Unity makes something obsolete is generally because there is already a function (or another mean) that does the job for you. In this case, you can child the object you want to rotate to the object you want to be the pivot point, and then rotate the parent object.
If you don't want your parent object to rotate, then make an empty object and repeat.
In any case, RotateAround has been obsolete since... 2013? It does not look like they will replace it any time soon.
Thanks for the reply Tripleganger. Sadly, obj2 is the main vr camera, which I CANNOT rotate. It's is, however, already a child of obj1. So the only way to actually rotate object 2, is in fact to rotate obj1 around it, which is what I'm using this call for.
Somehow, creating extra objects, parenting them, then rotating them, then unparenting them and, possibly, having to destroy them again, seems a lot of work to replace what is in fact just an already tested and existing calculation? Any better options?
No worries. It is a bit extra, but I believe it is worth it.
I have been working on this code. Although it is mostly hard coded, the only thing wrong with it is that it is not smooth. It makes an object interpolate around the position of another object, but as already said, you might need to work on it and make it smooth. There are enough resources online for you to implement it, I have been working on it for more than I could, sorry for this "half-answer".