Search Unity

A good First Person Jumping System

Discussion in 'Getting Started' started by Flulouch_, Mar 16, 2020.

  1. Flulouch_

    Flulouch_

    Joined:
    Jan 31, 2020
    Posts:
    1
    I am very new and I've made a few attempts at making a FPS character controller. Each time I've been stumped by jumping, watched a lot of tutorials and all of them seam to have this same issue. The jump is inconsistent, it wont work every time the button is pressed and it wont be a consistent amount of force applied even when though its looking for when the button is pressed down. This has really baffled me, so if you could point me in the direction of a tutorial that works for you or help with code.

    Code (CSharp):
    1. public class ControlInput : MonoBehaviour
    2. {
    3.  
    4.     public CharacterController playerObj;
    5.  
    6.     #region PUBLIC FIELDS
    7.     [Header(header: "PlayerMovement")]
    8.     public float playerSpeed = 12f;
    9.     public float JumpForce = 10;
    10.     public float maxChangeInVerticalVelocity = 100;
    11.  
    12.     [Header(header: "Player Environments")]
    13.     public float gravity = -9.81f;
    14.  
    15.     [Header(header: "Leaning While Running")]
    16.     public Transform _Pivot;
    17.     public float RunningLeanspeed = 10f;
    18.     public float RunningLeanmaxAngle = 2f;
    19.     #endregion
    20.  
    21.     float curAngle = 0f;
    22.  
    23.     Vector3 velocity;
    24.     int jumpCount = 0;
    25.  
    26.     // Update is called once per frame
    27.  
    28.     void Awake()
    29.     {
    30.         if (_Pivot == null && transform.parent != null) _Pivot = transform.parent;
    31.     }
    32.     void Update()
    33.     {
    34.         float x = Input.GetAxis("Horizontal");
    35.         float z = Input.GetAxis("Vertical");
    36.  
    37.         Vector3 move = transform.right * x + transform.forward * z;
    38.  
    39.  
    40.        
    41.  
    42.         //RUNNING LEAN
    43.         if (Input.GetKey(KeyCode.A))
    44.         {
    45.             curAngle = Mathf.MoveTowardsAngle(curAngle, RunningLeanmaxAngle, RunningLeanspeed * Time.deltaTime);
    46.         }
    47.         else if (Input.GetKey(KeyCode.D))
    48.         {
    49.             curAngle = Mathf.MoveTowardsAngle(curAngle, -RunningLeanmaxAngle, RunningLeanspeed * Time.deltaTime);
    50.         }
    51.         else
    52.         {
    53.             curAngle = Mathf.MoveTowardsAngle(curAngle, 0f, RunningLeanspeed * Time.deltaTime);
    54.         }
    55.  
    56.         _Pivot.transform.localRotation = _Pivot.transform.localRotation * Quaternion.AngleAxis(curAngle, Vector3.forward);
    57.         curAngle = Mathf.Clamp(curAngle, -5f, 5f);
    58.  
    59.         //JUMPING
    60.         if (Input.GetKeyDown(KeyCode.Space))
    61.         {
    62.             velocity.y += JumpForce;
    63.         }
    64.         else
    65.         {
    66.             velocity.y += gravity * Time.deltaTime;
    67.         }
    68.  
    69.      
    70.  
    71.         //EDITING THE ACTUAL PLAYER
    72.  
    73.         playerObj.Move(move * playerSpeed * Time.deltaTime);
    74.         playerObj.Move(velocity * Time.deltaTime);
    75.  
    76.  
    77.     }
    78. }
    79.  
    Cheers for anyhelp