How to make a First-Person Character Controller jump?

Viewed 39

I'm making a simple FPS Character Controller, and I'm trying to make it jump, however anything I try or any tutorial I watch seems to fail. Nothing happens when I press space, and I've tried a number of different things.

EDIT: Using Unity documentation I changed the jump code to this:

if (Input.GetButtonDown("Jump") && controller.isGrounded)
{
    jumping = true;
    velocityY += Mathf.Sqrt(jumpForce * -3.0f * gravity);
}

if (controller.isGrounded && !jumping)
{
    velocityY = 0.0f;
}

Now when you press Space, nothing happens except you can no longer use WASD to move.

Here's my code:

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

public class PlayerController : MonoBehaviour
{
    [SerializeField] Transform playerCamera = null;
    [SerializeField] float mouseSensitivity = 3.5f;
    [SerializeField] float walkSpeed = 6.0f;
    [SerializeField] float gravity = 13.0f;
    [SerializeField] float jumpForce = 5.00f;
    [SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;

    [SerializeField] public bool lockCursor = true;

    float cameraPitch = 0.0f;
    float velocityY = 0.0f;
    bool jumpPressed = false;
    CharacterController controller = null;

    Vector2 currentMouseDelta = Vector2.zero;
    Vector2 currentMouseDeltaVelocity = Vector2.zero;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        if(lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    void Update()
    {
        UpdateMouseLook();
        UpdateMovement(); 
    }

    void UpdateMouseLook()
    {
        Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"),Input.GetAxis("Mouse Y"));

        currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);

        cameraPitch -= currentMouseDelta.y * mouseSensitivity;
        cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

        playerCamera.localEulerAngles = Vector3.right * cameraPitch;
        transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
    }

    void UpdateMovement()
    {
        Vector2 inputDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        inputDir.Normalize();

        //Jump code that doesn't work
        if (Input.GetKeyDown(KeyCode.Space))
        { 
            jumpPressed = true;
        }

        if (controller.isGrounded)
        {
            velocityY = 0.0f;
        }

        if (controller.isGrounded && jumpPressed)
        {
            velocityY = Mathf.Sqrt(jumpForce * gravity);
        }

        velocityY += gravity * Time.deltaTime;

        Vector3 velocity = (transform.forward * inputDir.y + transform.right * inputDir.x) * walkSpeed + Vector3.down * velocityY;

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

What I have here is just a version I made after a lot of other things didn't work. Any help?

0 Answers
Related