Movement in VR Part 3 ( Gravity ) - Unity3d

 Gravity and falling.

First, we're going to create a few variables, gravity = -9.81f, LayerMask ground layer, and falling speed.

We can do a simple gravity for our character, we can constantly move our character downwards with the speed of fall speed, but it doesn't feel correct because gravity isn't constant. So that's why we have gravity, we'll increase the fall speed by the gravity. But we can't do this constantly because then eventually we'll fall faster than the speed of light. So what we can do is check if we're grounded, if we're grounded that means we're not falling and so we don't need to increase falling speed and can reset the fall speed. We can check this at the end of FixedUpdate function.


We multiply the falling speed by Time.fixedDeltaTime to simulate gravity.

Now in order to determine if our character is grounded, we can use ray casting to see if our character capsule is touching the ground layer. We can start casting from the center of our character and use a sphere cast downwards to determine if we're touching anything or more specifically the ground layer objects. If we are then return true, else false.