Add transform.SetPose or a Pose argument to SetPositionAndRotation

There is a **Pose **struct that holds a position and rotation. Very cool and useful, but it would be nice to have some helper functions in the Transform Component to use that

We can now do

transform.SetPositionAndRotation(pose.position,pose.rotation);
transform.SetLocalPositionAndRotation(pose.position, pose.rotation);

you could add an overload

transform.SetPositionAndRotation(pose);
transform.SetLocalPositionAndRotation(pose);

or a new function

transform.SetPose(pose);
transform.SetLocalPose(pose);

EXAMPLE
it would be nice to have also a pose getter/setter or GetPose/GetLocalPose function to do something like this

var startPose = transform.pose; //or transform.GetPose()

//after some time, restore the initial position and rotation
transform.SetPose(startPose);

//or with a setter
transform.pose = startPose

i hope to get some feedback from the community and someone of the unity team

1 Like

It’s a struct so it’s not likely to save you much if any time. Passing a Pose is still going to jam both those args on the stack, even if they’re just one Pose arg, as it’s obviously not passing it by reference.

Yeah, the code is slightly cleaner, but not sure it would be worth the extra overloading noise.

Hi kurt, thanks for the quick reply!
What do you mean by overloading noise?

(my intention was just to reduce the amount of code required, not to optimize performance, maybe the “optimization” tag is misleading)

Edit: i’m currently using extension methods to achieve this but i don’t know if it’s slower

public static class ExtensionMethods
{
    public static void SetPose(this Transform t, Pose pose)
    {
        t.SetPositionAndRotation(pose.position, pose.rotation);
    }
 
    public static void SetLocalPose(this Transform t, Pose pose)
    {
        t.SetLocalPositionAndRotation(pose.position, pose.rotation);
    }
 
    public static Pose GetPose(this Transform t)
    {
        return new Pose(t.position, t.rotation);
    }
 
    public static Pose GetLocalPose(this Transform t, Pose pose)
    {
        return new Pose(t.localPosition, t.localRotation);
    }
}

Just more flavors of the same thing, that’s all. Like Physics.Raycast() is horribly overloaded, but that’s probably just because it grew organically over time, at least I’m guessing.

My limited understanding is that these are just syntactic sugar: there’s no extra call involved, it just replaces the code in place and compiles it.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

1 Like