脚本挂在炸弹上!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TargetDetaction : MonoBehaviour
{private Transform PlayerTF;private Transform bomb;private float radius;private string Player = "Player";void Start(){bomb = this.gameObject.transform;GameObject PlayerGO = GameObject.FindWithTag(Player);PlayerTF = PlayerGO.transform;SphereCollider capsuleCollider = PlayerGO.transform.GetComponent<SphereCollider>();radius = capsuleCollider.radius;}private Vector3 LeftTarget, RightTarget;/// <summary>/// 计算切点/// </summary>private void CaculateTarget(){//向量加减的位置在原点位置Vector3 PlayertoExplosionBomo = this.transform.position - PlayerTF.position;// Debug.DrawLine(Vector3.zero, PlayertoExplosionBomo);// Debug.DrawLine(Vector3.zero, this.transform.position);// Debug.DrawLine(Vector3.zero, PlayerTF.position);//归一化后* 半径 获取 人物到炸弹的半径向量Vector3 PlayertoExplosionBomoDircetion = PlayertoExplosionBomo.normalized * radius;//归一化// Debug.DrawLine(Vector3.zero, PlayertoExplosionBomoDircetion*3,Color.cyan); //math.acos 求反余弦 (临边/chang) 弧度值-> 弧度转角度float angle = Mathf.Acos(radius / PlayertoExplosionBomo.magnitude) * Mathf.Rad2Deg;//向量旋转一个指定的欧拉角LeftTarget = PlayerTF.position+ Quaternion.Euler(0, -angle, 0) * PlayertoExplosionBomoDircetion;RightTarget = PlayerTF.position+ Quaternion.Euler(0, angle, 0) * PlayertoExplosionBomoDircetion;Debug.DrawLine(Vector3.zero, Quaternion.Euler(0, -angle, 0) * PlayertoExplosionBomoDircetion * 2, Color.cyan);Debug.DrawLine(Vector3.zero, Quaternion.Euler(0, angle, 0) * PlayertoExplosionBomoDircetion * 2, Color.cyan);Debug.DrawLine(Vector3.zero, PlayerTF.position, Color.cyan);Debug.DrawLine(Vector3.zero, LeftTarget);}private void Detchtion(){CaculateTarget();Debug.DrawLine(this.transform.position, LeftTarget, Color.red);Debug.DrawLine(this.transform.position, RightTarget, Color.blue);Debug.DrawLine(this.transform.position, PlayerTF.position, Color.yellow);}// Update is called once per framevoid Update(){Detchtion();}
}