Search Unity

(Closed) Mouse delta , fps independent

Discussion in 'Input System' started by GameMadeByGamers, Jul 22, 2019.

  1. GameMadeByGamers

    GameMadeByGamers

    Joined:
    Nov 25, 2014
    Posts:
    54
    Hi there! Got a problem when activating vsync the character and camera often over shot probably due to the rotation not being fps independent, using the legacy input system dont cause this problem so i suppose it the way i use the new system that is wrong.

    - The FixedTimestep is set to 1/60
    - I dont use Time.FixedDeltaTime or Time.DeltaTime because i use smoothDamp

    RotatorInput class :
    Code (csharp):
    1.  
    2. using Sirenix.OdinInspector;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [System.Serializable]
    8. public class RotatorInputs : IVector2Provider
    9. {
    10.     private CameraRotator cameraRotator;
    11.     private CharacterRotator characterRotator;
    12.  
    13.     [SerializeField, Range(0,1f)] private float xAxisSensitivity = 0.5f;
    14.     [SerializeField, Range(0, 1f)] private float yAxisSensitivity = 0.5f;
    15.  
    16.     [SerializeField, ReadOnly] Vector2 mouseDelta;
    17.     public void Ini(PlayerInputs playerInputs , CameraRotator cameraRotator , CharacterRotator characterRotator)
    18.     {
    19.         this.cameraRotator = cameraRotator;
    20.         this.characterRotator = characterRotator;
    21.  
    22.         cameraRotator.axisProvider = this;
    23.         characterRotator.axisProvider = this;
    24.  
    25.         Cursor.lockState = CursorLockMode.Locked;
    26.         Cursor.visible = false;
    27.  
    28.         //TO FIX using the new inputs system has weird behavior on low fps (not fps independant)
    29.         playerInputs.CameraAndRotation.MouseDelta.performed += context => mouseDelta = new Vector2(context.ReadValue<Vector2>().x * xAxisSensitivity, context.ReadValue<Vector2>().y * yAxisSensitivity);
    30.         playerInputs.CameraAndRotation.MouseDelta.canceled += context => mouseDelta = Vector2.zero;
    31.     }
    32.  
    33.  
    34.     public Vector2 GetVector2()
    35.     {
    36.         //Using the legacy fix the problem
    37.         //mouseDelta = new Vector2(Input.GetAxis("MouseX") * xAxisSensitivity, Input.GetAxis("MouseY") * yAxisSensitivity);
    38.         return mouseDelta;
    39.     }
    40. }
    41.  
    42.  
    CharacterRotator class :
    Code (csharp):
    1.  
    2. using Sirenix.OdinInspector;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [RequireComponent(typeof(Rigidbody))]
    8. public class CharacterRotator : SerializedMonoBehaviour
    9. {
    10.     public IVector2Provider axisProvider;
    11.  
    12.     public bool canRotate = true;
    13.  
    14.     private float rotY;
    15.     private float targetRotY;
    16.     private float rotYVelocity;
    17.  
    18.     public float yAxisSpeed = 5f;
    19.     public float rotYSmoothTime = 0.05f;
    20.  
    21.     private Rigidbody rigidbody;
    22.  
    23.     private void Start()
    24.     {
    25.         rigidbody = GetComponent<Rigidbody>();
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if (axisProvider == null || canRotate == false)
    31.         {
    32.             return;
    33.         }
    34.  
    35.         UpdateAxis(axisProvider.GetVector2().x);
    36.     }
    37.  
    38.     private void FixedUpdate()
    39.     {
    40.         Quaternion deltaRot = Quaternion.Euler(new Vector3(0, rotY, 0));
    41.  
    42.         rigidbody.MoveRotation(deltaRot);
    43.     }
    44.     public void UpdateAxis(float axisY)
    45.     {
    46.         //Rotation on Y axis
    47.         targetRotY += axisY * yAxisSpeed;
    48.         rotY = Mathf.SmoothDamp(rotY, targetRotY, ref rotYVelocity, rotYSmoothTime);
    49.     }
    50. }
    51.  
    52.  
     
  2. GameMadeByGamers

    GameMadeByGamers

    Joined:
    Nov 25, 2014
    Posts:
    54
    Nevermind updated to 0.9 has seem to fix the problem