Does anyone know how to make a good 3rd person rigid body controller?

Hi, I’m trying to create my first ever game in Unity, and even though it may be hard I’m trying to create a 3d platformer inspired by games like Crash Bandicoot, as I was researching for a way to control character I found that there are two ways, the character controller and rigid body, and while researching I discovered that the one that would most suit my game would be a rigid body controller, but I couldn’t find anywhere a good 3rd person rigid body controller, so what I am using now is a very bad character controller script that tries to imitate the rigid body controller in aspects of gravity, jumping and sliding, but it’s not very consistent, I was wondering if anyone knew or could help me create a rigid body controller for my game.

for anyone who wants to know the code I’m using it is right below:

ps: as I said this is my first game so I am very lost when it comes to coding this is also a reason why I need some help.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;

    public float speed = 10f;

    //turnning smoothness

    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    //jump controls

    public float jumpHeight = 3f;
    public float gravity = -29.43f;
    Vector3 velocity;
    bool isGrounded;

    //gravity check
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;



    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -1f;
        }

       //player movement on both axis

        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
     
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        if (Input.GetKeyDown("space") && isGrounded)
        {
            velocity.y += Mathf.Sqrt(jumpHeight * -2f * gravity);
        }


        if (direction.magnitude >= 0.1f)
        {
            //player turning depending on where moving

            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
         
            controller.Move(direction * speed * Time.deltaTime);
         
        }

    }

}

Every character controller is pretty much custom-tuned to the play and motionn of that game. Comparing even games of the same genre there are massive differences in choices, state processing, priority, timing, sequencing, etc.

This may or may not be solvable. Consistency might have something to do with the code, or it might have only to do with the dimensions and setup of the level, the speeds you choose, weights, forces, Physics Materials, etc.

To understand the inconsistency (and nobody here can do it!), I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

For something like this you might want to buy a good asset to do it. It’s not trivial to implement, and you’ll spend a lot of time fiddling with it instead of developing your game. Also, most assets include code you can look at, so if you’re interested in how they did it, you can dig in. Lots can be learned that way.

1 Like