Search Unity

Help Needed with FPS Character Controller

Discussion in 'Scripting' started by ethanseow, Aug 17, 2019.

  1. ethanseow

    ethanseow

    Joined:
    May 28, 2019
    Posts:
    1
    Hi, I've been having trouble lately with jumping and the other controls of my character controller in Unity. And a list of problems came up, all countering each other

    1. When using PlayerMovement in FixedUpdate, the FPS camera begins to stutter, regardless if the rotation/movement script of the camera is in FixedUpdate or Update. I couldn't find a reliable way to get rid of the stutter without decreasing the timesteps
    2. When placing PlayerMovement in Update, the jump height of the character becomes inconsistent as PlayerMovement is being called a different amount of times every second, so on one jump could reach a different height than other jumps, regardless of Time.deltaTime
    3. SetGravity must be placed in FixedUpdate for the similar reason as 2, because it is going to be called at different rates every second.

    I found out the heights of the jump through the variable highestHeight which prints the highestHeight of the current jump

    I would be really grateful if someone could give me leads on how to fix this without changing timesteps because I heard it takes away performance

    if you would like to try this script out, its just a camera placed on a capsule with their respective scripts
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private CharacterController charController;
    8.     private float vertInput;
    9.     private float horiInput;
    10.  
    11.     private Vector3 forwardMovement;
    12.     private Vector3 rightMovement;
    13.     private Vector3 vectorSpeed;
    14.  
    15.     private bool isJumping = false;
    16.     public float jumpHeight = 30f;
    17.     private Vector3 refCurrentVeloc;
    18.     private float refCurrentJump;
    19.     public float movementSpeed;
    20.     public float upwardsVeloc = 0;
    21.     public float gravity = 2f;
    22.     private float highestHeight;
    23.     private float prevUpwardsVeloc;
    24.     public float smoothTime = 0.02f;
    25.     private void Start()
    26.     {
    27.         charController = GetComponent<CharacterController>();
    28.     }
    29.     private void Update()
    30.     {
    31.         if (Input.GetKey(KeyCode.Space) && !isJumping && charController.isGrounded)
    32.         {
    33.             Jump();
    34.         }
    35.         else
    36.         {
    37.             SetGravity();
    38.         }
    39.         PlayerMove();
    40.         if (isJumping)
    41.         {
    42.             if (transform.position.y > highestHeight)
    43.             {
    44.                 highestHeight = transform.position.y;
    45.             }
    46.         }
    47.         Debug.Log(highestHeight);
    48.  
    49.     }
    50.     private void PlayerMove()
    51.     {
    52.         vertInput = Input.GetAxis("Vertical");
    53.         horiInput = Input.GetAxis("Horizontal");
    54.         forwardMovement = vertInput * transform.forward;
    55.         rightMovement = horiInput * transform.right;
    56.         //vectorSpeed = Vector3.SmoothDamp(vectorSpeed,Vector3.ClampMagnitude(rightMovement + forwardMovement, 1f) * movementSpeed,ref refCurrentVeloc, smoothTime);
    57.         vectorSpeed = Vector3.ClampMagnitude(rightMovement + forwardMovement, 1f) * movementSpeed;
    58.         vectorSpeed.y = upwardsVeloc;
    59.         charController.Move(vectorSpeed * Time.deltaTime);
    60.  
    61.     }
    62.  
    63.     private void Jump()
    64.     {
    65.         isJumping = true;
    66.         upwardsVeloc = jumpHeight;
    67.     }
    68.  
    69.     private void SetGravity()
    70.     {
    71.         if (charController.isGrounded)
    72.         {
    73.             upwardsVeloc = -gravity;
    74.             isJumping = false;
    75.             highestHeight = 0;
    76.         }
    77.         else
    78.         {
    79.             upwardsVeloc -= gravity;
    80.         }
    81.     }
    82. }
    Code (CSharp):
    1. public class Camera : MonoBehaviour
    2. {
    3.     public Transform armsPivotTransform;
    4.  
    5.     private float refVelocX;
    6.     private float refVelocY;
    7.     public float mouseSensitivity;
    8.  
    9.     private float amtRotatedY = 0;
    10.     private float rotX;
    11.     private float rotY;
    12.     void Start()
    13.     {
    14.         Cursor.lockState = CursorLockMode.Locked;
    15.         Cursor.visible = false;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         rotX = Mathf.SmoothDamp(rotX,Input.GetAxis("Mouse X")  * mouseSensitivity * Time.deltaTime, ref refVelocX, 0.02f);
    21.         rotY = Mathf.SmoothDamp(rotY,- Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime, ref refVelocY, 0.02f);
    22.         //rotX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    23.         //rotY = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    24.         amtRotatedY += rotY;
    25.         if(amtRotatedY > 90)
    26.         {
    27.             ClampYRot(90);
    28.             rotY = 0;
    29.         }
    30.         else if(amtRotatedY < -90)
    31.         {
    32.             ClampYRot(-90);
    33.             rotY = 0;
    34.         }
    35.         transform.Rotate(new Vector3(rotY, 0, 0));
    36.         armsPivotTransform.Rotate(new Vector3(rotY, 0, 0));
    37.         transform.parent.Rotate(new Vector3(0, rotX, 0));
    38.     }
    39.  
    40.     private void ClampYRot(float rotClampValue)
    41.     {
    42.         transform.eulerAngles = new Vector3(rotClampValue, transform.eulerAngles.y, transform.eulerAngles.z);
    43.         armsPivotTransform.eulerAngles = new Vector3(rotClampValue, armsPivotTransform.eulerAngles.y, armsPivotTransform.eulerAngles.z);
    44.     }
    45. }
    46.  
     
    Last edited: Aug 17, 2019