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.