Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Change and Track Movement Speed

Discussion in 'Scripting' started by FreepZed, Dec 3, 2023.

  1. FreepZed

    FreepZed

    Joined:
    Dec 3, 2023
    Posts:
    2
    Hey, I recently got back into coding after a while as apparent by all the comments. In this movement script everything works fine, but I am trying to find a way to correlate the field of view with the speed of the player. That also means that I am trying to add a gradual acceleration when sprinting instead of the instant transition. I feel I could add the gradual acceleration if given a way to track the true movement speed of the player, as the “speed” value does not change when sprinting. Anything helps, thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. public class playerMovement : MonoBehaviour
    7. {
    8.     public CharacterController controller;
    9.  
    10.     //General values
    11.     public float speed = 12f;
    12.     public float gravity = -9.81f;
    13.     public float jumpHeight = 3f;
    14.     public float sprintMultiplier = 2;
    15.  
    16.     //Ground Check
    17.     public Transform groundCheck;
    18.     public float groundDistance = 0.4f;
    19.     public LayerMask groundMask;
    20.  
    21.     Vector3 velocity;
    22.     bool isGrounded;
    23.  
    24.     //booligans
    25.     public bool isSprinting;
    26.  
    27.     //camera
    28.     public Camera playerCam;
    29.  
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.  
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         Cursor.visible = false;
    40.         Cursor.lockState = CursorLockMode.Locked;
    41.  
    42.         //More ground check, heavy & can't jump if you dont write what the ground is :)
    43.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    44.  
    45.         if(isGrounded && velocity.y < 0)
    46.         {
    47.             velocity.y = -2f;
    48.         }
    49.  
    50.         //Jump function
    51.         if(Input.GetButtonDown("Jump") && isGrounded)
    52.         {
    53.             velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    54.         }
    55.  
    56.         //Sprint function
    57.         if(Input.GetKey(KeyCode.LeftShift))
    58.         {
    59.             isSprinting = true;
    60.         }
    61.         else
    62.         {
    63.             isSprinting = false;
    64.         }
    65.  
    66.         //Checks if you're pressing WASD
    67.         float x = Input.GetAxis("Horizontal");
    68.         float z = Input.GetAxis("Vertical");
    69.  
    70.         //Changes the value of your position/"move value" corresponding to your WASD input
    71.         Vector3 move = transform.right * x + transform.forward * z;
    72.  
    73.         //important to put all "If True" checks after this^ as the game will get confused if you try to edit a value that doesn't exist yet but also early enough to plug into future equations :)
    74.         if(isSprinting == true)
    75.         {
    76.            move *= sprintMultiplier;
    77.         }
    78.  
    79.         //moves the player controller according to your speed and "move value"
    80.         controller.Move(move * speed * Time.deltaTime);
    81.  
    82.         //downwards velocity is increased by gravity (Literally the line that adds gravity) ALSO THE LINE BELOW, FIGURE OUT WHY??
    83.         velocity.y += gravity * Time.deltaTime;
    84.        
    85.         controller.Move(velocity * Time.deltaTime);
    86.  
    87.     }
    88. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,312
    You need to smoothly change your speed quantity rather than all at once.

    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




    HOWEVER! Throw the above code away now. It calls .Move() twice in one frame and is defective.

    CharacterController CharMover broken:

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