When you are moving around a game object, does anyone know if the transform vectors are updated every time rotation or local rotation is, or if the property calculates the rotated vector every time you access it?

If I need to call the global forward vector numerous times per update without changing the rotation in between, I’d rather store transform.forward in a separate Vector3 rather than having Unity calculate transform.rotation*Vector3.forward every time I call transform.forward.

I don’t have Unity Pro, otherwise I’d benchmark it with the profiler myself . . .

I know I can always just take the precaution anyways, but it’d be nice to know for reference anyways. Thanks in advance!

So what happens on the managed side is quite clear and not that much calculations. However we don’t know what happens in “INTERNAL_get_rotation” since it’s a native code function. It might request an update of all parents to actually calculate the quaternion. We simply don’t know.

So in general if you use one of these properties multiple times inside the same function, store it in a local variable. As you already said that of course makes only sense if the value doesn’t change between the different uses inside the function.

Although I am presuming some design decisions about Unity, this is what it should be like.

transform.forward is the same as doing transform.TransformDirection(Vector3.foward), which is the same as doing transform.rotation*Vector3.forward

Doing transform.eulerAngles = angles is the same as doing transform.rotation.Set(Quaternion.Euler(angles))

Any global properties such as position and rotation are defined as so:

function get rotation() : Quaternion {
if (parent) {
return localRotation * parent.rotation;
}
return localRotation;
}
function get position() : Vector3 {
if (parent) {
return parent.TransformPoint(localPosition);
}
return localPosition;
}
function TransformPoint() : Vector3 {
return rotation * Vector3.Scale(lossyScale, position);
}

I know that I’m not 100% right, but the general idea stands. Unless you are accessing local spacial information, you are recursively calculating it. Every time.

Hope this helps,
Benproductions1

PS: I’m going to keep this as a community Wiki so people can fix my code