adding a jump button without using 'rigidbody' (very new to coding)

hi guys,
i’m very very new to coding and programing and i’m trying to create TPS movement.
i got the camer and basic controlls from a brackeys video but i can’t add a jump function because every tutorial tells me to use rigidbody, which when added causes the box (player) object to start rolling around when being controlled: instead of ‘gliding’ when i press ‘w’ to go forward it starts rolling.

anyway, here’s the code i used for movement:

using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;


public class ThirdPersonMovement : MonoBehaviour

{
   

    public CharacterController controller;
    public Transform cam;
    public float speed = 6f;
    public float turnSmoothTime = 0.01f;
    float turnSmoothVelocity;

    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
       
        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
        }
       
       
    }
}

help would be appreciated, even more so thoroughly explained help :)))

There’s an example script on this manual page that implements jumping (and falling) without Rigidbody physics:

thanks! and should i add this to the script or as a new script?

You should read the script in the example and incorporate the bits related to jumping and gravity into your existing script.

ok, i tried to incorporate it in my script and the player does not move at all now. this is the current script:

using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;


public class ThirdPersonMovement : MonoBehaviour

{
   

    public CharacterController controller;
    public Transform cam;
    public float speed = 6f;
    public float turnSmoothTime = 0.01f;
    float turnSmoothVelocity;
    private float jumpHeight = 1.0f;
    private float gravityValue = -9.81f;
    private Vector3 playerVelocity;
    private bool groundedPlayer;

    private void Start()
    {
        controller = gameObject.AddComponent<CharacterController>();
    }

    void Update()
    {
        groundedPlayer = controller.isGrounded;
        if (groundedPlayer && playerVelocity.y < 0)
        {
            playerVelocity.y = 0f;
        }

        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
       
        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
            if (Input.GetButtonDown("Jump") && groundedPlayer)
            {
                playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
            }

            playerVelocity.y += gravityValue * Time.deltaTime;
            controller.Move(playerVelocity * Time.deltaTime);
        }
       
       
    }
}

I’m certain you’ve missed the step in all of these tutorials where they tell you how to lock the rotation on the rigidbody, which prevents this from happening.

how do i do so?

thanks. i’ll try that. if i’m not mistaken that caused it to jitter but i’ll check again.
just out of curiosity, could you explain why my script combined script didn’t work? i got a console error saying that the ‘object reference’ was not set to anything

As a general rule, when you have an error, copy and paste the full error message rather than trying to paraphrase it, as there are details in the message that help to debug it, such as the line number.

Without seeing the line number, the most likely culprit is probably that you need to assign “cam” in the inspector.