Search Unity

Putting everything in void Update() for a first person controller, how do I make movement smoother?

Discussion in 'Editor & General Support' started by jamiecropley, Aug 18, 2020.

  1. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    First my code is here:

    https://github.com/Some-T/FirstPers...aster/Assets/Scripts/FirstPersonController.cs

    and:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5. using Cursor = UnityEngine.Cursor;
    6.  
    7.  
    8.  
    9.  
    10. public class FirstPersonController : MonoBehaviour
    11. {
    12.     private float speed = 5;
    13.     private float jumpPower = 5;
    14.     Rigidbody rb;
    15.     CapsuleCollider col;
    16.     public Camera PlayerCamera;
    17.     public GameObject crossHair;
    18.     bool isActive;
    19.     float HorizontalInput;
    20.     float VerticalInput;
    21.     public GameObject mainPlayer;
    22.  
    23.  
    24.     void Start()
    25.     {
    26.         Cursor.visible = false;
    27.         Cursor.lockState = CursorLockMode.Locked;
    28.         rb = GetComponent<Rigidbody>();
    29.         col = GetComponent<CapsuleCollider>();
    30.         crossHair = GameObject.FindWithTag("CrossHair");
    31.         mainPlayer = GameObject.FindWithTag("Player");
    32.     }
    33.  
    34.  
    35.     void Update()
    36.     {
    37.         HorizontalInput = Input.GetAxisRaw("Horizontal");
    38.         VerticalInput = Input.GetAxisRaw("Vertical");
    39.         Vector3 xMovement = PlayerCamera.transform.right * HorizontalInput;
    40.         Vector3 zMovement = PlayerCamera.transform.forward * VerticalInput;
    41.         Vector3 velocity = (xMovement + zMovement).normalized * speed;
    42.         Vector3 forward = PlayerCamera.transform.forward;
    43.        
    44.         velocity.y = rb.velocity.y;
    45.         rb.velocity = velocity;
    46.         forward.y = 0;
    47.         forward.Normalize();
    48.         zMovement = forward * VerticalInput;
    49.  
    50.         if (isGrounded() && Input.GetButtonDown("Jump"))
    51.  
    52.         {
    53.             rb.velocity = Vector3.zero;
    54.             rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
    55.         }
    56.  
    57.  
    58.         if (Input.GetKeyDown("escape"))
    59.         {
    60.             Cursor.lockState = CursorLockMode.None;
    61.         }
    62.  
    63.         if (Input.GetButtonDown("Sprint"))
    64.         {
    65.             speed = 15;
    66.             Debug.Log("Speed is now: " + speed);
    67.         }
    68.  
    69.         if (Input.GetButtonUp("Sprint"))
    70.         {
    71.             speed = 5;
    72.             Debug.Log("Speed is now: " + speed);
    73.         }
    74.  
    75.         if (Input.GetKeyDown(KeyCode.LeftControl))
    76.         {
    77.             mainPlayer.transform.localScale = new Vector3(1.0f, 0.5f, 1.0f);
    78.             Debug.Log("Left control held down");
    79.         }
    80.  
    81.         if (Input.GetKeyUp(KeyCode.LeftControl))
    82.         {
    83.             mainPlayer.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    84.             Debug.Log("Left control no longer held down");
    85.         }
    86.  
    87.         if (Input.GetKeyDown(KeyCode.H))
    88.         {
    89.             isActive = !isActive;
    90.         }
    91.  
    92.         if (isActive)
    93.         {
    94.             crossHair.SetActive(true);
    95.         }
    96.         else
    97.         {
    98.             crossHair.SetActive(false);
    99.         }
    100.     }
    101.  
    102.     private bool isGrounded()
    103.     {
    104.         return Physics.Raycast(transform.position, Vector3.down, col.bounds.extents.y + 0.1f);
    105.     }
    106. }
    I don't know if I am being overly pedantic as the player moves fine, but is there anyway to make the movement / camera movement more smoother when the player moves in any given direction? Or have I done the best I can here? Looking for feedback on my code more than anything in this respect.
     
  2. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    315
    Instead of setting the speed, set the acceleration. Have a separate variable store the velocity. In the Update() function, use velocity = velocity * acceleration; Clamp the magnitude of the velocity between 0 and the max velocity.
     
  3. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    Thanks for the tips, can you be a bit more specific on which lines you are referring to please?