using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 切线检测
/// </summary>
public class TangentDetection : MonoBehaviour
{
public string playerTag = "Player";
private Transform playerTF;
private float radius;
private void Start()
{
GameObject playerGO = GameObject.FindWithTag(playerTag);
playerTF = playerGO.transform;
radius = playerTF.GetComponent<CapsuleCollider>().radius;
}
private Vector3 leftTangent, rightTangent;
//计算切点
private void CalculateTangent()
{
//玩家到炸弹的向量
Vector3 playerToExposion = this.transform.position - playerTF.position;
//玩家到炸弹方向的向量(长度为半径)
Vector3 playerToExposionDirection = playerToExposion.normalized * radius;
//与切点相交的半径 和 方向向量(playerToExposionDirection)的夹角
float angle = Mathf.Acos(radius / playerToExposion.magnitude) * Mathf.Rad2Deg;
//得出中心两边切点的位置 要加playerTF.position的原因是把坐标从世界原点挪到玩家坐标点
leftTangent = playerTF.position + Quaternion.Euler(0, -angle, 0) * playerToExposionDirection;
rightTangent = playerTF.position + Quaternion.Euler(0, angle, 0) * playerToExposionDirection;
}
public void Detection()
{
CalculateTangent();
Debug.DrawLine(this.transform.position, leftTangent);
Debug.DrawLine(this.transform.position, rightTangent);
}
private void Update()
{
Detection();
}
}