Transform (position and motion)

Transform

Transform is the one component that every GameObject in Unity has. As such, you can directly access the transform component of the GameObject directly without using "GetComponent<>()" like you do with every other component.


Common commands:

  • transform.position - read where your gameobject is (global game world units) or set your GameObject's position.
    • Example: transform.position += Vector3.right; // adds one to the x coordinate in a single frame/step
    • Example: if (transform.position.y < -2f) // check if the GameObject ever falls below some level, then do something
    • Example: transform.position = (some tweening function) // using Lerp, Slerp, Dotween or something similar you could manually tell an object to travel back and forth
  • transform.rotation - rotations are a big topic. They are actually stored as four complex (imaginary) numbers called a "Quaternion". Instead of trying to ever manually do this yourself, use the premade rotation functions that Unity provides and trust in the efficiency of their algorithms.
    • Example: transform.eulerAngles - use this to read or set the values of Euler Angles (0-360 degrees). Don't increment this to rotate an object, but you could for example go from 0 degrees to 180 degrees to flip an object.
      • transform.eulerAngles = new Vector3(10f, 30f, 0);
    • Example: transform.Rotate(Vector3.forward*length) - pass in a Vector3 which essentially does two things: (1) The direction is the axis to rotate around (2) The length/magnitude of the vector is the speed or amount to rotate.
    • Example: transform.RotateAround(Vector3 point, Vector3 axis, float degrees) - This could be used for something like a moon rotating around a point, or in general anything that you want to rotate around an axis other than its own center/position point.
  • Vector3.Lerp(Vector3 start, Vector3 end, float percent) - this is a useful function that returns a point between the two passed in points, that is a percent of the way from the start to the end. In this way, you can just increment your float "percent" variable and animate something going back and forth from one point to another
    • Example: transform.position = Vector3.Lerp(start, finish, percent); // have this in the Update() method and then increment the percent variable (percent must be between 0.0 and 1.0 which is 0% and 100%.
  • transform.right - the "forward" direction of 2D GameObjects. This can be used in a lot of really useful ways.
    • Example: transform.right = target - transform.position; // This subtracts two Vector3's (end-start) to get a direction to point in. Then it sets your "right" or "forward" direction to face that way. This is a really easy way to have a 2D object face a target. You can use a Lerp or Slerp to smooth out the rotation so there is a bit of lagging glide to it if you want.
    • Example: transform.position += transform.right * speed; // Moves the 2D GameObject in the "forward" direction it is facing. This would be really useful in a top down 2D game, like driving a car.
  • Time.deltaTime - this is the fraction of a second interval that has passed since the last time the Update() method was called. This is used to regulate motion over a varying framerate and keep the speed constant. In other words, if only a little time has passed, only travel a little forward. If a lot of time has passed, travel further. This keeps the y=mx+b linear because a small step forward goes a small step up, while a large step forward goes a large step up.
    • Example: transform.position += Vector3.right * speed * Time.deltaTime;



Beyond this, there are A TON of other useful commands. Here is a quick gyst of some:

  • transform.parent - read or set the parent of your game object. This could be used to for a player to land on a platform and have him move with the platform, or begin to rotate/travel with a planet. Example, "transform.parent = null" removes your GameObject from being a child of another.
  • transform.childCount - count how many children GameObjects there are. Really useful for monitoring/counting things quickly like number of enemies or bullets.
  • transform.forward - same uses as "transform.right" mentioned above, except transform.forward is used in 3D and by default is the positive Z axis.
  • (all of methods that convert local to world space and vice versa)
  • gameObject - This is the same as a "this" command in Java. It is a way to refer to the GameObject the script is attached to. Notice the LOWERCASE first letter.
  • transform.Translate(Vector3 move) - this can be used just like "transform.position += Vector move" except can handle local position as a child better and a little more efficiently.


One big warning: If you want to have accurate collision detection, do not manually move the object using the Transform. Instead, use the similar commands that Rigidbody2D provide such as "rbody.Move(Vector3 position)". This moves the object the same as "transform.position += Vector3.right" except it calculates correctly any collisions, as collisions are all handled by the Rigidbody2D class.

08.0 Transform (App and Game Design)

Useful Example Code Snippets


Move at a Constant Speed

  • Example of how to move a GameObject at a constant speed (in game units per second).


Random Numbers

  • Example of how to select a random float between two values


Falling and Respawning 1

  • Example of an object always moving down at "speed" game units per second. When the y-value gets too low (-4f) then it resets/respawns to the top (5f). I also added that it respawns at a random x-position.


Falling and Respawning 2

  • Example of how to use public variables for the y-cutoff value and the Vector3 position to respawn to. Notice I am not using the transform to move the object here. The object is moving elsewhere based on the Rigidbody's physics.


Rotating

  • Example of how to rotate an object at a constant speed, in degrees per second.


Back and Forth Motion

  • Example of how to use a Vector3.Lerp() and a sine curve to smoothly go back and forth between two points.


Aim at the Mouse Cursor 1

  • Example of how to aim the "right" direction of your character at another GameObject. This could be used to aim a turret/gun or have your character face someone/something. This is the longer, inefficient way to do this. Look at the next block to see a more elegant and efficient version.


Aim at the Mouse Cursor 2 (Better)

  • More efficient example of the same. Notice these differences:
    • Create the camera variable once and set it to "Camera.main" once. This means we only use GameObject.Find() and GetComponent<>() once rather than every single frame. This is WAY more efficient.
    • Create the direction vector only once, and then just update its value each frame.


Growing and Shrinking

  • There is no transform.scale, instead there is transform.localScale. You can read all about why movement and rotation cannot provide an exact global scale, and thus there is a global transform.lossyScale ... but I digress.
  • transform.localScale defaults to (1,1,1) or "Vector3.one".
  • This handles stretching and squishing each dimension (z-dimension doesn't matter in 2D game)

Simple example of setting the scale to half of the x-dimension width


Example of pulsing the uniform scale (ratio of x- and y- stays the same) to grow and shrink the image using a smooth sine curve.

Better example of the previous, but with more control.

This is only the start ...

You can become ever more clever, efficient, and elegant with your coding. Here is a simple example of how to use Lerp to smoothly approach a target or have a springy bounce.

Note: In the code shown, the green "x" would represent "transform.position"