Find Angle Between Two Objects Relative To Forward

Hello. I currently have a few scripts, the main one I have inserted. Essentially they are old-school angle based sprite billboarding for multi-directional sprites. It works perfectly except for the fact that regardless of the orientation of the sprite object, the angle is still the same. I need the angle to be relative to the gameObject.transform.forward but so far, nothing I’ve done has worked.

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

[RequireComponent(typeof(Animator))]
public class AngleCalculator : MonoBehaviour
{
        public Animator objectAnimator;
        private SpriteRenderer spriteRenderer;
        private GameObject cameraParent;
        private Vector3 localPosition;
        private Vector3 cameraPosition;
        private float localX;
        private float localZ;
        private float cameraX;
        private float cameraZ;
        public float angle;

        void Awake(){
            cameraParent = GameObject.Find("Player");
            objectAnimator = gameObject.GetComponent<Animator>();        
            spriteRenderer = gameObject.GetComponent<SpriteRenderer>();  
        }


        void Update(){

        localPosition = gameObject.transform.position;
                localX = localPosition.x;
                localZ = localPosition.z;

            cameraPosition = cameraParent.transform.position;
                cameraX = cameraPosition.x;
                cameraZ = cameraPosition.z;

        angle = Mathf.Rad2Deg * Mathf.Atan2((cameraZ-localZ), (cameraX-localX));

        if (angle < 0){
            angle = 360 + angle;
        }

        Vector3 lookDirection = new Vector3(cameraParent.transform.forward.x, 0, cameraParent.transform.forward.z);
            transform.LookAt(transform.position + lookDirection);

        if (angle >= 202 && angle <= 337.8){
            spriteRenderer.flipX = true;
        }
            else {
                spriteRenderer.flipX = false;
            }

        objectAnimator.SetFloat("ViewAngle", angle);

        }

    public void OnDrawGizmos(){

        if (!Application.isPlaying){
            SceneView sceneView = GetActiveSceneView();
            if (sceneView){
                Vector3 lookDirection = new Vector3(sceneView.camera.transform.forward.x, 0, sceneView.camera.transform.forward.z);
                transform.LookAt(transform.position + lookDirection);
            }
        }
    }

    private SceneView GetActiveSceneView(){
        if (EditorWindow.focusedWindow != null && EditorWindow.focusedWindow.GetType() == typeof(SceneView))
            return (SceneView)EditorWindow.focusedWindow;

            return null;
        }
}

I think you can do this by getting the transform.InverseTransformDirection() of each vector, then set the .Z to zero (effectively making them Vector2), then consider their angle. That would sorta be like an ortho view on the two vectors from the viewpoint of the transform.forward axis, like noon-day shadow casting.

I can’t set Z to 0, as Y is null in this case. I don’t think I can calculate a slope with just X-intercept coordinates.

I’m bumping this thread again. I’ve been at it for days. The closet I’ve gotten is by using InverseTransformPoint. This worked perfectly in the inspector view, but in game it didn’t work at all. With the InverseTransformPoint, the relative angle stayed around 70.

Any luck with this? I’m trying to do something similar.

Same… Trying to make kind of an old school phone dialer. Have a circle, with points on it, and want to know the Angle between those points on the Circle, and a “reader” that is positioned above the outside of the circle, above it in the center. It would Light up as you pull the numbers around to the top.
When that number is pulled to the top within a few degree’s would cause that top light to turn on… But When I have them lined up right now for some reason it starts at 54 degrees instead of 0… if I drag a number all the way around the dial, the angle goes from 54 to 125 then to -54 then to 0 (for a full spin…)

Check this thread: