Gravity

...wants to bring me down

·

2 min read

Gravity

I had to make THE decision about Player movement: Rigidbody or not? Since I don’t really have a full idea of how the gameplay should play out, I decided to go with a CharacterController and re-evaluate my decision at a later stage. I like the idea of having more control over player movement and I also prefer an arcade-y movement style so for now my decision was quite natural.

Implementing gravity in this context made me realise that managing physics, however simple, would probably benefit from knowing the Controller state. For example, Unity's documentation seems to suggest an application of gravity where its contribution to the vertical velocity (velocity.y) is disabled when the Controller is grounded.

As of now I however decided to avoid it for the sake of not overcomplicating, also because I noticed that any GameObject with a mesh renderer seem to block the Controller’s movement, making the isGrounded check practically useless for gravity calculations; in other words I can just keep the Y component of the CharacterController's Move method active at all times.

private void ApplyGravity()
{
     // This is then contributing to the overall player's 
     // velocity inside Update()
     _velocity.y += gravity * Time.deltaTime;
}

If the gameplay I will want will become particularly nuanced with many different physics states (swimming, jumping, etc.) it might make sense to implement a basic State Machine to manage all the possible combinations.

Let's see if I will have to refine the whole concept. For now, this is the very elementary result: gravity.gif