Search Unity

Question Problem with jumping FPS

Discussion in 'Scripting' started by Deleted User, May 31, 2023.

  1. Deleted User

    Deleted User

    Guest

    Hello! i am stuck on jumping and gravity, My jumping code breakes my gravity code mening when having my jumping function my gravity just shuts of and i stop falling. I can not jump either too?. I probely just missed something, All help is greatly appreciate!

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public float sprintSpeed = 3f;
    9.     public float normalSpeed = 1f;
    10.     private float moveSpeed;
    11.  
    12.     public float jumpHeight = 3f;
    13.     public float gravity = -10f;
    14.     private Vector3 velocity;
    15.     private bool grounded;
    16.  
    17.     public float sensitivity = 1f;
    18.  
    19.     private float verInput;
    20.     private float horInput;
    21.  
    22.     private float mouseX;
    23.     private float mouseY;
    24.     private float cameraClamp;
    25.  
    26.     public GameObject groundCheck;
    27.     public Camera playerCamera;
    28.     private Transform playerTransform;
    29.     private CharacterController controller;
    30.  
    31.     public LayerMask groundLayer;
    32.  
    33.     private void Awake()
    34.     {
    35.         controller = GetComponent<CharacterController>();
    36.         playerTransform = transform;
    37.  
    38.         Cursor.lockState = CursorLockMode.Locked;
    39.         Cursor.visible = false;
    40.     }
    41.  
    42.     private void Update()
    43.     {
    44.         grounded = Physics.CheckSphere(groundCheck.transform.position, 0.4f, groundLayer);
    45.         Move();
    46.         Camera();
    47.         Gravity();
    48.         Jump();
    49.     }
    50.    
    51.     private void Move()
    52.     {
    53.         horInput = Input.GetAxis("Horizontal");
    54.         verInput = Input.GetAxis("Vertical");
    55.  
    56.         Vector3 moveDirection = transform.forward * verInput + transform.right * horInput;
    57.  
    58.         moveSpeed = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed:normalSpeed;
    59.  
    60.         controller.Move(moveDirection * moveSpeed * Time.deltaTime);
    61.     }
    62.  
    63.     private void Camera()
    64.     {
    65.         mouseX += Input.GetAxis("Mouse X") * sensitivity;
    66.         mouseY -= Input.GetAxis("Mouse Y") * sensitivity;
    67.  
    68.         mouseY = Mathf.Clamp(mouseY, -80f, 80f);
    69.         playerCamera.transform.localEulerAngles = new Vector3(mouseY, 0f, 0f);
    70.         playerTransform.localEulerAngles = new Vector3(0f, mouseX, 0f);
    71.     }
    72.  
    73.     private void Gravity()
    74.     {
    75.         velocity.y += gravity * Time.deltaTime;
    76.  
    77.         if(grounded && velocity.y < 0f)
    78.         {
    79.             velocity.y = -1f;
    80.         }
    81.  
    82.         controller.Move(velocity * Time.deltaTime);
    83.     }
    84.  
    85.     private void Jump()
    86.     {
    87.         if(Input.GetKey(KeyCode.Space) && grounded)
    88.         {
    89.             velocity.y = Mathf.Sqrt(jumpHeight * 1f * gravity);
    90.         }
    91.     }
    92. }  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    The above script is (apparently) derived from defective Unity example code that calls .Move() twice per frame.

    This will cause you endless difficulty. I suggest you discard this script and instead try this:

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

    If you call .Move() twice in one single frame, the grounded check may fail.

    I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

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

    Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    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://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!