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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom LookAt implementation is marked as Invalid and cannot be saved to prefab

Discussion in 'Cinemachine' started by PPE-TrackMan, Feb 10, 2022.

  1. PPE-TrackMan

    PPE-TrackMan

    Joined:
    May 4, 2021
    Posts:
    9
    Hi,

    We wanted a little different behaviour than the HardLookAt, copied it and changed a minor thing. End results:
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. namespace PuttPuttKit.Prototyping
    5. {
    6.     [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
    7.     [AddComponentMenu("")] // Don't display in add component menu
    8.     [SaveDuringPlay]
    9.     public class HardLookAtWithOffset : CinemachineComponentBase
    10.     {
    11.         public float HightAboveTarget;
    12.        
    13.         /// <summary>True if component is enabled and has a LookAt defined</summary>
    14.         public override bool IsValid { get { return enabled && LookAtTarget != null; } }
    15.  
    16.         /// <summary>Get the Cinemachine Pipeline stage that this component implements.
    17.         /// Always returns the Aim stage</summary>
    18.         public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Aim; } }
    19.  
    20.         /// <summary>Applies the composer rules and orients the camera accordingly</summary>
    21.         /// <param name="curState">The current camera state</param>
    22.         /// <param name="deltaTime">Used for calculating damping.  If less than
    23.         /// zero, then target will snap to the center of the dead zone.</param>
    24.         public override void MutateCameraState(ref CameraState curState, float deltaTime)
    25.         {
    26.             if (IsValid && curState.HasLookAt)
    27.             {
    28.                 Vector3 dir = ((curState.ReferenceLookAt + HightAboveTarget * Vector3.up) - curState.CorrectedPosition);
    29.                 if (dir.magnitude > Epsilon)
    30.                 {
    31.                     if (Vector3.Cross(dir.normalized, curState.ReferenceUp).magnitude < Epsilon)
    32.                         curState.RawOrientation = Quaternion.FromToRotation(Vector3.forward, dir);
    33.                     else
    34.                         curState.RawOrientation = Quaternion.LookRotation(dir, curState.ReferenceUp);
    35.                 }
    36.             }
    37.         }
    38.     }
    39. }
    It works perfectly in the editor, but when I try to save it, to the prefab, then it gives
    upload_2022-2-10_9-47-13.png
    The prefab is not immutable, I can make other changes in it without problems.
    When enter prefab mode and trying to save that same change. Then I get:
    upload_2022-2-10_9-46-35.png

    Is there something I am missing to the extension I made?

    Best regards Povl
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hi

    What version of Cinemachine and unity editor are you using? I am trying to reproduce on our side.
     
  3. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Why is your prefab called CameraBrain? Could you show the whole prefab when you open it. and also an instantiated one in the Hierarchy? Could you also send some screenshots of your vcam and and all its components?
    You have two warnings, the first says something about your script. Could you also show your script in the Project window?

    Or can you send me a repro project?
     
  4. PPE-TrackMan

    PPE-TrackMan

    Joined:
    May 4, 2021
    Posts:
    9
    Thank you for the response.
    I am having a hard time recreating the problem on a smaller example (Editor 2021.2.8f1 and Cinemachine 2.8.4).
    So I tried to create the entire CameraBrain prefab again, and now it works. Should have considered that earlier, luckily we havn't added all parts to it yet, so it wasn't that much work.

    My current thought is, there might be a missing script attached to one of the hidden cm game objects. Is there an easy way to verify that? I tried comparing the .prefab files in notepad++, but all the ids are basically different, so can't really pull out which ones are important.

    If that is the case, then it would be nice, if it weren't so hidden (just a small feature thought if more hits similar errors :D). I did do some renaming of the HardLookAtWithOffset file and maybe one of them was after I had set it on the prefab level?