How would i rewrite this script with a Character Controller? C#

Hello Everybody,

i was working for some Time on my Player and i wrote a Script. It is working well- but i can glitch throught walls and objects. I searched in Forums that i could use the Character Controller. I searched for the Character Controller Scripting but i did not really understand how i can use this in my script.
Can somebody show me how i implement a CC in my script- and its working like before? Transform.Translate was not a good idea! And the Character should walk and run at the same speed; if the framerate is 10 or 200, so if should be in Update() but * Time.deltaTime- or is this normal at the CC??

Please Help,
Thanks in Advance,

Heres my Code:

public float walkspeed;
    public float runspeed;
    [HideInInspector]
    public Rigidbody rigid;
    public float jumpspeed;
    public GameObject cam;
    [HideInInspector]
    public bool isrunning;
    [HideInInspector]
    public bool walkshake;
    public Rigidbody rigidb;
    
    

    void Update()
    {
        if (Input.GetKey("left shift") && Input.GetAxis("Vertical") > 0)
        {
            float trans = Input.GetAxis("Vertical") * runspeed;
            float strafe = Input.GetAxis("Horizontal") * runspeed;
            trans *= Time.deltaTime;
            strafe *= Time.deltaTime;
            transform.Translate(strafe, 0, trans);
            isrunning = true; //this bools are for the camera- play the run or walk shake anim- first person
            walkshake = false;
        }

        else if (Input.GetKeyDown("space"))
        {
            rigidb.AddForce(0, jumpspeed, 0);

        }

        else
        {
            float trans = Input.GetAxis("Vertical") * walkspeed;
            float strafe = Input.GetAxis("Horizontal") * walkspeed;
            trans *= Time.deltaTime;
            strafe *= Time.deltaTime;
            transform.Translate(strafe, 0, trans);
            if (trans > 0) {
                walkshake = true;
            }
            else
            {

                walkshake = false;

            }
            isrunning = false;
        }
    

        
        



    }

To use the CC you have to call the function ChatacterController.Move (). How this function works is by taking the current position of the player (transform.position), and then adding the next position.

The “next position” is defined by your input. I would recommend storing this as a Vector3 because it more easioy allows you to normalize your direction vectors (in my opinion). This is important because with out doing so, you will move faster when moving diagonally.

This is pretty simple to set up: new Vector3 (horizontal input, 0, vertical input).normalized

Make sure this vector is convertes to world space using transform.TransformVector ().

Then call CC.Move():
CC.Move (input * speed * Time.deltaTime);

Now for jumping. The CC already gets grounding info, and this bool can be accessed with CharacterController.isGrounded.

To actually jump you need to store a vertical velocity. You will have to solve for this velocity using the handy equations:
KE = 0.5mv^2
PE = mgh
Set then equal, and solve for v, and you get
v = sqrt (2gh)
where g is gravity and h is jump height. It is important to note that g must be positive.

So to sum up, set your vertical speed variable to sqrt (2gh) whenever you jump.

Next up we must make the CC behave according to gravity. To do this we are going to substract from our vertical speed variable whenever we are grounded. Using v = at this is pretty simple:

vertical speed -= g * Time.deltaTime

Note: when the CC is grounded, set your vertical speed to something like this:

vertical speed = - g * Time.deltaTime

In my experience the CC does not t being grounded very reliably otherwise.

So finally, the last thing to do is to apply the certical speed to the player using the CC.Move() function. This can be done like so:

CC.Move (transform.up × vertical speed × Time.deltaTime)

Hope his help, and I hope I did all this correctly!