Animated mesh looking the wrong way when using SetLookAtPosition

I have some kind of problem with the SetLookAtPosition() function…

For some reason my big statue boss doesn’t want look at the position at all… (He’s a bit of a shy guy if you ask me…)

Here’s screenshot of the scene view:


The position I want its head to look at is at the blue star… In fact, it’s exactly the position I’m feeding to the SetLookAtPosition() function.

I don’t have any idea what could be causing this. I’m also doing the same thing with my other rigs yet no bug at all…

The code itself is quite simple, too:

private void LateUpdate()
{
    if (m_currentLookPosition != m_targetVisualPosition)
    {
        m_currentLookPosition = Vector3.Lerp(m_currentLookPosition, m_targetVisualPosition, Time.deltaTime * (m_lerpTimeScale * 2f));
    }
}

private void OnAnimatorIK(int layerIndex)
{
    m_animator.SetLookAtWeight(m_lookIKWeight, 0.125f, 1f, 1f, m_headTwistAngleLimit);
    if (m_hasVisualPosition && !IsInVisualField(m_targetVisualPosition))
    {
        m_animator.SetLookAtPosition(m_currentLookPosition);
        DebugExt.DrawPoint(m_currentLookPosition, Color.blue, 0.5f, 0);
    }
}

Basically I’m just changing the target position smoothly in the LateUpdate part and assigning it in the OnAnimatorIK one. Nothing fancy really…

Oh, Just as a heads up, the statue mesh is fully custom while it’s animations comes form Mixamo. Not sure it’s the culprit, but you never know…

I rely on this call for my enemy (or in this case, boss) A.I. so any help would be appreciated!

Finally, found it!

For those who are wondering, the bug was quite simple: the SetLookAtPosition sometimes wasn’t invoked at all due to the encapsulating if. When you have a look weight bigger than zero without a look position it instead uses a default coordinate (I suspect (0,0,0) but it doesn’t really matter)

I’ve simply removed the !IsInVisualField(m_targetVisualPosition) bit from the if and now it works wonderfully…

That removed bit was part of a previous version of the code and didn’t get refactored properly.

So, kids, the lesson here is to never forget to refactor…

Otherwise you’re gonna have a bad time.