Struggling with Sonic-Style Physics on Loops and Slopes in Unity (3D) Using Dynamic Rigidbody

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật