I tried this :
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.SocialPlatforms;
using UnityEditor.Animations;
[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour
{
public Transform lookObj = null;
public float finalLookWeight;
public float weightDamping = 1.5f;
public float speed;
protected Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
//a callback for calculating IK
void OnAnimatorIK()
{
if (lookObj != null)
{
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);
// RotateTowards
Vector3 targetDirection = lookObj.position - transform.position;
float singleStep = speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
}
I added the // RotateTowards
I want the character head to keep looking on the target rotating smooth when the object is in the front as it is now but also the whole character to rotate spin facing the target. So the character will track the target in all the directions.
The result is that the character is lying down :