Copy a transform

So I was under the impression that a Transform was just another struct like a Vector and would behave like any other variable. I just discovered that this isn’t the case.
I think part of my confusion is that I’m expecting a transform to be the same as a matrix which doesn’t seem to be the case.

eg. (Javascript)

var newTransform = player.transform;
newTransform.position.x += 1.0f;

The above code alters the players position which would indicate that the “=” operator is not a regular copy operator for Transforms but instead copies a reference or pointer.

This is confusing and inconsistent with all other types.

That aside, how do I copy a transform?
or if a transform contains other data besides position, rotation and scale, is there a simple way to copy a transform into a matrix without doing it bit by bit so I can do matrix math on it without modifying the original transform it came from?

A Transform is a Component. A component is a Monobehaviour. a Monobehaviour is an Object, not a struct. If Transform was a struct, then this script would not work:

var target : Transform;
var distance : float = 10.0;
function Update()
   if(Vector3.distance(this.transform.position, target.position) < distance)
        Debug.Log("Close to target!");

Because target would be a copy of the input transform when the script is initialized, it would not move with the original because it is different from the original. If this script did not work, Unity would be quite a useless engine.

if you want to create a transform that does nothing except it copies the values of an original transform, then you should create an empty game object, and set it’s transform’s values = to the original’s.

Or if you want to create a copy of the transform, and every bit of the GameObject it is attached to, then you can just do this:

 Instantiate(targetTransform, targetTransform.position, targetTransform.rotation);

Actually, the = operation on the first line is exactly how all objects work in Javascript (and UnityScript, and C#). In all these languages, variables have reference semantics. (In Javascript, this is kinda sorta true for even primitive types like int, given immutability, but that’s a rather more subtle point). Things like Vector3, which are structs and therefore value types, are the exception rather than the rule.

The second line is actually the weird one, because it invokes a sort of UnityScript hack to allow modifying one element of a struct property. The same code does not compile in C#.

If you have two Transforms (that is, two game objects) and you want to set the second’s transformation to be the same as the first’s, it is sufficient to copy localPosition, localRotation, and localScale. Of course, if the two Transforms have different parents, this will put them in different places in the world.

The full transformation of a Transform (from local to world space) as a Matrix44 is accessible via the localToWorldMatrix variable. If you want just the local transformation matrix (from local to parent-local space) multiply the parent’s worldToLocalMatrix by the child’s localToWorldMatrix.

That’s not really an idiomatic way to work with transformations in Unity, though. Transform and Quaternion have plenty of utility methods to help you do things in a semantically meaningful fashion, rather than mucking around with basis vectors. It’s worth getting to know them, rather than working against them.

I got around this by writing my own Transform class called Trans. You can create a GameObject, but why take the performance slowdown of instantiation?

using UnityEngine;

public class Trans

	public Vector3 position;
	public Quaternion rotation;
	public Vector3 localScale;

	public Trans (Vector3 newPosition, Quaternion newRotation, Vector3 newLocalScale)
		position = newPosition;
		rotation = newRotation;
		localScale = newLocalScale;

	public Trans ()
		position =;
		rotation = Quaternion.identity;
		localScale =;

	public Trans (Transform transform)
		copyFrom (transform);

	public void copyFrom (Transform transform)
		position = transform.position;
		rotation = transform.rotation;
		localScale = transform.localScale;

	public void copyTo (Transform transform)
		transform.position = position;
		transform.rotation = rotation;
		transform.localScale = localScale;


Hi where is the robot soldies ? I’ve search here but I did found it

Hi I wonder how to make a man whose holding the gun and be useful in the unityl ,ike this Locomotion System | Animation Tools | Unity Asset Store