Time flies when you’re having fun, and the 2D dungeon escape was fun but as it draws to a close for now (more to come as always lol) it’s time to move onto the world of 3D by making a 3rd person zombie shooter.
Since we are working in 3d now, there is going to be a lot to learn but at the same time, we can already use a lot of what we already have learnt which should make things a lot easier.
As always, starting off with the super basics to get the mechanics working before adding in all the prettiness.
A capsule for the player, a cube for the ground, perfect for testing things out.
To get the movement working we can use the code we used in the 2.5D platformer. Check if the player is grounded, if they are, check for the horizontal and verticle axis for side to side and forwards/back movement then move based on those inputs.
And of course, testing things out to make sure they work.
Looking good so far.
Bonus: Camera Look system.
Now for the camera to move around.
First, we will need a reference to the camera that is currently sitting on the player as a child.
Next, we can use the input manager to get the axis for the mouse.
Let’s start on the easier of the 2, looking left and right.
First, we will want to make a new variable to store the current rotation of the player which we can get from transform.localEulerAngles. This should give the players rotation in a Vector 3. Next, we can add the mouse X to the current rotation’s y-axis, which will let us turn the player around. Lastly, we can set the transform.rotation to Quaternion.AngleAxis(currentRotation.y, Vector3.up); Since transform.rotation needs it to be a quaternion we have to use this.
Time to test it out!
Movement, check! Looking from left to right, check!
Now for up and down. Unlike the left and right, up and down we will need to change the camera’s axis, weird things happen when you change the player's transform for this one too.
I don’t think it’s meant to do that!
Although, there isn’t much difference between up/down and left/right, it’s mostly just what you’re applying it to.
Again we want to make a new vector 3 to store the camera’s current rotation and use the local euler angles we just need to point it at the camera’s transform instead of using the base transform. Next, add the mouse Y to the camera’s X-axis, and lastly, assign the new rotation to the quaternion angle axis using vector 3.right this time.
This seems all good and well until you test it out (this is why we test so often).
Up and down seem to be working fine but what is going on with the side to side now? This one actually happens when you don’t use the local rotation of the camera, which comes into the topic of world space vs local space (which I will go into in the next article).
With that one change, now we get this which is much better.
Now that we have the movement for the player and we can look around, we can take a look into the world space vs local space to see what was going on with our camera before. See you there!