Search Unity

Rotation offset for virtual camera

Discussion in 'Cinemachine' started by rubiez64, Dec 2, 2019.

  1. rubiez64

    rubiez64

    Joined:
    Jul 25, 2016
    Posts:
    26
    Hello all,

    I have a question, I'm trying to figure out how to add rotation to a virtual camera.
    since cinemachine controls the rotation... is there an extension similar to offset, but for rotation?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Not out of the box, no, but you can take CinemachineCameraOffset.cs, copy it, and modify it to affect state.OrientationCorrection instead of state.PositionCorrection. The units are in camera-local space.
     
  3. rubiez64

    rubiez64

    Joined:
    Jul 25, 2016
    Posts:
    26
    Thanks! you helped me a lot!

    here's a quick summery if anyone needs:
    1) go to packages > Cinemachine > Runtime > Behaviours
    2) Open "CinemachineCameraOffset.cs" and copy the script.
    3) Create a new script "CinemachineCameraRotation.cs" and paste the code(make sure to rename the class as well).
    4) replace "state.PositionCorrection += offset;" with "state.OrientationCorrection *= Quaternion.Euler(offset);".
    5) add this as an extension.
     
    kai303, raphaelnew and Gregoryl like this.
  4. kai303

    kai303

    Joined:
    Jun 9, 2016
    Posts:
    21
    Thanks rubiez64 - I was totally happy when I found your post after hours of trying to get some rotation offset to my virtual camera!

    I would like to recommend to add a script like that to the official cinemachine package. There might be other users who really need it.
     
    hawaiian_lasagne likes this.
  5. Dibloo

    Dibloo

    Joined:
    Apr 18, 2019
    Posts:
    1
    Hey guys, I know it's old but the problem with rubiez64 solution is that the rotation offset will be in world space and not in local space of the camera, so when the camera will turn it will rotate in the wrong direction :/
     
  6. KorDum

    KorDum

    Joined:
    May 9, 2019
    Posts:
    6
    Use it :)

    Code (CSharp):
    1. protected override void PostPipelineStageCallback(
    2.     CinemachineVirtualCameraBase vcam,
    3.     CinemachineCore.Stage stage,
    4.     ref CameraState state,
    5.     float deltaTime
    6. ) {
    7.     if (stage != applyAfter) {
    8.         return;
    9.     }
    10.  
    11.     state.RawOrientation = state.RawOrientation.ApplyCameraRotation(offset, state.ReferenceUp);
    12. }
     
    hungcuong21891 likes this.
  7. hungcuong21891

    hungcuong21891

    Joined:
    Dec 5, 2016
    Posts:
    1
    Work for me !!! Thanks