Search Unity

Question [SOLVED] Jitter with view model weapon sway

Discussion in 'Physics' started by ChiliStudio, Sep 13, 2020.

  1. ChiliStudio

    ChiliStudio

    Joined:
    Jan 19, 2018
    Posts:
    33
    I am trying to have a weapon/arms model lag behind the rotation of the first-person camera.
    I have direct mouse input controlling the camera, no mouse smoothing or frame rate dependency.

    No matter how I put it I get a terrible jitter from the view model (THE CAMERA ROTATION IS FINE!):


    This has nothing to do with character movement, it happens when standing still.

    Here is the code:

    Code (CSharp):
    1.     Vector2 newRot;
    2.     public float sens;
    3.     public GameObject viewModel;
    4.  
    5.     void Awake()
    6.     {
    7.         newRot = new Vector2(camTransform.eulerAngles.x, camTransform.eulerAngles.y);
    8.     }
    9.  
    10.     private void Update()
    11.     {
    12.         newRot.y += Input.GetAxis("Mouse X") * sens);
    13.         newRot.x -= Input.GetAxis("Mouse Y") * sens;
    14.  
    15.         newRot.x = Mathf.Clamp(newRot.x, -90f, 90f);
    16.  
    17.         // MOVE CAMERA
    18.         camTransform.localRotation = Quaternion.Euler(newRot.x * lookSpeed, newRot.y * lookSpeed, 0);
    19.  
    20.         // Now here is where my problem is: I want to make the weapon follow the camera smoothly and delayed,             but it jitters badly
    21.  
    22.        
    23.             // ROTATE VIEW MODEL
    24.             viewModel.transform.rotation = Quaternion.Slerp(viewModel.transform.rotation, camTransform.rotation, .1f);
    25.  
    26.     }

    I have tried:
    • Quaternion.Lerp instead
    • Changing the Slerp factor to
      Quaternion.Angle(camTransform.rotation, viewModel.transform.rotation) / float delayFactor
    • Moving the camera movement to LateUpdate(), and keeping the View Model code in Update()
    • Moving all of the code to LateUpdate()
    • viewModel.transform.rotation = Quaternion.RotateTowards(viewModel.transform.rotation, camTransform.rotation, Quaternion.Angle(viewModel.transform.rotation, camTransform.rotation));

    • viewModel.transform.rotation = Quaternion.LookRotation(Vector3.SmoothDamp(viewModel.transform.forward, camTransform.transform.forward, ref v, viewModel.rotateSpeed));

    • viewModel.transform.rotation = Quaternion.Slerp(viewModel.transform.rotation, camTransform.rotation, viewModel.rotateSpeed * Time.deltaTime);

    Help is much appreciated. If it isn't against the rules I'll paypal ya $5 if you can fix it :)
    Thanks!
     
    Last edited: Sep 13, 2020
  2. ChiliStudio

    ChiliStudio

    Joined:
    Jan 19, 2018
    Posts:
    33
    Alright for anyone wandering that might have been having the same problem, I fixed this by making my view model a child of the camera. Then I made four animations for the view model holder that have one keyframe each, these are just the poses that represent the maximum allowed sway. SwayLeft, SwayRight, SwayUp, and SwayDown. In the view model animator controller, I set up a 2d freeform cartesian blend tree on a new layer that is additive. Then, through code, I lerped the parameters that control the blend tree.

    Here's what my code looks like on update now: (note i've only set up the x sway atm)
    Code (CSharp):
    1.        
    2.  
    3. float x = 0;
    4.  
    5. void Update(){
    6.     if (aimingDownSights)
    7.             {
    8.                 x = Mathf.Lerp(x, 0, .5f);
    9.             }
    10.             else
    11.             {
    12.                 x = Mathf.Lerp(x, -Input.GetAxis("Mouse X") / 3, .03f);
    13.             }
    14.  
    15.  
    16.             if (animator != null)
    17.                 animator.SetFloat("Blend", x);
    18. }
    19.  
    Anyways that solved my problem, kinda weird.
     
    Last edited: Sep 15, 2020