Follow Camera Offset does not Change when Follow Target object rotates when Camera Active

Cinemachine 3.0.0-pre9

I have a cinemachine camera that looks at object A and follows object B. In addition object B always looks at object A. When object B changes position and rotates to look at object A the camera is offset as if object B was looking in he direction when the camera was activated so if object B was looking 90 degrees to the right the offset that was z now needs to be x…

Is there any way to force the follow offset to be recalculated based on the new rotation of follow object?

It is also possible that the issue I am having is that the offset always is applied in world space and it just so happens my initial orientation is along the correct axis… Is it possible that the follow offset even when set to Lock to Target with World up is applied in World Space?

To further clarify I see the problem is the Follow is not responding to rotation of the tracking object at all. .If I go into scene mode and rotate the Tracking object the camera does not rotate at all with it. Since the rotation of the follow object has changed the offset of the follow should move with it but it does not…

If I swap out the Follow Object with a different transform the offset changes to match the rotation of the new tracking object and when I slot the old one back in it adjusts to the rotation of that object. But if I just rotate the tracking object the follow offset does not change to match the new rotation… Hope that makes sense… Seems I need a way to force the update

Ok for anyone else out there who might be having a similar problem, many of our CM2 cameras had Transposers on them which during the upgrade process CM3 converts to Follow. The problem is follow does not track to the rotation of the object it is following… In order to do that you need to use a Position Composer it seems…

Perhaps someone from Unity can confirm this to be true before I convert our 60+ cameras from Follow to Position Composer… Thanks

Follow can track the target’s rotation. You need to set the binding mode to one of the “Lock To Target” options.

9421259--1320173--upload_2023-10-20_10-5-52.png

Can you show me an image of the CM2 camera’s inspector before it got converted to CM3, and then an image of the CM3 camera’s inspector after it got converted? It could be there there is a bug in the upgrader that set the wrong binding mode.

EDIT: I just checked the upgrader, it upgrades the binding mode correctly in my test case. The before/after inspectors of your CM camera would be most helpful to understand what went wrong.

EDIT2: Don’t use LockToTargetOnAssign - that gives the behaviour you described, as designed. It computes the offset based on the rotation at the time the target was assigned, and leaves it there.

I was using Lock to Target with World up and it does not track the rotation of the follow object, in fact I tried all of them and none of them responded to the follow target rotation

Here are three images showing the setup, and two different rotations of the tracking object and the camera in the same place. Previously the z offset would apply to the direction of the blue arrow

How very strange. It works when I try it.

9421574--1320242--rotate_target.gif

What is the gameobject relationship between pintarget and aimtarget?

The only relationship is that aimTarget always looks at pinTarget and camera is also looking at pinTarget and camera is offset from aimTarget. But in my world for whatever reason the offset is always applied in world space regardless of the follow binding mode. One other thing to add is all our cameras are managed by a CinemachineStateCamera. The brain is set to Smart Update

Using Unity 2022.3 LTS

Would you be willing to send me the project? You can zip it up and DM it to me.

One other thing before we go down that path… I do have some cameras that work in the scene but they are cameras that are part of a Clear Shot Camera… They all respond to rotating the follow object just fine… I did notice when I upgraded the project that those cameras that worked had both a look at and a follow. The ones that do not work had only a look at and I had to right click the settings next to the look at and select use a separate follow target or something like that

I also tried creating a new camera from scratch and it has the same problem…

IF the above does not ring any bells I will try recreating in an empty project and see if I can send that… Thanks

The rotating offset only cares about a Follow target. The LookAt should make no difference. You can just have a common “tracking target” that serves for both, or you can assign separate objects to LookAt and Follow.

That the cameras are inside a clearshot or statedriven makes no difference.

I also tried creating a new camera from scratch and it has the same problem…
That’s what I did and it worked. We must be doing something differently. If you can make that happen in a standalone project then we can easily track this down.

Ok I did some digging into what was going on and this is what I found…

