Search Unity

Question I want to take only the rotation of the Y axis, not the X and Z axes, without constraining the posit

Discussion in 'Cinemachine' started by ktk_unity750, Apr 19, 2023.

  1. ktk_unity750

    ktk_unity750

    Joined:
    May 23, 2022
    Posts:
    6
    I'm using the FreeLook camera. When the FreeLookCamera sees the LookAt, I don't want to constrain the position of the FreeLookCamera camera, only rotate the Y axis. What method should I take?
     
    Last edited: Apr 19, 2023
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    In that case you should use a simple virtual camera:

    upload_2023-4-19_7-22-17.png
     
  3. ktk_unity750

    ktk_unity750

    Joined:
    May 23, 2022
    Posts:
    6
    Is there a way to keep the FreeLook control but exclude the rotation of the X and Z axes only for the gaze?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    You can keep the FreeLook and put DoNothing in Aim for Top, Middle, and Bottom rigs. That will allow the camera to orbit, but its rotation will remain constant. Then, you can add a custom Aim behaviour to rotate the camera Y only.

    Alternatively, you can make a custom extension to lock the X and Z rotation of the camera. Here is one that locks the Y position. You can modify it to lock the X and Z rotations instead.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that locks the camera's Y co-ordinate
    6. /// </summary>
    7. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class LockCameraY : CinemachineExtension
    9. {
    10.     [Tooltip("Lock the camera's Y position to this value")]
    11.     public float m_YPosition = 10;
    12.  
    13.     protected override void PostPipelineStageCallback(
    14.         CinemachineVirtualCameraBase vcam,
    15.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    16.     {
    17.         if (stage == CinemachineCore.Stage.Finalize)
    18.         {
    19.             var pos = state.RawPosition;
    20.             pos.y = m_YPosition;
    21.             state.RawPosition = pos;
    22.         }
    23.     }
    24. }
    25.  
     
  5. ktk_unity750

    ktk_unity750

    Joined:
    May 23, 2022
    Posts:
    6
    Thanks for the nice class, but the camera shakes when moving up and down. Can you tell me what's wrong with my code?


    Code (CSharp):
    1. public class AltavaCamYLook : CinemachineExtension
    2. {
    3.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    4.     {
    5.         if (stage == CinemachineCore.Stage.Finalize)
    6.         {
    7.             var pos = vcam.LookAt.position;
    8.             pos.y = vcam.transform.position.y;
    9.             vcam.LookAt.position = pos;
    10.         }
    11.     }
    12. }
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    I am confused about what you're trying to do. I thought you wanted to constrain the camera rotation, but instead you are constraining the position.

    Are you looking for a camera that rotates around the player only horizontally but not vertically? If so, then replace the FreeLook with a VirtualCamera set up like this:

    upload_2023-4-21_7-37-2.png

    If that is not what you want, please describe the desired camera behaviour more carefully. A picture or diagram would be helpful.
     
  7. ktk_unity750

    ktk_unity750

    Joined:
    May 23, 2022
    Posts:
    6
    The shape of the camera I want to do is to limit the camera's line of sight while maintaining the range of motion of the prelook.

    If you look at the picture, the red arrow is the current pre-look and the blue arrow is the direction I want to see.
    .
     

    Attached Files:

  8. ktk_unity750

    ktk_unity750

    Joined:
    May 23, 2022
    Posts:
    6
    I think I've found a way! However, there is a small problem. Is there any way to access the LookAtOverride of the red box area using script?
     

    Attached Files:

  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    It would be freeLook.GetRig(n).LookAt, where n = 0...2
     
  10. ktk_unity750

    ktk_unity750

    Joined:
    May 23, 2022
    Posts:
    6
    Any help is greatly appreciated Thank you for your support!