三角形攻击区域

本文详细介绍了一种在Unity中实现角度与距离判断的技术方案。主要内容包括:通过计算物体自身Z轴与目标物体之间的角度来判断方向是否接近;计算两物体间的实际距离,结合角度判断进行目标锁定;提供了一个具体的代码示例。

 


 

前言就不铺垫了...

 

这个功能可以在很多地方用的到,而且一般面试的时候也会提到一些。

因为自己遇到过这样 的问题,并且 在当时做的时候很费劲,但是后来 做出来之后,又觉得简单的要死。

 

 

  ok,进入正题。

 

先说思路。

1、计算角度。

2、计算与物体距离。

3、判断。

4、解决

 

第一步,计算角度

 

第一我们需要计算出角度。但是我们应该如何计算角度呢?

   首先我们需要明确,计算角度需要有两条边。

第一条是我们自身物体上的Z轴方向的向量,

第二条是敌人的位置的向量。

自身Z轴的向量为transform.forward;

敌人方向的向量为(敌人.position-transform.position);

 

这里两个向量都得出后,我们需要利用Vector3的angle方法求出他们的夹角。

 

简单例子

 

public class example : MonoBehaviour {
	public Transform target;
	void Update() {
		Vector3 targetDir = target.position - transform.position;
		Vector3 forward = transform.forward;
		float angle = Vector3.Angle(targetDir, forward);
		if (angle < 5.0F)
			print("close");

	}
}
// prints "close" if the z-axis of this transform looks
// almost towards the target
//如果当前变换z轴接近目标小于5度的时候,打印"close"
var target : Transform;
function Update () {
	var targetDir = target.position - transform.position;
	var forward = transform.forward;
	var angle = Vector3.Angle(targetDir, forward);
	if (angle < 5.0)
		print("close");
}

OK第一步完成。

 

 

第二步,计算距离。

 

或者直接用unity中自带的方法。

public Transform A_OBJ;//公开对象A任意对象
public Transform B_OBJ;//公开对象B摄像机

public float Distance_C;

Distance_C = Vector3.Distance(A_OBJ.position, B_OBJ.position);

得出距离。

 

公式为num=(B.x-A.x)*(B.y-A.y)*(B.z-A.z)

 

得出两个值 之后就简单了,判断喽。

 

 

 

 

 

 

第三步,判断角度与距离。

 

 

 

第四步,就OK了。

 

 

这里上一下代码,代码中有注释,应该很好理解。

 

 

 

using UnityEngine;
using System.Collections;

public class Test009 : MonoBehaviour {



    public Transform T001;
    public Transform T002;
    public Transform enemy003;


    private Vector3 forward;
    private Vector3 V001;
    private Vector3 V002;

    private Vector3 E003;

    //基于自己Z轴与001的角度
    private float M_001_angle;



    //基于自己Z轴与Enemy003的角度
    private float M_E003_angle;








    //以下为记录长度距离
    private float E_1;



	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

        //基于自己Z轴的向量
         forward = transform.forward;

        //基于自己Z轴与T001的向量-----------角度
         V001 = T001.position - transform.position;
         M_001_angle = Vector3.Angle(V001,forward);
        // print(M_001_angle);



//-----------------------------------------------------------

        //基于自己Z轴与T002的向量-----------角度







//-----------------------------------------------------------

        //基于自己Z轴与Enemy的向量--------------角度
         E003 = enemy003.position - transform.position;
         M_E003_angle = Vector3.Angle(E003, forward);
         //print(M_E003_angle);

//判断位置

         Vector3 E11p = enemy003.transform.position;

         //(自己)  与E的距离
         E_1 = (E11p.x - forward.x) * (E11p.x - forward.x) + (E11p.y - forward.y) * (E11p.y - forward.y) + (E11p.z - forward.z) * (E11p.z - forward.z);

         print(E_1);



        //判断角度
         if (M_E003_angle <= 30)
         {
             if (E_1 <= 60)
             {
                 print("进入角度,干他!!");
             }
         }
         
         


	
	}
}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen7474

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值