I've been following this tutorial http://stormtek.geek.nz/rts_tutorial/part10.php and I came across transform.forward. I have no idea what this is supposed to mean. I looked it up in the documentation and i get http://docs.unity3d.com/ScriptReference/Transform-forward.html and it still confuses me. what exactly does transform.forward.x, transform.forward.y, transform.forward.z return?

Hi There, Yes, the documentation may be quite confusing at times, especially when you are starting-up with Scripting, so I will try to explain this to the best that I can. For a given object, you can have a transform. This transform will store information on the position, scale, or rotation of your object. Sometimes, it is also useful to obtain the direction of your object (to see where its heading or to perform forward movement) and Transform.forward will provide you with this, as it is a vector that points forward for the object selected. As you may already know, each vector has a x,y, and z coordinate in space. So, if you were to look at the x,y and z coordinates of this vector, this can be obtained using transform.forward.x, transform.forward.y, and transform.forward.z. In the example provided on the page you mentioned, transform.forward is used to compute the angle between the direction of the object and another object; however, it can be used for more basic things such as moving your character forward based on time, using ,for example, transform.translate (See: http://docs.unity3d.com/ScriptReference/Transform.Translate.html). On this page, the example shows the following code Transform.Translate(Vector3.forward * Time.deltaTime); Which means that the object will translate (or move linearly) forward based on time. I hope this helps; If you need to know more about vectors, I have prepared a tutorial here that should get you started fast with these concepts. If this is still unclear, just give me a shout. I'm happy to help. Best of Luck!

okay i kind of get it, so transform.forward is the vector that points "forward" of the object from the object itself. What does the magnitude of this vector represent?

The magnitude is the size or length of the vector. For example if you think about velocity/speed. The velocity vector give you the direction of the object, while the magnitude, usually referred as speed, will gibe you the value. So for example, the car can be heading west (direction), with a speed of 20 kilometers per hour (magnitude). The same applies to forces, a force has a direction, but also a magnitude (value). So you can exert a force of magnitude xx in a particular direction. You may also have herad about scalars and vectors. So the magnitude is a scalar, it has no direction; while the vector has a direction. Both can provide important information for the movement of an object. This is an excerpt from my blog that should summarize it all: "So far, even if you are not used to vectors, you may already have used variables such as float, int, or boolean to define a quantity. If you were, let’s say, observing a moving object, determining the value of the speed would be great using a float (for example 2 meters per second); however, this quantity, often referred as a magnitude, would not provide any information on the direction of the object (where we it is going). For example, an object could be going a 2 meters per second towards the positive or negative x-axis. So while having the magnitude is useful, knowing the direction is even more useful to determine where this object is going. When we add the magnitude of the speed to its direction, we obtain the velocity, a vector representation that includes both information on the movement of an object: its speed and direction." Hope this helps

I mean... I understand that transform.forward is a vector now and this vector points in the same direction as the "front" of an object in the scene. However, this vector should have a magnitude right? I am wondering what that magnitude represents. For example, if an object is facing in the z-direction transform.forward.x=0 transform.forward.y=0 transform.forward.z="some number" what would this magnitude (some number in this case) represent? EDIT: Nevermind, i found the answer to this by creating a script that has variables "public float forwardx" "public float forwardy" "public float forwardz" and then under update(), writing "foward.x=transform.forward.x" "foward.y=transform.forward.y" "foward.z=transform.forward.z" For people that want to know what I found... forward.x, y, z ranges from 0-1 where if an object faces one axis, the transform.forward of that corresponding axis would be 1 and every other axis would be 0. so, in the above example, transform.forward.z would b 1. if an object is facing about 45 degrees in between x and z... forward.x, and forward.z are around 0.7071068, 0.7071068 (for some reason, the half way point of a rotation is not 0.5, but 0.707... which i find weird.) EDIT2: Whoops, i feel dumb for not recognizing it... 45 degrees is sin(45) which is 0.707 if i were to change rotation.y then forward.z changes by cos(rotation.y) and forward.x changes by sin(rotation.y) if i were to change rotation.x then forward.z changes by cos(rotation.x) and forward.y changes by sin(-rotation.x) if i change the rotation of both x AND y, i have no idea whats happening, but its probably does some kind of trig math that i learned at school but forgot.

The magnitude is always 1. It has no meaning; the purpose of transform.forward (as well as .right, etc.) is to give you a direction. But Unity has to return a vector with some magnitude; if it were length 0, then it couldn't represent a direction. So, they simply return a vector of length 1. And yes, trig is your friend if you want to understand vectors better.

It's a unit vector. Unit vectors are a special case of vectors where the magnitude is simply 1 unit, like joestrout said. A magnitude of 1 is convenient for directions so that you can multiply by a speed value or whatever.

I still don't understand exactly what vector Transform.forward returns. You say it returns the direction of an object, but how does a transform even know this? Is the transform object saving information on where it was in the previous frame, and using this to determine the direction its heading in? I don't see how a Transform object could know it's own direction without caching history. I also don't understand the connection between transform.forward and the z axis (blue) axis that the Unity manual references. What is special about the z axis (over the y and x axis) that contains information about the "direction"? Thanks for making the tutorial.

That's what a Transform is: it represents the position, rotation, and scale of an object. That is literally its entire job. It's just little object that stores a position (Vector3), a scale (Vector3), and a rotation (Quaternion). No, history and frames have nothing to do with it. A Transform represents where the object is right now. It's just a matter of definition. The local blue (Z) axis is what Unity chose to call "forward." And so, since we're using Unity, we all call it "forward" as well. They could have called it "zeeblefrub" and then we would instead be talking about the zeeblefrub direction. But most people find "forward" to be more clear.

Ok starting to get it now. So if I placed a new game object on the scene, it's faces (0,0,1) by default and as it rotates during the game, one can use transform.forward to get the direction its facing in, which is entirely based on the current rotation. I guess my next question is how does one calculate the transform.forward based off a given rotation quaternion? Or where can I read up on this? Thanks

Ok, but do you know the math behind a quarternion times the vector (0,0,1) or where can I read more on this? The wikipedia/wolfram alpha for quarternion is pretty dense, so If you have any recommended readings that would be very helpful

Why do you care? Unity's done it for you so you don't have to. If you really do want/need to understand quaternion math, Wikipedia and Wolfram Alpha are good places to go. You'll just have to wade through it. But the whole point of encapsulation (a key concept in programming) is hiding the details, so you don't have to worry about them.

Should be obvious....I want to have a thorough understanding of the fundamentals. In my opinion, if you don't understand quarternion math at least at a high level then you don't really get how Transform.forward returns the direction vector. Yes, obviously encapsulation is useful, but this seems like a key concept in movement and rotation so I will read up more on it.

In this case we are talking about Vector3 from (0,0,0) in world space, or from my object local coordinates yet?

@RedHaro, @26sramirez6, here are the fundamentals. Every GameObject has a Transform. We will only consider 3D here, so it's a plain old Transform. The Transform has THREE key elements: a Vector3 .position, a Quaternion .rotation, and an optional Transform .parent. Just about everything else is just window dressing to make it easier to work with these three things. So the Vector3 position is pretty obvious to figure out that it is considered the position in 3D space of the object. But we know we will not be working with atom-sized objects, but instead whole models which take up some bulky 3D space, right? Which means its orientation is important. One part of the orientation puzzle is that Quaternion .rotation, but we will get back to that later. For now, let's JUST focus on the model of an ogre or spaceship or poker card which you just imported in from another program. I'll go with the ogre, please. In the 3D modeling program called Blender, they have set up certain conventions, or arbitrary choices which users will tend to agree with, although everyone also agrees they're arbitrary and you can overcome them with mental energy. In Blender, vectors like (0, 0, +1) are "upward" and a grid is drawn in the X/Y plane to reinforce that convention. The same thing goes with Unity, but they chose a different set of arbitrary conventions. In Unity, (0, 0, +1) is "forward", not upward. In Unity, (0, +1, 0) is "upward" instead. And since Unity works in a left-hand coordinate system, then (+1, 0, 0) must be perpendicular to the other two in the "rightward" direction. A grid is drawn in the X/Z plane to reinforce that mental idea that +Y is up. Before we tackle Transform's similar members, the Vector3 class in Unity has some helper methods/properties to get useful constant vectors: * Vector3.zero is (0, 0, 0) * Vector3.one is (1, 1, 1) * Vector3.right is (1, 0, 0) * Vector3.up is (0, 1, 0) * Vector3.forward is (0, 0, 1) * Vector3.left is (-1, 0, 0) * Vector3.down is (0, -1, 0) * Vector3.back is (0, 0, -1) They just added all these for your convenience, and you can use them for anything but they reinforce their conventions pretty strongly with those names. They could have called them "north" or "blue" but they didn't. With those conventions, most Unity users work hard to ensure that their models will be rotated appropriately in the import process, so that your ogre's nose is facing in the +Z direction while standing with the feet near (0, 0, 0) and the head somewhere in the +Y direction, depending on how bad the ogre's hunchback has been bent. Now back to Transform.rotation. This is a Quaternion, which you may understand from that name, represents a rotation in 3D space. It's a hypercomplex number (xi+yj+zk+w), but we won't get into the details. What you need to know is that a Vector3 multiplied by a Quaternion will rotate the vector in 3D space by a specific amount in a specific arc. As a convenience, Transform simply adds similar methods/properties based on the Vector3 standards. * Transform.forward has been calculated for you as Vector3.forward * Transform.rotation. * Transform.up has been calculated for you as Vector3.up * Transform.rotation. * Transform.right has been calculated for you as Vector3.right * Transform.rotation. That's all there is to Transform.forward. It's a handy way to get a 1-unit-long vector that faces in the same direction as your imported ogre's facing. If you want to move the ogre one giant ogre step forward, Transform.position += Transform.forward * one_giant_ogre_step. If you rotate the ogre, or move the ogre, away from its original arrangement, well, now you have opened up a whole ball of worms. Let's say you rotate the ogre by 45 degrees toward his right hand. Now his nose points in the northeast direction, not north as before. If you want to put a hat on the ogre's head, you will now need to think in the ogre's local space where ogre.transform.forward no longer matches the world space of Vector3.forward. The Transform now has to maintain two whole coordinate systems, to let you work in either world space or local space, depending on which is convenient. Well, Transform didn't have to do that, but Transform does that so that your code does not have to take on the burden and the bug-fighting involved in constantly calculating such things for yourself. So look at Transform.localPosition and Transform.localRotation. You can modify EITHER set, and the OTHER set will get updated auto-magically, according to the appropriate transform math. Pretty neat. What about that Transform.parent? Well, when you first add a new GameObject, the .parent is null. If you set the parent of the ogre's hat's transform to be the ogre's transform, then the ogre's hat will MOVE with the ogre without a single line of code to be written. It's attached, joined, locked together. To calculate the ogre's hat's position in world space, you would need to do the math that combines the ogre's hat's position in local space relative to the ogre, and the ogre's rotation, and the ogre's position in local space relative to the mine cart that the ogre is riding in, and so on. It's a huge stack of transformation math. Good thing that the Transform class does all this behind the scene, giving you both .localPosition and .position, and also .localRotation and .rotation.

OMG OMG OMG… I sooooo needed this.. omg thank you.. (hey, I'm an art person.. jeeze thinking in 3space sometimes gives me a petite mal seizure.. seriously, I can't look out windows in high spaces either..