Understanding Movement and Rotation in C#

Global vs Local Space

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video segment, learn the distinction between Local Space and World Space and how to transform between the two using the Transform component.

Keywords

  • Local Space
  • World Space
  • Unity
  • Coordinate systems
  • Transform
  • Inverse transform

About this video

Author(s)
Alan Thorn
First online
12 January 2019
DOI
https://doi.org/10.1007/978-1-4842-4442-5_9
Online ISBN
978-1-4842-4442-5
Publisher
Apress
Copyright information
© Alan Thorn 2019

Video Transcript

[Audio Begins] [0:00:00]

Alan Thorn: In this movie, I want to discuss the difference between local space and world space, or rather between local coordinates and world coordinates. This is a critically important distinction in Unity that allows you to harness unprecedented levels of power when controlling the movement and direction of objects. In particular, we’re going to look at one, what the difference is between local space and world space, and then two, the practical steps we can take to convert points, directions, and spaces between the different coordinate systems.

To demonstrate this, I’m going to be using the sample scene that we’ve been working with here. And in fact, one of the very first distinctions to highlight local and world space is if I rotate this car object. You can see that here, this car object has a forward vector pointing forward here. And yet this direction can be represented in two different ways. The local space way is to say that this forward vector has the direction of zero, zero, one. Because it has a value of one in the zed field. Notice that when we align this object to the world origin here, the forward vector of this vector here is going to be pointing forwards, that is zero, zero, one. As soon as we move this away from the world space here, we still have this concept of this blue forward-pointing arrow. In terms of local space, the actual representation of this vector remains unchanged. It’s value is zero, zero, one, but having been rotated to face in this direction. In terms of the space of the world, this direction is no longer zero, zero, one, but has some other value because the vector has been rotated. So in terms of its absolute representation and direction within the world, its value is not zero, zero, one. But for the purposes of this object, the fact that it’s always pointing forwards, locally we could say that it was a vector of zero, zero, one. The local space representation is zero, zero, one, and the world space representation is something else.

I’m going to restore this back to the origin here, and I’m simply going to go back to the script file. In particular, I’m going to script file for this gun tote that we created in the preceding movie, which is this object here, the Look At script. If I display the Look At script here inside visual studio, I’m going to create a representation inside the update function here that’s going to display the difference between the world and local coordinates. I’m going to create a public variable here, and it’s only being created as public so that we can see its value inside the object inspector. I’m going to choose vector 3 here, and we’re going to be viewing the world direction, that is rather the forward direction as expressed in terms of the world. So we have our forward direction here. And I’m going to display the other direction values, the forward direction in local space. So let’s just put that here, local.

Now, inside the update function, I’m just going to assign there some values. So the forward direction is simply going to be this transform.forward. And my local forward direction is simply going to be vector 3 dot forward. The critical difference between these two is that one is defined in local space and the other is defined in world space. I’m going to choose Ctrl-S to save it, and I’m going to move back to Unity, and select the pivot point here, so that I can take a look here at the two vectors. I’m going to press Play on the toolbar to take a look at their values. So we see here that the forward vector of this terret [phonetic] can be represented in two ways. The local space representation is zero, zero, one. And it doesn’t matter where this terret turns to rotate, it will always have a local forward vector of zero, zero, one. But the world space version of that, is these values, these three values here.

Now, importantly, we can convert between the two spaces. So for example, if I take a look at the direction here, we have the forward direction, the world. And I’m going to rename this other one here to be converted. I’m just going to call it that for one moment. The forward direction of the world is going to maintain the same value that we have, which is this transform.forward. But I want to show you this function here. This transform.transformdirection. We can put local space directions in here, and Unity will convert them for us. So for example, vector3.forward, which is the same as vector3, zero, zero, one. Here we have a converted world space direction. So this time we’re using function transform direction to take a vector that’s specified in local space. And Unity will automatically convert it for us to world space.

I’m going to choose Play on the toolbar, and notice this time the three values, or the two values here are exactly the same. One is the transform.forward vector that always is the world space direction for the forward axis on the object. But the second one took the forward vector in local space, zero, zero, one, and converted it to the world space representation.

Just so that you know, Unity has a function to convert the other way too. That is to take a world space vector and to convert it back into local space. That function is transform.inversetransformdirection. So it has inverse taken here. If I were to do that and plug in the forward vector, for example, into here, we would in fact get back our local vector. So I’m just going to go back to this here, and make sure that the code is saved. Go back to Unity and take a look at the converted probe. To see, I choose Play. And this time notice that we have our vector back, our transform director back, which is simply a one in the zed axis. So we’ve been able to take a vector in local space and convert it to world space. And we can also use the inverse transform direction to go the other way.

In this movie, we looked at the distinction between world space and local space and how we could transform between the two different spaces using the transform component with transform direction and inverse transform direction.

[Audio Ends] [0:07:31]