It is all going wrong in this code in TrackTarget method of TargetTracking.cs

 else if (settings.BindingMode != BindingMode.LazyFollow)
                {
                    var relative = (Quaternion.Inverse(PreviousReferenceOrientation)
                        * targetOrientation).eulerAngles;
                    for (int i = 0; i < 3; ++i)
                    {
                        if (relative[i] > 180)
                            relative[i] -= 360;
                        if (Mathf.Abs(relative[i]) < 0.01f) // correct for precision drift
                            relative[i] = 0;
                    }
                    relative = component.VirtualCamera.DetachedFollowTargetDamp(
                        relative, settings.GetEffectiveRotationDamping(), deltaTime);
                    dampedOrientation = PreviousReferenceOrientation * Quaternion.Euler(relative);
                }

In here relative is always 0,0,0… This is because PreviousReferenceOrientation is always 0,0,0

So why is Previous Reference Orientation always 0… Well its because of this code

public void InitStateInfo(
            CinemachineComponentBase component, float deltaTime,
            BindingMode bindingMode, Vector3 up)
        {
            Debug.Log($"Initializing Camera {component.gameObject.name} State is Valid {component.VirtualCamera.PreviousStateIsValid}");
            bool prevStateValid = deltaTime >= 0 && component.VirtualCamera.PreviousStateIsValid;
            if (component.VirtualCamera.Name == "Aim") Debug.Log("DebugME");
            if (m_PreviousTarget != component.FollowTarget || !prevStateValid)
            {
                m_PreviousTarget = component.FollowTarget;
                m_TargetOrientationOnAssign = component.FollowTargetRotation;
                //if (component.VirtualCamera.Name == "Aim") PreviousReferenceOrientation = GetReferenceOrientation(component, bindingMode, up);
            }
            if (!prevStateValid)
            {
                PreviousTargetPosition = component.FollowTargetPosition;
                Debug.Log($"Initializing Camera State {component.gameObject.name}");
                PreviousReferenceOrientation = GetReferenceOrientation(component, bindingMode, up);
            }

In this code the previousStateValid is always true and so we never call GetReferenceOrientation

You can see from the code above I added a commented out code, but when this code is active it works just fine…
The one camera that does work gets initialized here with PreviousStateIsValid = true…

I tried forcing PreviousStateValid = false prior to activating the camera and unfortunately it is set back to true by
InternalUpdateCameraState(Vector3 worldUp, float deltaTime) before InitStateInfo is called…

I am guessing our root cause is the way we load cinimachine on project load…

Do you have any suggestions on how I can make sure that when InitStateInfo is called we can force an update?

Ok I think I got to the bottom of it…

public override bool PreviousStateIsValid
        {
            get => base.PreviousStateIsValid;
            set
            {
                base.PreviousStateIsValid = value;
                for (int i = 0; m_ChildCameras != null && i < m_ChildCameras.Count; ++i)
                    m_ChildCameras[i].PreviousStateIsValid = value;
            }
        }

This code executes on our CinemachineState Camera which controls all of the Virtual Cameras in the scene… and so as soon as we set the Previous State Valid to false this sets it to true because we have not set the Cinemachine State camera valid to false… I will investigate what happens if we set the state to false…

ok this does not work because

public override void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
        {
.......
            PreviousStateIsValid = true;
        }

gets called before InitStateInfo is called on the camera…

Do you have any suggestions?

Very interesting. I wish I had a repro project I could debug.

Try this experiment: change CinemachineCameraManagerBase.PreviousStateIsValid to this:

public override bool PreviousStateIsValid
{
get => base.PreviousStateIsValid;
set
{
base.PreviousStateIsValid = value;
// Only propagate to the children when we're invalidating the state
if (value == false)
for (int i = 0; m_ChildCameras != null && i < m_ChildCameras.Count; ++i)
m_ChildCameras*.PreviousStateIsValid = value;*
*}*
*}*
*```*
*Please tell me what happens.*
*EDIT: I corrected a mistake I made above. The class to change is CinemachineCameraManagerBase.*

Yes that fixed everything :slight_smile: :):frowning: Any chance this change will make it into the code?

ASAP my friend. Thanks for bringing this to our attention.