I’m building a Sonic-inspired fangame in Unity and have created a custom physics framework called the “Vortex Engine” to handle Sonic’s movement. The character’s movement on flat, horizontal ground works perfectly, but I’m having difficulty getting Sonic to correctly run on loops and slopes, especially at high speeds. The character either detaches from the slope or doesn’t stick to it correctly.
I suspect that using a dynamic Rigidbody might be part of the issue, but I’m not entirely sure. I’ve spent about a week trying to solve this problem with minimal progress.
Below is the script that handles Sonic’s movement. I’ve included only the relevant portions for clarity, but if more context is needed, I’m happy to provide the full script.
using UnityEngine;
public class SonicController : MonoBehaviour
{
// Movement variables
public float acceleration;
public float deceleration;
public float topSpeed;
public float maxSpeed;
// Slope physics variables
public float slopeAssistance = 5f;
public float slopeDrag = 5f;
public float wallStickSpeedThreshold = 2f;
private Rigidbody rb;
private bool isGrounded;
private float currentSpeed;
private Vector3 groundNormal;
private Vector3 inputDirection;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
HandleInput();
CheckGrounded();
HandleMovement();
}
void HandleMovement()
{
if (isGrounded)
{
AlignWithGround();
if (inputDirection.magnitude > 0)
{
currentSpeed += acceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, 0, topSpeed);
float slopeRotation = Vector3.Dot(inputDirection, groundNormal);
if (slopeRotation > 0) // Moving downhill
{
currentSpeed += slopeRotation * slopeAssistance * Time.deltaTime;
}
else if (slopeRotation < 0) // Moving uphill
{
currentSpeed += slopeRotation * slopeDrag * Time.deltaTime;
}
}
else
{
currentSpeed -= deceleration * Time.deltaTime;
if (currentSpeed < 0) currentSpeed = 0;
}
Vector3 moveDirection = transform.forward * currentSpeed;
rb.velocity = new Vector3(moveDirection.x, rb.velocity.y, moveDirection.z);
}
}
void CheckGrounded()
{
RaycastHit hit;
Vector3 rayDirection = -transform.up;
isGrounded = Physics.Raycast(transform.position, rayDirection, out hit, groundCheckDistance, groundLayer);
if (isGrounded)
{
groundNormal = hit.normal;
if (Vector3.Angle(Vector3.up, groundNormal) >= 60f && currentSpeed < wallStickSpeedThreshold)
{
isGrounded = false;
}
}
}
void AlignWithGround()
{
Quaternion slopeRotation = Quaternion.FromToRotation(transform.up, groundNormal) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, slopeRotation, Time.deltaTime * 10f);
}
}
Here are my current rigidbody and variable settings: Rigidbody Settings Variable Values
Issues:
- Sonic detaching from slopes and loops at high speeds.
- Inconsistent behavior on slopes: Sonic doesn’t stick to the slope as
expected. - Handling of steep surfaces: Sonic slides off, doesn’t follow the
surface properly or just gets stuck halfway up the surface.
Attempts to Fix:
- Tried adjusting Rigidbody settings (e.g., mass, drag, interpolation).
- Modified slope physics variables (e.g., slopeAssistance, slopeDrag).
- Used AlignWithGround and CheckGrounded methods to better align Sonic
with the slope.
What changes can I make to ensure that Sonic correctly sticks to and moves on loops and slopes? Are there any common practices or specific Rigidbody settings that I should be using for this type of physics in Unity?