clone a Transform object at runtime

how do i create a Transform object at runtime for temporary position & rotation calculation. i don't need a full blown game object. what i'd like to do something like this pseudocode:

function GenericTransformation(source:Transform):Transform {
   var result:Transform = source.clone();
   result.position += Vector3.up * hotspotDistance;
   return result;
}

I'm pretty sure you can't make one without the other. GameObjects and Transforms are sort of inseparable in Unity.

Just create a new GameObject (it will automatically have a Transform - no need to add one) and use its transform to do whatever you want, then destroy the whole thing afterwards - or keep it around if you want to re-use the transform as a helper in future operations. Any GameObjects that are created at runtime will be removed when you stop the movie anyway.

There's nothing much that's really "full blown" about a gameobject. If you create a new one from code, it has no collider, mesh renderer or anything like that, so it's actually a pretty lightweight and invisible object.

One question that arises in my head is why you'd need a temporary Transform object. Are you using all data provided by a Transform object? Are you just using the position? If possible, you might want to cut down the dependency to only a Vector3 for position depending on your needs.

While I don't know the code depending on this method, if possible, would changing approach make things easier for you? In case you need rotation and scale also, you can create your own struct type that combines two Vector3s and a Quaternion.

If you're providing the transform to code you don't have the source to or don't want to mess around in, then I suppose making a "locator object" is a feasible solution. I don't mind making a game object for "locators" or "anchor points". If you will be instansiating alot of these, then I'd suggest you parent the temporary locators with a "locator root" object to avoid cuttering down the heirarchy view. Another approach would be to flag them invisible.

Clone Solution - Put this class in project

public struct TransformData
{
	public Vector3 position;
	public Quaternion rotation;
	
	public Vector3 localPosition;
	public Vector3 localScale;
	public Quaternion localRotation;
	
	public Transform parent;
}

public static class TransformUtils {
	
	public static TransformData Clone( this Transform transform )
	{
		TransformData td = new TransformData();
		
		td.position = transform.position;
		td.localPosition = transform.localPosition;
		
		td.rotation = transform.rotation;
		td.localRotation = transform.localRotation;
		
		td.localScale = transform.localScale;
		
		td.parent = transform.parent;
		
		return td;
	}
	
}

Example:

public class testeScript : MonoBehaviour {

        TransformData td;

	// Use this for initialization
	void Start () {
		td = transform.Clone();
	}
	
	// Update is called once per frame
	void Update () {
                // Returns transform to start position
		if( Input.GetKeyDown(KeyCode.Space))
                {
                    transform.position = td.position;
                }
	}
}