Search Unity

Question I can't aim properly in 3rd person with cinemachine and animation rigging

Discussion in 'Cinemachine' started by Pietrofonix, Apr 12, 2022.

  1. Pietrofonix

    Pietrofonix

    Joined:
    Aug 1, 2020
    Posts:
    54
    Hi guys i have a problem aiming in 3rd person with Cinemachine. Basically i'm trying to create a 3rd person shooter controller with two cinemachine, one for the normal movement and one for the aiming. The first works very good with a framing transposer in the body and a POV in the Aim, while for the second i can't find good settings to adjust it. I tried to duplicate the first and mantain the same settings with a lower camera distance but the camera movement is not good at all. I also tried with 3rd person follow in the body, nothing in the aim and adjust it via script, but i can't find a solution. I want the camera to rotate and look where the player is aiming. I'm using Control as videogame reference and trying to replicate it, so the player rotates by aiming on the x axis but stay put while aiming on the y axis. I specify that i'm using the new input system and the animation rigging, this is my script:
    Code (CSharp):
    1.  
    2. //Player rotation method
    3. void PlayerRotation()
    4.     {
    5.         if (PlayerRifleAimingIdle.PlayerIsAiming)
    6.         {
    7.             transform.Rotate(new Vector3(0f, m_mouseDelta.x * m_aimSensivity, 0f));
    8.         }
    9.  
    10.         if (IsMovementPressed && !PlayerRifleAimingIdle.PlayerIsAiming)
    11.         {
    12.             Quaternion targetRotation = Quaternion.LookRotation(m_moveDir);
    13.             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.fixedDeltaTime * m_rotationSpeed);
    14.         }
    15.     }
    16.  
    17. //Player aiming method
    18. void AimTarget()
    19.     {
    20.         if (PlayerRifleAimingIdle.PlayerIsAiming)
    21.         {
    22.             m_rig.weight = 1f;
    23.             Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, m_mousePosition.y, 0f));
    24.             if (Physics.Raycast(ray, out RaycastHit hit))
    25.             {
    26.                 m_aimTarget.position = hit.point;
    27.                 m_crosshair.SetActive(true);
    28.                 m_crosshairPos.anchoredPosition = new Vector2(m_crosshairPos.anchoredPosition.x, m_mousePosition.y);
    29.             }
    30.         }
    31.         else
    32.         {
    33.             m_crosshair.SetActive(false);
    34.             m_rig.weight = 0f;
    35.         }
    36.     }
    37.  
    This is a video of my project so you can better see what i want to achieve:
     
  2. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Have you checked out this video? Tutorial that solves almost exactly this problem.

    Hope it helps!
     
    Gregoryl likes this.
  3. Pietrofonix

    Pietrofonix

    Joined:
    Aug 1, 2020
    Posts:
    54
    Yes i have seen this, but i have a problem with the vertical rotation of the camera. I put an empty object on the neck of the player and set it as follow target on the aim camera. Then inside the script i'm trying to clamp the values but it doesn't work and i don't know why. This is my code:

    Code (CSharp):
    1.  
    2. //Rotation of the target
    3. m_aimFollowTarget.rotation *= Quaternion.AngleAxis(-m_mouseDelta.y * m_aimSensivity, Vector3.right);
    4.  
    5. //Rotation of the player
    6. transform.Rotate(new Vector3(0f, m_mouseDelta.x * m_aimSensivity, 0f));
    7.  
    8. //Clamp of the vertical rotation
    9. Vector3 angles = m_aimFollowTarget.localEulerAngles;
    10.  
    11. if (angles.x > 89f)
    12. {
    13. angles.x = 89f;
    14. }
    15. else if (angles.x < -89f)
    16. {
    17. angles.x = -89f;
    18. }
    19.  
    20. m_aimFollowTarget.localEulerAngles = angles;
    21.  
     
  4. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    NOT AN ENGINEER (anymore) ALERT:

    Take a look at the documentation for localEulerAngle...it reads:


    It seems to me that you're relying on this property in a way that isn't consistent with how it's intended to be used. I believe that the code shown at around 3:50 in the tutorial addresses this case as well?
     
    Gregoryl likes this.