Why the transform is not looking at and rotating towards the same time to the lookObj using ik?

Viewed 34

If I move the lookObj in front the transform the transform my player will look at the lookObj but I want that the transform will rotate and look at the lookObj.

The lookObj is behind the transform and instead moving the lookObj I want the transform to rotate and look at.

I set the flag go variable from another script to true inside Update().

using UnityEngine;
[RequireComponent(typeof(Animator))]

public class MyLookAt : MonoBehaviour
{
    public Transform lookObj = null;
    public float finalLookWeight;
    public float weightDamping = 1.5f;
    public bool go = false;

    protected Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    //a callback for calculating IK
    void OnAnimatorIK()
    {
        if (lookObj != null && go)
        {
            Vector3 flattenedLookAtVector = Vector3.ProjectOnPlane(lookObj.position - transform.position, transform.up);
            float dotProduct = Vector3.Dot(transform.forward, flattenedLookAtVector);
            float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
            finalLookWeight = Mathf.Lerp(finalLookWeight, lookWeight, Time.deltaTime * weightDamping);
            float bodyWeight = finalLookWeight * .5f;

            animator.SetLookAtWeight(finalLookWeight, bodyWeight);
            animator.SetLookAtPosition(lookObj.position);

            Quaternion handRotation = Quaternion.LookRotation(lookObj.position - transform.position);
            animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f);
            animator.SetIKRotation(AvatarIKGoal.RightHand, handRotation);
        }
    }
}
0 Answers
Related