Player Gravity Glitch

I made a player movement script with gravity, but each time i run my game, the player just goes down a bit really fast (like it’s teleporting), but then it just stays in the air. Then, when the player body is not directly above the ground, it just falls down. Here’s the entire script.

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -7.93f;

    Vector3 velocity;

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }
}

(yeah it’s the brackeys script)

Which unfortunately is derivative of the now-faulty Unity example script code, which is STILL provided as the reference code.

I wrote about this before: the Unity example code in the API no longer jumps reliably.

I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken, as you can see here:

https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

Here is a work-around:

https://discussions.unity.com/t/811250/2

I recommend you also go to that same documentation page and ALSO report that the code is broken.

When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

https://discussions.unity.com/t/855344

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!

Thanks!

Well shoot, i figured out that your workaround conflicts with my mouselook script (again from brackeys).
Here’s the mouse look:

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

public class MouseLook : MonoBehaviour
{

    public float mouseSensitivity = 300f;

    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

If you could also send me a work-around for this, that would be great!

Steps to success:

  • identify how the two scripts tangle with each other (is it a common transform both are trying to manipulate? only one will win!)

  • restructure the scene (and possibly the code) to accommodate the requirements of the code