Unity Cookbook. Transform class
Transform class
Transform
- Script Reference: Transform
- Manual: Transform
- Important classes: Transform
- The GameObject’s position, rotation and scale can be manipulated via its Transform component
- either by using the included methods, or by directly manipulating properties
- Properties are either global or local: e.g.,
positionvslocalPosition - Local coordinates are with respect to the GameObject’s parent
Translation
- Translation properties (can be directly changed):
transform.positiontransform.localPosition
- Translation (movement) methods:
transform.Translate()float moveSpeed = 3.0f; if(Input.GetKey(KeyCode.UpArrow)) transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.DownArrow)) transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);- Note: Translating happens by default according to GameObject’s local coordinate system
Rotation
- Manual: Rotation and orientation in Unity
- The rotation of a transform visible in the inspector is shown as Euler angles
- Secretly, Unity uses Quaternions to store the rotation of a transform
- Both are available in code
- Rotation properties:
transform.rotation, global rotation as a Quaterniontransform.localRotation, local rotation as a Quaterniontransform.eulerAngles, global rotation as Euler angles (Vector3)transform.localEulerAngles, local rotation as Euler angles (Vector3)
- Rotation methods:
transform.RotateAround()transform.LookAt()-
transform.Rotate()(multiple variants available)if(Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.RightArrow)) transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
Scale
- There are no methods for changing scale.
- Scale properties:
transform.localScaletransform.lossyScale- Read-only!
- Script Reference: lossyScale
Local axes
transform.up,transform.rightandtransform.forwardrefer to the local axes of the GameObject: green, red and blue, respectively- Global versions are
Vector3.up,Vector3.rightandVector3.forward
- Global versions are
- You can use them to e.g., rotate the GameObject around these axes:
if (Input.GetKey(KeyCode.LeftAlt)) transform.RotateAround( transform.position, // rotates along the GameObject's origin point... transform.right, // around the GameObject's red axis... 180 * Time.deltaTime // with a rate of 180 degrees per second. );
Exercise 1.
Make a child GameObject rotate around its parent.
Extra: How to rotate something by x degrees
- In a mishmash of vectors, rotations and transforms, it can be difficult to see what to do when trying to apply rotations!
- Let’s say, we want to rotate something around the global $z$ axis by $30\degree$.
- First, create a new rotation with
Quaternion.Euler:Quaternion newRotation = Quaternion.Euler(0, 0, 30); - Then, we use it to rotate whatever we need to rotate…
- To rotate a Vector3 by a Quaternion:
Vector3 newVector = newRotation * oldVector; - To rotate a Quaternion by a Quaternion:
Quaternion newQuaternion = newRotation * oldQuaternion;- We can use this technique to rotate a Transform by a Quaternion:
transform.rotation = newRotation * transform.rotation;
- We can use this technique to rotate a Transform by a Quaternion:
- Note: We do not need a Quaternion to rotate a transform. We can just apply the rotation with
transform.Rotate(0, 0, 30);
Extra: How to look instantly towards a point (3D)
- What if we don’t know how many degrees we want to rotate something by, but instead want to rotate something so that it points to a given direction (vector)?
- To rotate to a direction given by another Transform, use the previously mentioned Transform.LookAt method:
transform.LookAt(otherGameObject.transform); - To rotate to a direction given by another vector, use Quaternion.LookRotation:
transform.rotation = Quaternion.LookRotation(vector);
There’s also Quaternion.FromToRotation for rotating a vector to align another vector
- E.g., here’s how to sets the the transform’s $y$-axis (transform.up) to go along the directionVector
transform.rotation = Quaternion.FromToRotation(transform.up, directionVector);
Extra: How to look slowly towards a point (3D)
To slowly rotate towards a target, there are two methods available:
- If you need to manipulate position vectors: Vector3.RotateTowards
- If you need to manipulate rotation quaternions: Quaternion.RotateTowards
Extra: How to instantly look towards a point (2D)
- The methods described in the previous slide do not (directly) work in 2D
- Even
transform.LookAt(Transform target)does not work, because it aligns thetransform.forwardaxis towards the target, which is the local $z$ axis - the one that points UP in 2D games! - Instead, we want to align the
transform.rightaxis towards the target:transform.right = target.transform.position - transform.position;(or if you want to use angles:)
float angle = Mathf.Atan2(target.transform.position.y, target.transform.position.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
How to slowly look towards a point (2D)
- uhhhh xx todo
Changing many values at once
- You can select multiple GameObjects at once to edit their transforms values at once
- You can even set their position, rotation or scale to be a range of values by setting the field to
L(lower, upper)- Other neat ways exist as well.
R(lower, upper)sets the values to a random range, and you can multiply all values at once with+=value
- Other neat ways exist as well.
- See other numeric field expressions in the Unity docs