Implementing Parkour System in Unity

The concept of parkour, a training discipline focused on overcoming physical obstacles using movement, has seen immense popularity in video games, offering players an interactive and engaging environment. Implementing this system in Unity can elevate the gameplay experience. This tutorial will guide you through the process of setting up a basic parkour system in Unity, focusing on wall running and vaulting.

Assets Required

  • A basic character controller: We will be using a player controller from the Third-Person Camera in Unity as a base class for modifications.
  • Parkour animations: wall-running, vaulting, etc.

1. Setting the Scene

  • Ensure the environment has walls and obstacles tagged appropriately, using "Wall" for walls and "Obstacle" for vaultable obstacles.

2. Modifying the 'SC_TPSController' Script for Wall Running

2.1. Detecting a Wall:

  • This method checks if the character is beside a wall using raycasting.
bool IsBesideWall(Vector3 direction)
{
    RaycastHit hit;
    if (Physics.Raycast(transform.position, direction, out hit, 1f))
    {
        if (hit.transform.CompareTag("Wall"))
        {
            return true;
        }
    }
    return false;
}

2.2. Wall Running

  • This coroutine handles wall running.
public float wallRunTime = 2f; // Duration of the wall run.

IEnumerator WallRun(Vector3 direction)
{
    float startTime = Time.time;
    canMove = false; // Disable regular movement during wall run.

    while (Time.time - startTime < wallRunTime)
    {
        characterController.Move(direction * speed * Time.deltaTime);
        yield return null;
    }

    canMove = true; // Enable regular movement after wall run.
}
  • Integrate wall running in the 'Update()' method:
// Wall Run
if (IsBesideWall(transform.right) && Input.GetButton("Jump") && canMove && !characterController.isGrounded)
{
    StartCoroutine(WallRun(transform.right));
}
else if (IsBesideWall(-transform.right) && Input.GetButton("Jump") && canMove && !characterController.isGrounded)
{
    StartCoroutine(WallRun(-transform.right));
}

3. Modifying the 'SC_TPSController' for Vaulting

3.1. Detecting an Obstacle

  • Check for a vaultable obstacle in front of the character.
bool IsObstacleInFront()
{
    RaycastHit hit;
    if (Physics.Raycast(transform.position, transform.forward, out hit, 1.5f))
    {
        if (hit.transform.CompareTag("Obstacle"))
        {
            return true;
        }
    }
    return false;
}

3.2. Vaulting

  • Add a vaulting method:
void Vault()
{
    canMove = false; // Disable regular movement during vaulting.
    
    // Translate the player over the obstacle.
    Vector3 vaultMovement = (Vector3.up + transform.forward) * speed * Time.deltaTime;
    characterController.Move(vaultMovement);
    
    canMove = true; // Enable regular movement after vaulting.
}
  • Integrate vaulting in the 'Update()' method:
// Vaulting
if (IsObstacleInFront() && Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
    Vault();
}

Conclusion

These modifications will introduce wall running and vaulting functionalities to the existing third-person player controller. Test these new movements in various scenarios to ensure they perform as intended. Adjustments might be necessary depending on the game's specific environment or the desired parkour mechanics.