FANDOM


This script allows characters to rely on FinalIK's Look At IK component when turning their head. To use it, add the Look At IK component to your character's base and configure it. Then, add the following code to the character, and assign the two fields.

FinalIKHeadTurn.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;
using RootMotion.FinalIK;

public class FinalIKHeadTurn : MonoBehaviour
{

#region Variables

[SerializeField] private LookAtIK lookAtIK;
[SerializeField] private Char character;

private Transform headTurnTarget;
private LerpUtils.FloatLerp headWeightLerp = new LerpUtils.FloatLerp (true);
private Vector3 offset;
private float headWeight = 0f;
private const float headTurnSpeed = 5f;

#endregion


#region UnityStandards

private void OnEnable ()
{
EventManager.OnSetHeadTurnTarget += SetLookAtTarget;
EventManager.OnClearHeadTurnTarget += ClearLookAtTarget;
}


private void OnDisable ()
{
EventManager.OnSetHeadTurnTarget -= SetLookAtTarget;
EventManager.OnClearHeadTurnTarget -= ClearLookAtTarget;
}


private void Update ()
{
headWeight = headWeightLerp.Update (headWeight, (headTurnTarget != null) ? 1f : 0f, headTurnSpeed);
}


private void LateUpdate ()
{
if (lookAtIK)
{
if (headTurnTarget)
{
lookAtIK.solver.IKPosition = headTurnTarget.position + offset;
}
lookAtIK.solver.IKPositionWeight = headWeight;
}
}

#endregion


#region PublicFunctions

public void SetLookAtTarget (Char character, Transform headTurnTarget, Vector3 offset, bool isInstant)
{
if (headTurnTarget != null && character == this.character)
{
this.headTurnTarget = headTurnTarget;
this.offset = offset;

if (isInstant)
{
headWeight = 1f;
headWeightLerp.Update (1f, 1f, headTurnSpeed);
}
}
}


public void ClearLookAtTarget (Char character, bool isInstant)
{
if (character == this.character)
{
headTurnTarget = null;

if (isInstant)
{
headWeight = 0f;
headWeightLerp.Update (0f, 0f, headTurnSpeed);
}
}
}


public void ClearLookAtTarget (Transform _headTurnTarget)
{
if (headTurnTarget == _headTurnTarget)
{
headTurnTarget = null;
}
}

#endregion

}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.