How To Sprint In Unity 3D | Unity Tutorial
Unlock the full potential of your game characters with our step-by-step video tutorial on How to Sprint in Unity 3D! In this guide, we'll walk you through the process of implementing a dynamic sprinting mechanic, enhancing your game's movement system. Learn how to set up sprinting controls, adjust player speed, and create smooth transitions between walking and sprinting states. Whether you're a beginner or looking to refine your skills, this tutorial provides clear explanations and practical tips to elevate your Unity 3D projects. Hit play and start sprinting towards better gameplay today!
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Scripts:
PlayerSprintController.cs
using UnityEngine;
public class PlayerSprintController : MonoBehaviour
{
public float moveSpeed = 5f; // Normal movement speed
public float sprintSpeed = 10f; // Sprint speed
public KeyCode sprintKey = KeyCode.LeftShift; // Key to activate sprint
public LayerMask groundLayer; // Layer mask for detecting ground
public float groundCheckDistance = 1.1f; // Distance to check for ground
private float currentSpeed;
private Rigidbody rb;
private bool isGrounded;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotation; // Freeze rotation to prevent tipping
currentSpeed = moveSpeed; // Set initial speed
}
private void Update()
{
// Check if sprint key is pressed
if (Input.GetKey(sprintKey))
{
currentSpeed = sprintSpeed;
}
else
{
currentSpeed = moveSpeed;
}
// Check if player is on the ground using a raycast
isGrounded = Physics.Raycast(transform.position, Vector3.down, groundCheckDistance, groundLayer);
}
private void FixedUpdate()
{
if (isGrounded) // Only allow movement if grounded
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontalInput, 0, verticalInput) * currentSpeed * Time.deltaTime;
rb.MovePosition(transform.position + movement);
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------
CameraFollow.cs
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform player; // The player's transform
public Vector3 offset; // Offset position from the player
public float smoothSpeed = 0.125f; // Speed at which the camera follows
void LateUpdate()
{
if (player != null)
{
// Calculate the desired position
Vector3 desiredPosition = player.position + offset;
// Smoothly interpolate between the camera's current position and the desired position
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
// Update the camera's position
transform.position = smoothedPosition;
}
}
}