Creating a Rigidbody Character Controller in Unity3D using Root Motion involves a few key components:
- Using Animator for animations that drive movement (root motion enabled).
- Applying physics using a Rigidbody and Collider.
- Handling rotation and movement in sync with the animation.
- Optionally, applying extra forces like gravity or slope adjustments.
✅ Overview
We will create a MonoBehaviour script that:
- Uses root motion from the Animator.
- Handles rotation based on movement direction.
- Optionally applies gravity manually (since Rigidbody gravity may interfere with root motion).
- Applies slope alignment if needed.
🧩 Step-by-Step Implementation
1. Set Up Your Animator
- Ensure your character’s Animator has Apply Root Motion enabled:
- In the Animator Controller, check "Apply Root Motion".
- Make sure your animations have Root Motion baked.
2. Create the Script
Create a new C# script called RigidbodyRootMotionController.cs and attach it to your character GameObject (must have Animator, Rigidbody, CapsuleCollider).
using UnityEngine;
[RequireComponent(typeof(Animator), typeof(Rigidbody), typeof(CapsuleCollider))]
public class RigidbodyRootMotionController : MonoBehaviour
{
[SerializeField] private float rotationSpeed = 10f;
[SerializeField] private float gravity = -9.81f;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float groundCheckDistance = 0.1f;
private Animator animator;
private Rigidbody rigidbody;
private Vector3 velocity;
private bool isGrounded;
private void Awake()
{
animator = GetComponent<Animator>();
rigidbody = GetComponent<Rigidbody>();
// Lock rotation through physics
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
private void Update()
{
UpdateGroundCheck();
HandleGravity();
}
private void FixedUpdate()
{
ApplyPhysicsMovement();
}
private void OnAnimatorMove()
{
// This is where root motion is applied
if (animator && animator.velocity.magnitude > 0f)
{
// Optional: Rotate character towards movement direction
Vector3 motionDirection = animator.velocity.normalized;
if (motionDirection.sqrMagnitude > 0.1f)
{
Quaternion targetRotation = Quaternion.LookRotation(motionDirection, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
}
}
}
private void UpdateGroundCheck()
{
Ray ray = new Ray(transform.position + Vector3.up * 0.1f, Vector3.down);
isGrounded = Physics.Raycast(ray, groundCheckDistance + 0.1f, groundMask);
}
private void HandleGravity()
{
if (!isGrounded)
{
velocity.y += gravity * Time.deltaTime;
}
else
{
velocity.y = Mathf.Min(0, velocity.y); // Keep on ground
}
}
private void ApplyPhysicsMovement()
{
Vector3 move = velocity * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + move);
}
// For animation control
public void SetWalkAnimation(bool walking)
{
animator.SetBool("IsWalking", walking);
}
public void PlayJumpAnimation()
{
animator.SetTrigger("Jump");
}
}
3. Configure the Animator
In your Animator Controller:
- Enable Apply Root Motion.
- Set up parameters like
IsWalking (bool), Jump (trigger), etc.
- Use Mecanim animations with baked root motion.
4. Optional: Slope Handling
To align the character to the ground slope:
private void AlignToSlope()
{
if (isGrounded && slopeHit.collider != null)
{
Vector3 slopeNormal = slopeHit.normal;
Quaternion slopeRotation = Quaternion.FromToRotation(Vector3.up, slopeNormal) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, slopeRotation, rotationSpeed * Time.fixedDeltaTime);
}
}
You'll need to cast a ray downward and store the slopeHit in UpdateGroundCheck.
✅ Summary
This controller:
- Uses root motion from animations for movement.
- Maintains physics interaction via Rigidbody.
- Manages rotation to follow movement direction.
- Applies custom gravity to keep the character grounded.
- Works well with Mecanim animations that use root motion.
📌 Tips
- If you're using Unity's built-in physics for jumping or slopes, you may need to adjust the root motion or disable it temporarily.
- Use Animator.playbackTime or IK for more advanced syncing (e.g., foot placement).
- You can blend between root motion and procedural movement if needed.
Let me know if you want to add features like jumping, slopes, or IK!