- Home /
Question regarding Transforms
Any good sources for undersatnding transforms? How do I get things to go diagonal, towrds a position?
Thanks
Answer by TheDemiurge · Oct 29, 2010 at 11:46 AM
The transform does two things: First, it lets a game object keep track of its position and rotation (both in relation to a parent object, and to the world), and its scale.
Second, it lets the object change and manipulate those values, in multiple ways.
The transform's position
member is a Vector3
type, which means it's got x, y and z member variables. For example if you have an object located at (0, 10, 0), this means when you access transform.position
, its members (x, y and z) will equal to 0, 10 and 0, respectively. For the rotation, rather than accessing transform.rotation
you'd access transform.eulerAngles
, because rotation is a Quaternion which is a whole other demon (a good, yet often misunderstood demon), and for that you'd never modify rotation
or localRotation
yourself. If another object was then attached to this one and centered to it, its transform.localPosition
would be (0, 0, 0), and its transform.localEulerAngles
would be (0,0,0) because it's not moved or rotated relative to its parent object, only to the game world. If you then move this child object 10 units upward, its local position would show (0, 10, 0) and its position would show (0, 20, 0). And so forth.
To move the object itself you use the Translate
function, and to rotate it you use Rotate
.
When you pull up the Transform
class in the scripting manual, you'll see a few examples of movement & rotation there. By default these two functions do their thing in relation to the local axes. What this means is that the easiest way to move forward is to use transform.Translate(Vector3.forward);
where Vector3.forward
is shorthand for (0, 0, 1). With rotations, you just need to remember that the rotation is done around the axis you specify. So transform.Rotate(Vector3.right)
will make the object spin forward.
Figuring out a specific way to move or rotate an object is often a question of math. It takes a basic understanding of vector math, and of some other things you can get from the Vector3 class. For example, when you add two vectors together you get a vector pointing somewhere between the two and at a length that depends on the two. Take a look at the following code.
var vec1: Vector3 = Vector3(0, 0, 1); // forward vector var vec2: Vector3 = Vector3(1, 0, 0); // right vector var newVec: Vector3 = vec1 + vec2; // (1, 0, 1)
function Update() { transform.Translate(newVec * Time.deltaTime); // move forward and to the right }
We took two vectors whose length was 1 and added them together. This creates a new vector angled 45 degrees to both and with a length equal to the square root of 2 (Pythagoras). If you then translate an object by this vector you'd get movement almost 1.5 times as fast as if you were using one of the other two. To solve this use something like var newVec: Vector3 = (vec1 + vec2).normalized;
. This brings the vector back down to a length of 1.
If for example vec1 was (0, 0, 2), then adding vec1 and vec2 together would yield a vector pointing at (1, 0, 2) with a length of the square root of 5, which is angled 63.44 degrees to vec1 and 26.56 degrees to vec2.
Best way to grasp it better is to just read up on the other members of Vector3, Quaternion and Transform and experiment.
Thank you very much for your advice and information. Greatly Appreciated. I will go about that and experiment with Vector3, Quaternion and Transform. Thank you.