Any ways to improve my Character Movement code?

I would also like to improve my sprinting function, I followed a youtube tutorial just for the movement, but I had to wing it for the sprint. I want to be able to just hold down the left shift key to sprint, but if the player accidentally hits the left shift key, the moveSpeed is just doubled and when the left shift key is pressed again, the speed is quadrupled.

I also keep getting: Look rotation viewing vector is zero. I’ve researched everything I could I cannot seem to keep it from popping up, it states that there’s an issue with the line: transform.forward += heading;

Also note, that I am creating a 3D Isometric game, so the player movement is crucial to the gameplay.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharController : MonoBehaviour
{

    public Rigidbody rb;
    public bool playerIsOnTheGround = true;


    [SerializeField]
    float moveSpeed = 5f;
    float sprintSpeed = 2f;

    Vector3 forward, right;

    void Start()
    {
        forward = Camera.main.transform.forward;
        forward.y = 0;
        forward = Vector3.Normalize(forward);
        right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;

        rb = GetComponent<Rigidbody>();
    }

    
    void Update()
    {
        if (Input.anyKey)
            Move();
    }

    void Move()
    {
        Vector3 direction = new Vector3(Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
        Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
        Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");

        Vector3 heading = Vector3.Normalize(rightMovement + upMovement);

        transform.forward += heading;
        transform.position += rightMovement;
        transform.position += upMovement;

       if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            moveSpeed = moveSpeed * sprintSpeed;
        }

       if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            moveSpeed = 5f;
        }



        if (Input.GetButtonDown("Jump") && playerIsOnTheGround)
        {
            rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
            playerIsOnTheGround = false;

        }

    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            playerIsOnTheGround = true;
        }
    }
}

I’m almost afraid to tackle all the errors in that script, lol, so I will help you with the sprint issue:

if (Input.GetKey(KeyCode.LeftShift))
{
     moveSpeed = moveSpeed * sprintSpeed;
}
else
{
     moveSpeed = 5f;
}

and would suggest caching the camera, then just setting up a Quaternion(rotation) that feeds off of camera forward(if you plan to use camera for direction purposes).

This makes no sense, as the objects forward can only be which way is it’s forward:

transform.forward += heading;

you should be stating the objects transform.rotation = , and it is better to use transform.Translate(0, 0, moveSpeed); instead of trying to just set it’s position like this:

transform.position += rightMovement;
transform.position += upMovement;

So to simplify it, you just want to set the objects forward(and backward) with Translate on it’s z axis. Then you modify which rotation the object should turn to with Euler or Quaternions (I suggest studying up on Quaternions). And the speed you only want while Lshift is pressed down, so that’s a simple if>else statement of GetKey which works while it is pressed and held. GetKeyDown is a one time call of if it was pressed, and GetKeyUp is rarely ever used by anyone(but is also a one call function).