Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question First Person Controller - deceleration

Discussion in 'Scripting' started by foxyyhappyw, Jul 6, 2023.

  1. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    hey, how to add deceleration to this code?
    I would like the character to gradually slow down after releasing the movement keys.

    This script is from this unity package: https://assetstore.unity.com/packag...-first-person-character-controller-urp-196525

    Code (CSharp):
    1. using UnityEngine;
    2. #if ENABLE_INPUT_SYSTEM
    3. using UnityEngine.InputSystem;
    4. #endif
    5.  
    6. namespace StarterAssets
    7. {
    8.     [RequireComponent(typeof(CharacterController))]
    9. #if ENABLE_INPUT_SYSTEM
    10.     [RequireComponent(typeof(PlayerInput))]
    11. #endif
    12.     public class FirstPersonController : MonoBehaviour
    13.     {
    14.         [Header("Player")]
    15.         [Tooltip("Move speed of the character in m/s")]
    16.         public float MoveSpeed = 4.0f;
    17.         [Tooltip("Sprint speed of the character in m/s")]
    18.         public float SprintSpeed = 6.0f;
    19.         [Tooltip("Rotation speed of the character")]
    20.         public float RotationSpeed = 1.0f;
    21.         [Tooltip("Acceleration and deceleration")]
    22.         public float SpeedChangeRate = 10.0f;
    23.  
    24.         [Space(10)]
    25.         public bool enableJumping;
    26.         [Tooltip("The height the player can jump")]
    27.         public float JumpHeight = 1.2f;
    28.         [Tooltip("The character uses its own gravity value. The engine default is -9.81f")]
    29.         public float Gravity = -15.0f;
    30.  
    31.         [Space(10)]
    32.         [Tooltip("Time required to pass before being able to jump again. Set to 0f to instantly jump again")]
    33.         public float JumpTimeout = 0.1f;
    34.         [Tooltip("Time required to pass before entering the fall state. Useful for walking down stairs")]
    35.         public float FallTimeout = 0.15f;
    36.  
    37.         [Header("Player Grounded")]
    38.         [Tooltip("If the character is grounded or not. Not part of the CharacterController built in grounded check")]
    39.         public bool Grounded = true;
    40.         [Tooltip("Useful for rough ground")]
    41.         public float GroundedOffset = -0.14f;
    42.         [Tooltip("The radius of the grounded check. Should match the radius of the CharacterController")]
    43.         public float GroundedRadius = 0.5f;
    44.         [Tooltip("What layers the character uses as ground")]
    45.         public LayerMask GroundLayers;
    46.  
    47.         [Header("Cinemachine")]
    48.         [Tooltip("The follow target set in the Cinemachine Virtual Camera that the camera will follow")]
    49.         public GameObject CinemachineCameraTarget;
    50.         [Tooltip("How far in degrees can you move the camera up")]
    51.         public float TopClamp = 90.0f;
    52.         [Tooltip("How far in degrees can you move the camera down")]
    53.         public float BottomClamp = -90.0f;
    54.  
    55.         //test
    56.         public float testvalue;
    57.  
    58.         // cinemachine
    59.         private float _cinemachineTargetPitch;
    60.  
    61.         // player
    62.         private float _speed;
    63.         private float _rotationVelocity;
    64.         private float _verticalVelocity;
    65.         private float _terminalVelocity = 53.0f;
    66.  
    67.         // timeout deltatime
    68.         private float _jumpTimeoutDelta;
    69.         private float _fallTimeoutDelta;
    70.  
    71.    
    72. #if ENABLE_INPUT_SYSTEM
    73.         private PlayerInput _playerInput;
    74. #endif
    75.         private CharacterController _controller;
    76.         private StarterAssetsInputs _input;
    77.         private GameObject _mainCamera;
    78.  
    79.         //private const float _threshold = 0.01f;
    80.  
    81.         private bool IsCurrentDeviceMouse
    82.         {
    83.             get
    84.             {
    85.                 #if ENABLE_INPUT_SYSTEM
    86.                 return _playerInput.currentControlScheme == "KeyboardMouse";
    87.                 #else
    88.                 return false;
    89.                 #endif
    90.             }
    91.         }
    92.  
    93.         private void Awake()
    94.         {
    95.             // get a reference to our main camera
    96.             if (_mainCamera == null)
    97.             {
    98.                 _mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
    99.             }
    100.         }
    101.  
    102.         private void Start()
    103.         {
    104.             _controller = GetComponent<CharacterController>();
    105.             _input = GetComponent<StarterAssetsInputs>();
    106. #if ENABLE_INPUT_SYSTEM
    107.             _playerInput = GetComponent<PlayerInput>();
    108. #else
    109.             Debug.LogError( "Starter Assets package is missing dependencies. Please use Tools/Starter Assets/Reinstall Dependencies to fix it");
    110. #endif
    111.  
    112.             // reset our timeouts on start
    113.             _jumpTimeoutDelta = JumpTimeout;
    114.             _fallTimeoutDelta = FallTimeout;
    115.         }
    116.  
    117.         private void Update()
    118.         {
    119.             JumpAndGravity();
    120.             GroundedCheck();
    121.             Move();
    122.         }
    123.  
    124.         private void LateUpdate()
    125.         {
    126.             CameraRotation();
    127.         }
    128.  
    129.         private void GroundedCheck()
    130.         {
    131.             // set sphere position, with offset
    132.             Vector3 spherePosition = new Vector3(transform.position.x, transform.position.y - GroundedOffset, transform.position.z);
    133.             Grounded = Physics.CheckSphere(spherePosition, GroundedRadius, GroundLayers, QueryTriggerInteraction.Ignore);
    134.         }
    135.  
    136.         private void CameraRotation()
    137.         {
    138.             // if there is an input
    139.             //if (_input.look.sqrMagnitude >= _threshold)
    140.             //{
    141.                 //Don't multiply mouse input by Time.deltaTime
    142.                 float deltaTimeMultiplier = IsCurrentDeviceMouse ? 1.0f : Time.deltaTime;
    143.                
    144.                 _cinemachineTargetPitch += _input.look.y * RotationSpeed * deltaTimeMultiplier;
    145.                 _rotationVelocity = _input.look.x * RotationSpeed * deltaTimeMultiplier;
    146.  
    147.                 // clamp our pitch rotation
    148.                 _cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);
    149.  
    150.                 // Update Cinemachine camera target pitch
    151.                 CinemachineCameraTarget.transform.localRotation = Quaternion.Euler(_cinemachineTargetPitch, 0.0f, 0.0f);
    152.  
    153.                 // rotate the player left and right
    154.                 transform.Rotate(Vector3.up * _rotationVelocity);
    155.             //}
    156.         }
    157.  
    158.         private void Move()
    159.         {
    160.             // set target speed based on move speed, sprint speed and if sprint is pressed
    161.             float targetSpeed = _input.sprint ? SprintSpeed : MoveSpeed;
    162.  
    163.             // a simplistic acceleration and deceleration designed to be easy to remove, replace, or iterate upon
    164.  
    165.             // note: Vector2's == operator uses approximation so is not floating point error prone, and is cheaper than magnitude
    166.             // if there is no input, gradually slow down the target speed to 0
    167.  
    168.             if (_input.move == Vector2.zero)
    169.             {
    170.                 targetSpeed = 0.0f;
    171.             }
    172.  
    173.             // a reference to the players current horizontal velocity
    174.             float currentHorizontalSpeed = new Vector3(_controller.velocity.x, 0.0f, _controller.velocity.z).magnitude;
    175.  
    176.             float speedOffset = 0.1f;
    177.             float inputMagnitude = _input.analogMovement ? _input.move.magnitude : 1f;
    178.  
    179.             if (currentHorizontalSpeed < targetSpeed - speedOffset || currentHorizontalSpeed > targetSpeed + speedOffset)
    180.             {
    181.                 // creates curved result rather than a linear one giving a more organic speed change
    182.                 // note T in Lerp is clamped, so we don't need to clamp our speed
    183.                 _speed = Mathf.Lerp(currentHorizontalSpeed, targetSpeed * inputMagnitude, Time.deltaTime * SpeedChangeRate);
    184.  
    185.                 // round speed to 3 decimal places
    186.                 _speed = Mathf.Round(_speed * 1000f) / 1000f;
    187.             }
    188.             else
    189.             {
    190.                 _speed = targetSpeed;
    191.             }
    192.  
    193.             // normalise input direction
    194.             Vector3 inputDirection = new Vector3(_input.move.x, 0.0f, _input.move.y).normalized;
    195.  
    196.             // note: Vector2's != operator uses approximation so is not floating point error prone, and is cheaper than magnitude
    197.             // if there is a move input rotate player when the player is moving
    198.             if (_input.move != Vector2.zero)
    199.             {
    200.                 // move
    201.                 inputDirection = transform.right * _input.move.x + transform.forward * _input.move.y;
    202.             }
    203.  
    204.             // move the player
    205.             _controller.Move(inputDirection.normalized * (_speed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime);
    206.  
    207.             //
    208.             Debug.Log("iD: " + inputDirection + " | current: " + currentHorizontalSpeed);
    209.         }
    210.  
    211.         private void JumpAndGravity()
    212.         {
    213.             if (Grounded)
    214.             {
    215.                 // reset the fall timeout timer
    216.                 _fallTimeoutDelta = FallTimeout;
    217.  
    218.                 // stop our velocity dropping infinitely when grounded
    219.                 if (_verticalVelocity < 0.0f)
    220.                 {
    221.                     _verticalVelocity = -2f;
    222.                 }
    223.  
    224.                 // Jump
    225.                 if (enableJumping)
    226.                 {
    227.                     if (_input.jump && _jumpTimeoutDelta <= 0.0f)
    228.                     {
    229.                         // the square root of H * -2 * G = how much velocity needed to reach desired height
    230.                         _verticalVelocity = Mathf.Sqrt(JumpHeight * -2f * Gravity);
    231.                     }
    232.                 }
    233.  
    234.                 // jump timeout
    235.                 if (_jumpTimeoutDelta >= 0.0f)
    236.                 {
    237.                     _jumpTimeoutDelta -= Time.deltaTime;
    238.                 }
    239.             }
    240.             else
    241.             {
    242.                 // reset the jump timeout timer
    243.                 _jumpTimeoutDelta = JumpTimeout;
    244.  
    245.                 // fall timeout
    246.                 if (_fallTimeoutDelta >= 0.0f)
    247.                 {
    248.                     _fallTimeoutDelta -= Time.deltaTime;
    249.                 }
    250.  
    251.                 // if we are not grounded, do not jump
    252.                 _input.jump = false;
    253.             }
    254.  
    255.             // apply gravity over time if under terminal (multiply by delta time twice to linearly speed up over time)
    256.             if (_verticalVelocity < _terminalVelocity)
    257.             {
    258.                 _verticalVelocity += Gravity * Time.deltaTime;
    259.             }
    260.         }
    261.  
    262.         private static float ClampAngle(float lfAngle, float lfMin, float lfMax)
    263.         {
    264.             if (lfAngle < -360f) lfAngle += 360f;
    265.             if (lfAngle > 360f) lfAngle -= 360f;
    266.             return Mathf.Clamp(lfAngle, lfMin, lfMax);
    267.         }
    268.  
    269.         private void OnDrawGizmosSelected()
    270.         {
    271.             Color transparentGreen = new Color(0.0f, 1.0f, 0.0f, 0.35f);
    272.             Color transparentRed = new Color(1.0f, 0.0f, 0.0f, 0.35f);
    273.  
    274.             if (Grounded) Gizmos.color = transparentGreen;
    275.             else Gizmos.color = transparentRed;
    276.  
    277.             // when selected, draw a gizmo in the position of, and matching radius of, the grounded collider
    278.             Gizmos.DrawSphere(new Vector3(transform.position.x, transform.position.y - GroundedOffset, transform.position.z), GroundedRadius);
    279.         }
    280.     }
    281. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    If you just want a Basic First Person controller, here's one:

    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!!

    (It relies on the natural smoothing in the Input.GetAxis() rules however)
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    On line 170.

    Change:
    targetSpeed=0;
    To:
    targetSpeed*=0.9f;
     
  4. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    i tried something similar earlier, not working ;/
     
  5. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    thanks, i will try that
     
  6. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    I still dont know how to do it ;/
    I tried to change the code in Move(), but there is still some problems.

    Code (CSharp):
    1. // normalise input direction
    2.  
    3.             var move = _input.move;
    4.  
    5.             desiredQuantityX = _input.move.x;
    6.             currentQuantityX = Mathf.Lerp(currentQuantityX, desiredQuantityX, 3 * Time.deltaTime);
    7.  
    8.             desiredQuantityY = _input.move.y;
    9.             currentQuantityY = Mathf.Lerp(currentQuantityY, desiredQuantityY, 3 * Time.deltaTime);
    10.  
    11.  
    12.             Vector3 inputDirection = new Vector3(currentQuantityX, 0.0f, currentQuantityY).normalized;
    13.  
    14.             // note: Vector2's != operator uses approximation so is not floating point error prone, and is cheaper than magnitude
    15.             // if there is a move input rotate player when the player is moving
    16.             if (move != Vector2.zero)
    17.             {
    18.                 // move
    19.                 inputDirection = transform.right * desiredQuantityX + transform.forward * desiredQuantityY;
    20.             }
    21.  
    22.             // move the player
    23.             _controller.Move(inputDirection.normalized * (_speed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime);
    24.  
    25.  
    26.             //Debug.Log("current: " + currentHorizontalSpeed + " | target: " + targetSpeed + " | speed: " + _speed + " | input: " + inputDirection.normalized);
    27.             Debug.Log("desiredX: " + desiredQuantityX + " | currentX: " + currentQuantityX +
    28.                 " | desiredY: " + desiredQuantityY + " | currentY: " + currentQuantityY);
     
  7. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    the problem is "inputDirection", because when the player releases all keys, then its value is set to 0 and multiplied in "_controller.Move" with speed immediately gives 0, not a smoothly changing value.

    but i dont how to fix this
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Line 19 is where you "consume" the inputs by putting it into inputDirection. You should be using current, not desired.

    Line 12 should probably just set inputDirection to Vector3.zero.
     
  9. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    i already tested setting "current" instead of "desired", but then the problem is that the value of "current" doesn't change from 0 to e.g. 1 only from e.g. -0.3 to 1 in mathf. And then it gives the effect of sliding on the floor.

    and if I set "Vector3.zero" then the deceleration effect doesn't work at all
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Well looking at line 23, everything you have done above it is made pointless because you .normalize the vector. :)

    So... don't do that. Normalizing forces the vector length to 1.0

    Thats why horribly long lines of code are bad. Do one computation per line.
     
  11. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59

    I added this code and it seems to work:

    Code (CSharp):
    1.  
    2.             if (_input.move == Vector2.zero && currentHorizontalSpeed > 0)
    3.             {
    4.                 float decelerationRate = 0.1f;
    5.                 _speed -= decelerationRate * Time.deltaTime;
    6.                 _speed = Mathf.Max(_speed, 0);
    7.  
    8.                 inputDirection = _controller.velocity.normalized;
    9.             }
    so the complete Move() code looks like this:

    Code (CSharp):
    1.         private void Move2()
    2.         {
    3.             float targetSpeed = _input.sprint ? SprintSpeed : MoveSpeed;
    4.  
    5.             if (_input.move == Vector2.zero)
    6.             {
    7.                 targetSpeed = 0.0f;
    8.             }
    9.  
    10.             float currentHorizontalSpeed = new Vector3(_controller.velocity.x, 0.0f, _controller.velocity.z).magnitude;
    11.             float speedOffset = 0.1f;
    12.  
    13.             if (currentHorizontalSpeed < targetSpeed - speedOffset || currentHorizontalSpeed > targetSpeed + speedOffset)
    14.             {
    15.                 _speed = Mathf.Lerp(currentHorizontalSpeed, targetSpeed, Time.deltaTime * SpeedChangeRate);
    16.                 _speed = Mathf.Round(_speed * 1000f) / 1000f;
    17.             }
    18.             else
    19.             {
    20.                 _speed = targetSpeed;
    21.             }
    22.  
    23.             Vector3 inputDirection = new Vector3(_input.move.x, 0.0f, _input.move.y).normalized;
    24.  
    25.             if (_input.move != Vector2.zero)
    26.             {
    27.                 inputDirection = transform.right * _input.move.x + transform.forward * _input.move.y;
    28.             }
    29.  
    30.             if (_input.move == Vector2.zero && currentHorizontalSpeed > 0)
    31.             {
    32.                 float decelerationRate = 0.1f;
    33.                 _speed -= decelerationRate * Time.deltaTime;
    34.                 _speed = Mathf.Max(_speed, 0);
    35.  
    36.                 inputDirection = _controller.velocity.normalized;
    37.             }
    38.  
    39.             _controller.Move(inputDirection.normalized * (_speed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime);
    40.         }
    it is okay? can anything be done better?
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    If it works, it works, move on!!

    If you want to improve yourself, go and do Step #2 below and challenge yourself to understand what each part of your code does.

    -----------------------

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
    foxyyhappyw likes this.