unity 玩家和炸弹切线计算方式
脚本挂在炸弹上!
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 frame
void Update()
{
Detchtion();
}
}