How can I use IK to make the player to rotate facing a target if the target is behind the player ?

This script is attached to the player.

using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))]

public class IKControl : MonoBehaviour
{

    protected Animator animator;

    public bool ikActive = false;
    public Transform headObj = null;
    public Transform lookObj = null;

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

    //a callback for calculating IK
    void OnAnimatorIK()
    {
        if (animator)
        {

            //if the IK is active, set the position and rotation directly to the goal.
            if (ikActive)
            {
                // Set the look target position, if one has been assigned
                if (lookObj != null)
                {
                    animator.SetLookAtWeight(1, 1, 1, 1, 1);
                    animator.SetLookAtPosition(lookObj.position);
                }
            }

            //if the IK is not active, set the position and rotation of the hand and head back to the original position
            else
            {
                animator.SetLookAtWeight(0);
            }
        }
    }
}

And this script is attached to a cube.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateAround : MonoBehaviour
{
    // Add this script to rotating object
    [Header("Gameobject that object/s will rotate around as center point")]
    public GameObject centerObj;//to get the position in worldspace to which this gameObject will rotate around.

    [Header("The axis by which it will rotate around")]
    public Vector3 axis;//by which axis it will rotate. x,y or z.

    [Header("Angle covered per update")]
    public float angle; //or the speed of rotation.

    public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
    private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness;

    // Update is called once per frame
    void Update()
    {
        //Gets the position of your 'Center' and rotates this gameObject around it by the 'axis' provided at speed 'angle' in degrees per update
        transform.RotateAround(centerObj.transform.position, axis, angle);
        time += Time.deltaTime;
        //Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay
        if (time > delay)
        {
            prevHeight = height;
            height = Random.Range(lowerLimit, upperLimit);
            time = 0;
        }
        //Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)
        transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
    }
}

The result is that the cube is rotating around the player with random height.
The player head,body,eyes rotating smooth looking at facing the cube.

But the player is looking at the cube only when the cube is in the view are that is in front the player the forward. If the cube is behind the player the player return to idle then when the cube getting to the view front again then the player starting looking at the cube again.

I want to make some how using the IK or maybe to add another code to the IKControl script that if the cube is behind the player rotate the whole player and keep facing and looking the cube.

I think it should be rotating the player on the X 360 degrees according to the cube position.
The problem is how to do it with the IK or maybe I should add the Update function to the script and do something else there?

This is a screenshot when the cube in the forward front view of the player and the player is looking at the cube :

And this is a screenshot when the cube is behind the player. The player is idle and I want the player to rotate with the cube :

Just a quick response, since I had a similar issue. You can use Transform.LookAt with the XZ position of your target (same x and z-coords, y-coordinate = 0) to make sure your character’s base transform is rotated generally towards the target, and then continue to use your IK script to perform fine-adjustment with the head.

If your character is on a slope/not flat, then you might have to use something in your character-controller to adjust for that (step in a circle on the slope, rather than a raw-turn), but that’s more specific to your use-case. Cheers!