Re-Creating Unity's Friction and Bounce (C#)

Hello Unity friends!

I am in the process of emulating Unity’s physics via script.

My main purpose for doing this is to implement portions of this code where appropriate in various scripts (eg: friction code in a custom character controller, implemented only while grounded).

Thus far, I’ve successfully emulated gravity and drag as such:

using UnityEngine;

public class RePhysics : MonoBehaviour
{
    public float drag;

    private Rigidbody Body;

    private void Start()
    {
        Body = GetComponent<Rigidbody>();

        Body.drag = 0f;
        Body.useGravity = false;
    }

    private void FixedUpdate()
    {
        var velocity = Body.velocity;

        Gravity( ref velocity );

        Drag( ref velocity );

        Body.velocity = velocity;
    }

    private void Gravity( ref Vector3 velocity )
    {
        velocity += Physics.gravity * Time.fixedDeltaTime;
    }

    private void Drag( ref Vector3 velocity )
    {
        velocity *= Mathf.Clamp01(1f - drag * Time.fixedDeltaTime);
    }
}

I’m currently struggling to determine how Unity implements bounce and friction.

There seems to be a distinct lack of information regarding this, and thus far, my Unity Answers post has garnered no response.

I’ve read the documents pertaining to physics in video games linked to Unity Answers topics similar to this one, but none appear to relate directly to Unity’s approach.

Does anyone know the code / formulas that Unity uses for its friction and bounce?

Any help in this area would be most appreciated.

Thank you so much in advance!

~ S.

got to ask… why? you’re recreating something that already exists and works… learning project or something?

Partially. I’ve only been using Unity for about a year and a half, and had never used C# prior to that.

Primarily, I find that a character controller suitable to my needs doesn’t exist, so I’m trying to create one that conforms to some Unity standards. My initial approach was to simply enable / disable or change the character’s material when it wasn’t grounded to prevent unwanted issues such as wall-sticking, but alas, there appears to be significant issues (glitches?) with that approach.

Additionally, by having code that emulates Unity’s own approach, I can confidently modify it to my future needs.

I’ve had robust results just going with a rigidbody controller and some spherecasts, if that helps. I can’t use Unity’s character controller on account of the garbage it spews when you want to query something (imagine for 40 enemies active) and I still need to interact with the world.

Character controller imo is just for prototyping.

Thank you for your response!

Actually, my Rigidbody character controller is mostly functional. What I’m working on right now is converting from arbitrary deceleration values to ones based on the ground itself – as well as implementing a Unity-standard bounciness. I know how I could fake this, but I want to start with a Unity-accurate baseline, then tweak it if I need to.

My post is less about creating a character controller, and really more about determining the actual code necessary to recreate Unity’s bounce and friction application.

1 Like

Also, I agree completely with the built-in character controller just being for prototyping.