Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved How to lock rotation of vcam while using aim and body same time?

Discussion in 'Cinemachine' started by FurkanAkkurt, Dec 16, 2022.

  1. FurkanAkkurt

    FurkanAkkurt

    Joined:
    Nov 24, 2022
    Posts:
    4
    I want to both follow an object and aim it. I'm trying to make a camera that will aim my object from character only on the x-axis and follow the character completely. If you want to imagine easier; I will set my camera like in the game called "Soccer Story". So I need to change both position and rotation. This is why I used body and aim together in my question.I know that body changes position, aim changes rotation. Maybe I am wrong..

    What have I done so far?
    • I wrote a extention for the body to keep it fixed on the z-axis.
    • Body mode is "Framing Transposer"
    • Following part done so far
    the question point:
    • I cant aim on x axis only. (I tried to write an extension but I think I failed.)
    • How can able to aim to X axis, Y and Z fixed?
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    If you wrote an extension to fix the position on the z-axis, then I am sure your rotation fix code was also close. ;)

    Try this to lock the camera rotation on Y and Z axes to a specified value:
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. [ExecuteAlways]
    5. [SaveDuringPlay]
    6. public class LockCameraRotationYZ : CinemachineExtension
    7. {
    8.     [Tooltip("Lock the camera's rotation around the Y axis to this value")]
    9.     public float LockY = 0;
    10.  
    11.     [Tooltip("Lock the camera's rotation around the Z axis to this value")]
    12.     public float LockZ = 0;
    13.  
    14.     protected override void PostPipelineStageCallback(
    15.         CinemachineVirtualCameraBase vcam,
    16.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    17.     {
    18.         if (stage == CinemachineCore.Stage.Finalize)
    19.         {
    20.             var euler = state.RawOrientation.eulerAngles;
    21.             euler.y = LockY;
    22.             euler.z = LockZ;
    23.             state.RawOrientation = Quaternion.Euler(euler);
    24.         }
    25.     }
    26. }
    27.  
     
    Last edited: Dec 16, 2022
    FurkanAkkurt and Onigiri like this.
  3. FurkanAkkurt

    FurkanAkkurt

    Joined:
    Nov 24, 2022
    Posts:
    4
    That is exactly what I was looking for. Thanks!