Forum rules - please read before posting.

Feature Request: Head turning using IK

Hi,

I like the feature of head turning inside AC but the setup seems a little weird. One has to supply 4 animations. Why is that? When using Mecanim there is a very easy way to make a character look at something using IK. Example code I am using (I cut some parts but the gist should be there), where a character will look at whatever is supplied in lookAt with smooth head turns when another lookAt has been set. This could simplify the head movement setup a lot (reduce it to a checkbox) :-)

    public Transform lookAt;
    private Vector3 curLookAt;

    void OnAnimatorIK() {
        Animator animator = GetComponent<Animator>(); // cache of course, reduced for this example

        if (curLookAt.Equals(Vector3.zero) && lookAt != null) {
            curLookAt = lookAt.position;
        }
        curLookAt = Vector3.Lerp(curLookAt, (lookAt == null) ? Vector3.zero : lookAt.position, Time.deltaTime);
        animator.SetLookAtPosition(curLookAt);

        // AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0); // might be useful in case AC needs to check if a certain animation is playing and restrict head movement then
        // if (lookAt.Equals(Vector3.zero) || !state.IsName("Idle")) {
        if (lookAt == null) {
            lookWeight = Mathf.Lerp(lookWeight, 0f, Time.deltaTime * 3);
        } else {
            lookWeight = Mathf.Lerp(lookWeight, 1f, Time.deltaTime);
        }
        animator.SetLookAtWeight(lookWeight);
    }

Comments

  • Thanks for the code, but this'd only be compatible with Unity Pro.  For maximum accessibility, AC implements a method that works for Free, too.
  • Would be a nice gimmick for users though to optionally enable it if PRO is detected :-)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.