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();
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值