Devlog 1: Player


As part of my Gameplay Programming project, I have been tasked to create a player controller. Luckily, I made one last Summer. What a coincidence!

The video showcases the finished animated product, but it took quite a bit of trial and error to make really work. Even the original code I produced had some issues that needed to be sorted out while developing this.

Let's look through the update of our single script, the player lerp animation.


We have 8 lines here, 7 of which are created functions, and the last is a built-in function with Unity. That makes it super easy to read how the Update flow works. I'm not sharing everything though! Just stuff that is a little harder to grasp through communication.

Get Input

Get Input simply takes the player inputs from Horizontal and Vertical inputs, and translates them to the X and Z axis, respectively. Then it clamps them being  somewhere between 0 and 1. So if you only put a little force in on a joystick, you'll only go a little fast. keyboard keys don't have this luxury, so they are either 0 or 1.

Calculate Camera

this function uses the camera.forward and camera.right vectors. It sets both their Y's to zero, then normalises the other two.

Calculate Ground

Go ahead and take this one! It got cut, but I forgot to remove it before writing this post. A raycast fired down from the bottom of the player goes down 0.2 units below the player's bottom. If it hits something, you're on the ground, and can jump and do standing animations. The issue is, you can be standing on something, but your centre isn't standing on it, so the game thinks you're airborne, and you go into a falling animation. This couldn't do, so I exchanged it for this;

This isn't so clean, but still works out. Adding a rigidbody and sphere collider to the bottom of my player character, and then doing this action when the sphere collides and stops colliding with the world, decides whether you are grounded or not. These work on their own thread, so don't need to be referenced in update.

Move Plane


The basic idea of this function is written in green at the top. anim.SetBool is related to the animation tree that the player character uses. If you're moving, you're using the movement animation. A shame there isn't a walking animation in the assets I have been asked to use.

Gravity


If you jumped off of an apartment block building, you would probably die. But you would also probably accelerate as you fell, too! The same is true for when you're controlling this character, except the dying part.

"airTimer", like Moving in the previous function, determines whether you are in the falling animation. If your airTimer is greater than 0.1, you're falling. If, while falling, your airTimer goes to anywhere below 0.1, like it does go to 0 where you're on the ground, you'll go to a landing animation.

Jump

It's quite simple really, if you press the jump button, you jump, and the jump animation plays, which then transitions to the falling animation. Your y velocity gets set to whatever you wanted it to be at the top of the script, and the Gravity function slowly brings it down to zero.

"Ah, I got you!" I hear you say, because I said it to myself as I wrote this, "What if I were to do a jump next to a low wall and then quickly jump again on the wall? I would get double the height!". And I see your(my) question, and label you(myself) a dolt. I just tested it, and that cool idea doesn't work, because it sets the jump velocity, instead of adding to it. Maybe it will work in the future, though.

Attack

Attack is the exact same as Jump, except it doesn't even create a collision box where you attack yet. It literally just triggers an animation.

Speaking of animations, here's the tree that calculates them all!


Look at that tree, absolutely DISGUSTING, if I say so myself! It makes sense to me though, and with a little looking after swallowing the puke you summoned from looking at it, it's not so bad. Most of the transition states are discussed above in the blog post, and Unarmed-Roll-Forward is actually Attack01, I just used roll-forward initially because it didn't look as wonky as the broken asset attack currently is. Hoping to fix that attack soon.

Also, the issue in the video? I can now replicate it, if you jump during your attack animation, the jump is delayed until the animation ends. This can be solved by putting another connection between roll-forward and unarmed-jump, triggered by JumpTrigger.

I'm glad you read this far. You don't have access to all of the code I made for this project, or how I edited what I learnt, but you do have access to the tutorials I followed.

This series by Nimso Ny is incredibly helpful to recreating movement not dissimilar to that of a popular 3D platformer game.

Thanks for reading, and have a good day!

Leave a comment

Log in with itch.io to leave a comment.