Game Object Tracker with Rotation of Observing game object

Hello all and thanks for reading.

I am attempting to do an objective tracker it will work like the Rocket League Ball Tracker when not camera locked.

I found this code on Unity Scripting/ Vector3.ClampMagnitude

and modified it to fit my needs. It works, as long as my vehicle is facing the Objective. If I rotate the vehicle it stops working. So I was thinking get that angle and do some stuff with that.

void Start () {
radius = 5f;
objective = GameObject.Find(“CapturePoint”);
centerPt = this.transform.position;
centerPtLocal = this.transform.localPosition;
}

// Update is called once per frame
void Update () {
    //Face Objective
    transform.LookAt(objective.transform);
    //Get angle of Car with respect to Objective
    Vector3 targetDir = objective.transform.position - transform.parent.transform.position;
    float angle = Vector3.Angle(targetDir, transform.parent.transform.forward);
    print(angle);

    // Get the new position for the object.
    Vector3 movement = objective.transform.position;
    Vector3 newPos = transform.position + movement;
    
    // Calculate the distance of the new position from the center point. Keep the direction
    // the same but clamp the length to the specified radius.
    Vector3 offset = newPos - centerPt;
    Vector3 clamped = Vector3.ClampMagnitude(offset, radius);

    Vector3 temp = new Vector3(clamped.x * Mathf.Sin(angle), 0f, clamped.z * Mathf.Cos(angle) );
  
    transform.localPosition = centerPtLocal + temp;

This is the solution that has created the Rocket League tracker in 2 dimensions.
There were two err that needed to be fixed 1) Vector.SignedAngle 2) convert degrees to Radians.
Remeber that Mathf.Cos and … Tan use Radians not Degrees!

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

/* Code takes this.Gobject or pointer which is alligned with Z axis and points it at target/objective
 * it will rotate around parent Gobject
 * pointer always resides at max radius
 * changes position of pointer with respect to parent.forward and target
 * only works in 3 axis.
 * Created by Jeigh07
 * inspired by Rocket League Pointer
 */

public class PointAtObjective : MonoBehaviour {

    public GameObject objective;
    public float radius;
    private Vector3 centerPt;
    private Vector3 centerPtLocal;
    private Transform parentXform;

    // Use this for initialization
    void Start () {
       
        radius = 2f;
        objective = GameObject.Find("CapturePoint");//the objective
        centerPt = this.transform.position;//the pointer which starts at center of center
        centerPtLocal = this.transform.localPosition; //local position of center
        parentXform = transform.parent.transform;


    }
	
	// Update is called once per frame
	void Update () {

        //Changes euler angles of pointer so that it may point to objective
        transform.LookAt(objective.transform);

        //Get the angle of car with respect to the objective in Radians   
        Vector3 targetDir = objective.transform.position - parentXform.position;
        float angle = Vector3.SignedAngle(targetDir, transform.parent.transform.forward, Vector3.up);
        angle *= Mathf.Deg2Rad;

        // Get the new position for the object.
        Vector3 movement = objective.transform.position;
        Vector3 newPos = transform.position + movement;

        // Calculate the distance of the new position from the center point. Keep the direction
        // the same but clamp the length to the specified radius.
        Vector3 offset = (newPos - centerPt) * 1000; //scaled to max out r
        Vector3 clamped =new Vector3(
           Mathf.Clamp(offset.x, -radius, radius),
           0f,
           Mathf.Clamp(offset.z, -radius, radius) ) ;

        //Rotate the pointer object about the parent object with respect to the smallest angle in rads of parent and objective
        Vector3 temp = new Vector3(clamped.x * Mathf.Sin(angle), 0f, clamped.z * Mathf.Cos(angle));
        //pass value to local position of pointer
        transform.localPosition = centerPtLocal + temp;
    }
}