Search Unity

FPS Character Controller - Momentum Solution

Discussion in 'Physics' started by BigBadSlug, May 20, 2020.

  1. BigBadSlug

    BigBadSlug

    Joined:
    Oct 15, 2019
    Posts:
    4
    Hey everyone,

    I'm posting this in hopes of helping someone out there struggling with this problem like I was. Character Controller momentum, while maintaining some degree of air control.

    I've spent so long trying to get this feeling good, scouring Unity answers for solutions but I've found next to nothing. I posted a half solution (Not really, wasn't a nice feeling controller) in 2019 but after playing with it more during quarantine I've found something that I feel happy with and I'd like to share with you all.

    This is absolutely not the best way to do this, if anyone has criticisms please by all means fire away, I'd love to improve this.

    Here's the Character Controller code, note the initial controller was made with help from Brackey's FPS controller tutorial -
    :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. [RequireComponent( typeof( CharacterController ) )]
    5. public class Slug_Controller : MonoBehaviour
    6. {
    7.     /*Private variables*/
    8.     CharacterController _playerController;
    9.     Vector3 _velocity;
    10.     Vector3 input;
    11.     Vector3 move = Vector3.zero;
    12.     float speed;
    13.     bool isGrounded;
    14.  
    15.     /*Public variables*/
    16.     public Transform groundCheck;
    17.     public float groundDistance = 0.4f;
    18.     public LayerMask groundMask;
    19.  
    20.     public float walkSpeed = 8f;
    21.     public float runSpeed = 12f;
    22.     public float gravity = -9.81f;
    23.     public float jumpHeight = 3f;
    24.     public float airSpeed = 6f;
    25.  
    26.  
    27.     private void Awake() {
    28.         _playerController = GetComponent<CharacterController>();
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         isGrounded = Physics.CheckSphere( groundCheck.position, groundDistance, groundMask );
    34.        
    35.         if( isGrounded && _velocity.y < 0f ) { _velocity.y = -2f; }
    36.         input = new Vector3( Input.GetAxisRaw( "Horizontal" ), 0f, Input.GetAxisRaw( "Vertical" ) );
    37.         input = transform.TransformDirection( input );
    38.         input = Vector3.ClampMagnitude( input, 1f );
    39.  
    40.         ProcessMovement();
    41.        
    42.         HandleJump();
    43.  
    44.         _playerController.Move( move * Time.deltaTime );
    45.  
    46.         ApplyGravity();
    47.     }
    48.  
    49.     void ProcessMovement() {
    50.         if ( isGrounded ) {
    51.             speed = Input.GetKey( KeyCode.LeftShift ) ? runSpeed : walkSpeed;
    52.             if ( input.x != 0 ) {
    53.                 move.x += input.x * speed;
    54.             } else {
    55.                 move.x = 0;
    56.             }
    57.             if ( input.z != 0 ) {
    58.                 move.z += input.z * speed;
    59.             } else {
    60.                 move.z = 0;
    61.             }
    62.  
    63.         } else {
    64.             move.x += input.x * airSpeed;
    65.             move.z += input.z * airSpeed;
    66.         }
    67.         move = Vector3.ClampMagnitude( move, speed );
    68.     }
    69.  
    70.     void HandleJump() {
    71.         if ( Input.GetButtonDown( "Jump" ) && isGrounded ) { _velocity.y = Mathf.Sqrt( jumpHeight * -2f * gravity ); }
    72.     }
    73.  
    74.     void ApplyGravity() {
    75.         _velocity.y += gravity * Time.deltaTime;
    76.         _playerController.Move( _velocity * Time.deltaTime );
    77.     }
    78. }
    79.  
    I hope this helps someone,

    Cheers!
     
    Flowyan likes this.
  2. EasilyDistracted

    EasilyDistracted

    Joined:
    Mar 20, 2021
    Posts:
    1
    This is exactly what I was looking for Thanks : )
     
  3. gelistiricihesabiyiitalim

    gelistiricihesabiyiitalim

    Joined:
    Feb 19, 2022
    Posts:
    1
    my life has been complete with this thank you so much bro but uhh you know the drill; hippidy hoppidy your code is now my property :D