Unity计算B点在A点的那个方向

using UnityEngine;
using System.Collections;

public class NewBehaviourScript1 : MonoBehaviour {
    public Transform jiaodu1;
    public Transform jiaodu2;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {

        Debug.Log(GetAngle(Vector3.zero, jiaodu2.position));
    }
    private float GetAngle(Vector3 a, Vector3 b)
    {
        b.x -= a.x;
        b.z -= a.z;

        float deltaAngle = 0;
        if (b.x == 0 && b.z == 0)
        {
            deltaAngle = 0;
        }
        else if (b.x > 0 && b.z > 0)
        {
            deltaAngle = 0;
        }
        else if (b.x > 0 && b.z == 0)
        {
            deltaAngle = 90;
        }
        else if (b.x > 0 && b.z < 0)
        {
            deltaAngle = 180;
        }
        else if (b.x == 0 && b.z < 0)
        {
            deltaAngle = 180;
        }
        else if (b.x < 0 && b.z < 0)
        {
            deltaAngle = -180;
        }
        else if (b.x < 0 && b.z == 0)
        {
            deltaAngle = -90;
        }
        else if (b.x < 0 && b.z > 0)
        {
            deltaAngle = 0;
        }

        float angle = Mathf.Atan(b.x / b.z) * Mathf.Rad2Deg + deltaAngle;
        if (angle < 0)
        {
            angle = (360-(-angle));
        }
        //if (angle >= 90 && angle <= 270)
        //{
        //    angle = 1;
        //}
        //else
        //{
        //    angle = 0;
        //}
        if(float.IsNaN(angle)){
            angle=0;
        }
        return angle;
    }  
}

你可能感兴趣的:(Unity学习笔记)