Grounded Checks in Unity
Grounded Checks in Unity
DOOM-style Game Solution
If our game is like DOOM, that is, the ground is a horizontal flat plane, the solution for our grounded check is pretty easy.
THE SETUP:
Create a cube and position it arbitrarily on a plane with default Transform. Create a script called GroundCheck and attach it to the cube.
THE LOGIC:
If the y-coordinate of the cube becomes less than or equal to 0.5, this means that our cube is touching the ground plane. Else, the cube is airborne.
SCRIPTING:
Save the script and play the game. Try moving the cube vertically and observing the messages that are logged to the console.
The Physics.CheckSphere Solution
The Physics.CheckSphere() is a handy function we can use to perform ground checks. The function returns a bool, and accepts two parameters, the center of the sphere and its radius
THE SETUP:
Create a sphere and position it arbitrarily on a plane with default Transform. Create a script called PhysicsGroundCheck and attach it to the sphere.
THE LOGIC:
The Physics.CheckSphere() method will return true when another collider is present inside the sphere specified by the parameters passed to the method.
SCRIPTING:
The sphere collider of the sphere itself will need to be turned off. Else, it will return true every frame. Move the sphere vertically and observe the messages output to the Console.
Comments
Post a Comment