Search code examples
unity-game-enginecontrolsframe-rate

unity fps jump with character controller


I'm pretty new to unity and wanted to create a fps movement. I found this tutorial from Brackeys and it works relatively well but if you jump with a ceiling over your head you're flying a few moments. I'm very sorry to ask such a basic question but I couldn't find anything. Here some code:

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

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;
    Animator animator;

    public float speed = 7f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (x != 0)
        {
            animator.SetBool("WalkXTrigger", true);
            x /= 2;
        }
        else
        {
            animator.SetBool("WalkXTrigger", false);
        }

        if (z != 0)
        {
            if (z<0)
            {
                animator.SetBool("WalkBackTrigger", true);
                z /= 1.5f;
            }
            if (z>0)
            {
                animator.SetBool("WalkTrigger", true);
            }
        }
        else
        {
            animator.SetBool("WalkTrigger", false);
            animator.SetBool("WalkBackTrigger", false);
        }

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

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

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
            animator.SetBool("JumpTrigger", true);
        }

        else
        {
            animator.SetBool("JumpTrigger", false);
        }

        velocity.y += gravity * Time.deltaTime;

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

So I think there are 2 options: 1. move the character down when he hits the ceiling 2. measure the space between character and ceiling and set it equal to the jump height My Problem: I don't know how to do any of this Hope my English is okay and the problem is described enough Thanks for your help


Solution

  • I'm not 100% sure whether your character keeps flying, or just stays there for a second or two, but either way, this little bit of code will work.

    public GameObject ceiling; //set this to your ceiling in the editor.
    public float ceilingDown = 0.5; //if this value doesn't work, just mess around with it a little.
    
    void OnTriggerEnter(ceiling){
        velocity.y -= ceilingDown;
    }
    

    If you add this to your code, it should force the player downward slightly. Gravity will take over after this.