World or local?
objective: looking into the difference between world space and local space.
When working with 2D and 2.5D we haven’t really had to worry about local space and world space since we are always heading in 1 direction but with 3D we will have to start worrying about it. Since we can turn around we will want to start to head where we are looking, not in the same direction all the time.
Here’s an example, with moving forward normally you can use Vector3.forward, try doing that with a 3d game and this happens.
As you can see, the camera turns but the player keeps running forward, so does that mean the character isn’t turning when we turn the camera?
With the blue arrow being where the player is facing, you can see the player is turning but it’s still heading in 1 direction. To see what is really happening we can change a setting.
After clicking on that, it should change to global.
Now let’s take another look at the player moving.
As you can see, the blue arrow isn’t changing direction as the player looks around. This shows us that the player is moving in world space and not local space.
One way to work out how to fix this would be to google how to change world space to local space in unity
After looking for a bit you should come across transform.TransformDirection();
Now if we change the velocity of the player using transform direction, we should be able to move in local space.
(it’s before the jump because it causes other bugs if it’s after.)
So now we get this.
This is why local space vs world space is a much bigger deal when it comes to 3D, we want to be able to go where we are looking.
Now for that bug with it being after the jump.
As you can see, the player does a little dance in mid-air instead of following through with the jump which we can see if we take a look at the Velocity of the player.
Yay for bugs!
Now for why that camera was messing up.
This one as I said before, comes down to world space vs local space. With world space, it’s taking its position and spinning it around the player but with local space, it’s spinning around its own axis as we want.
Another little short description of world space and local space and the hassle they can cause when you aren’t aware of them but also how we can fix them. Hope this has helped! Next time we can start on the shooting side of things. See you there!