Unity3D角色攻击范围判定和攻击判定

原地址:http://www.unity蛮牛.com/blog-1801-479.html

第一种方法:运用点乘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// <summary>
  
/// Checks the can attakc.
  
/// </summary>
  
/// <returns><c>true</c>, if can attakc was checked, <c>false</c> otherwise.</returns>
  
/// <param name=“enemy”> 敌人对象</param>
  
/// <param name=“_dis”>攻击范围</param>
  
public bool CheckCanAttack(GameObject enemy , float _dis)
  
{
  
1
  
Vector3 toOther = enemy.transform.position - myTransform.position;
  
dot = Vector3.Dot(forward , toOther);
  
if(dot>0.5f && _dis * _dis<2 * dot * dot )
  
{
  
if(!canAttackEnemy.Contains(enemy.GetComponent<Enemy>()))
  
canAttackEnemy.Add(enemy.GetComponent<Enemy>());
  
return true ;
  
}else{
  
if(canAttackEnemy.Contains(enemy.GetComponent<Enemy>()))
  
canAttackEnemy.Remove(enemy.GetComponent<Enemy>());
  
return false ;
  
}
  
}
 
 
 
第二种方法:运用触发器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void OnTriggerEnter(Collider other)
  
{
  
if(other.CompareTag(“Enemy”))
  
{
  
listEnemy.Add(other.gameObject);
  
Debug.Log(“Count:”+listEnemy.Count);
  
}
  
}
  
void OnTriggerExit(Collider other)
  
{
  
if(other.CompareTag(“Enemy”))
  
{
  
listEnemy.Remove(other.gameObject);
  
Debug.Log(“Count:”+listEnemy.Count);
  
}
  
}
 
 
 
当然这个触发器可以用多个基础模型拼出你想要的区域

==========================我是分割线=============================

下面来介绍一下我想出的几种攻击判定方法

第一种:给武器添加碰撞器

个人感觉这个比较耗资源,就是在挥动武器的时候与敌人碰撞

第二种:利用上面介绍的范围判定加上动画事件去做攻击判定

这个方法也是我现在正在用的方法

关于动画事件这个东西我在这里就不介绍了,大家去问度娘吧

原理就是当动画执行到某一帧的情况下添加事件判断敌人是否在攻击范围内,

如果在敌人响应被攻击的方法

 

原文地址:https://www.cnblogs.com/123ing/p/3911632.html