Here's a rigidbody character controller in Unity that uses root motion for movement:
Character Controller Script
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Animator))]
public class RootMotionCharacterController : MonoBehaviour
{
[Header("Movement Settings")]
public float walkSpeed = 2f;
public float runSpeed = 5f;
public float rotationSpeed = 10f;
public float groundCheckDistance = 0.1f;
[Header("Ground Detection")]
public LayerMask groundMask = 1;
public Transform groundCheck;
private Rigidbody rb;
private Animator animator;
private Vector3 rootMotionVelocity;
private bool isGrounded;
// Input variables
private Vector2 inputVector;
private bool isRunning;
// Animation parameter hashes for performance
private int speedHash = Animator.StringToHash("Speed");
private int isGroundedHash = Animator.StringToHash("IsGrounded");
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
// Configure rigidbody
rb.freezeRotation = true; // Prevent physics rotation
// If no ground check transform is assigned, create one
if (groundCheck == null)
{
GameObject groundCheckObj = new GameObject("GroundCheck");
groundCheckObj.transform.SetParent(transform);
groundCheckObj.transform.localPosition = Vector3.down * 1f;
groundCheck = groundCheckObj.transform;
}
}
void Update()
{
HandleInput();
CheckGrounded();
UpdateAnimator();
}
void FixedUpdate()
{
ApplyRootMotion();
}
void HandleInput()
{
// Get input from WASD or arrow keys
inputVector.x = Input.GetAxis("Horizontal");
inputVector.y = Input.GetAxis("Vertical");
// Check for run input (Left Shift)
isRunning = Input.GetKey(KeyCode.LeftShift);
}
void CheckGrounded()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckDistance, groundMask);
}
void UpdateAnimator()
{
// Calculate movement speed based on input magnitude
float targetSpeed = inputVector.magnitude;
// Apply run multiplier if running
if (isRunning && targetSpeed > 0)
{
targetSpeed *= (runSpeed / walkSpeed);
}
// Set animator parameters
animator.SetFloat(speedHash, targetSpeed, 0.1f, Time.deltaTime);
animator.SetBool(isGroundedHash, isGrounded);
// Handle rotation based on input
if (inputVector.magnitude > 0.1f)
{
Vector3 inputDirection = new Vector3(inputVector.x, 0, inputVector.y);
Vector3 worldDirection = Camera.main.transform.TransformDirection(inputDirection);
worldDirection.y = 0;
worldDirection.Normalize();
if (worldDirection.magnitude > 0.1f)
{
Quaternion targetRotation = Quaternion.LookRotation(worldDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,
rotationSpeed * Time.deltaTime);
}
}
}
void ApplyRootMotion()
{
if (!isGrounded) return;
// Apply root motion velocity to rigidbody
Vector3 velocity = rb.velocity;
velocity.x = rootMotionVelocity.x;
velocity.z = rootMotionVelocity.z;
rb.velocity = velocity;
// Reset root motion velocity
rootMotionVelocity = Vector3.zero;
}
// Called by Unity's animation system
void OnAnimatorMove()
{
if (!isGrounded) return;
// Capture root motion from animator
rootMotionVelocity = animator.deltaPosition / Time.fixedDeltaTime;
// Apply root motion rotation
transform.rotation *= animator.deltaRotation;
}
void OnDrawGizmosSelected()
{
// Draw ground check sphere
if (groundCheck != null)
{
Gizmos.color = isGrounded ? Color.green : Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckDistance);
}
}
}
Setup Instructions
-
Attach the script to your character GameObject that has a Rigidbody and Animator component.
-
Configure the Animator Controller with these parameters:
Speed (float) - Controls movement animations
IsGrounded (bool) - For jump/fall animations
-
Animation Setup:
- Create blend trees for idle/walk/run animations
- Ensure your animations have root motion enabled
- Set up transitions based on the Speed parameter
-
Rigidbody Settings:
- Mass: 1
- Drag: 0
- Angular Drag: 0
- Freeze Rotation: X, Y, Z (handled by script)
-
Ground Check:
- The script will create a ground check object automatically
- Or assign your own Transform to the
groundCheck field
- Set the
groundMask to match your ground layer
Key Features
- Root Motion Integration: Uses
OnAnimatorMove() to capture and apply animation-driven movement
- Physics-Based: Maintains physics interactions while using root motion
- Ground Detection: Prevents root motion application when airborne
- Smooth Rotation: Camera-relative movement with smooth turning
- Performance Optimized: Uses animation parameter hashes and efficient ground checking
This controller provides a solid foundation that you can extend with jumping, climbing, or other movement mechanics while maintaining the benefits of both root motion and physics-based movement.