球形范围判定

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class S4Player : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.P))
		{
			AttackEnemy();
		}
		
	}


	void AttackEnemy()
	{
		//获取在圈圈里面的人物
	Collider[] _colliders=	Physics.OverlapSphere(transform.position, 5);
		//   符合要求的敌人列表
		List<GameObject> AttackdList = new List<GameObject>();
		//筛选完毕
		foreach (var v in _colliders) //var类型不同于int string float 除了这三种类型 别的用var
		{
			//判断是否在攻击角度里面 
			if (UmbrellaAttact(transform, v.transform, 60, 5)) //if (UmbrellaAttact(transform, v.transform, 60, 5))
			{
				AttackdList.Add(v.gameObject);   
				Debug.LogError(v.name);  
			}
		}
//进行伤害计算
		foreach (GameObject o in AttackdList)
		{
			//获取距离
			float dis = Vector3.Distance(transform.position, o.transform.position);
//计算减血数值多少
			float blood = 5 * 10 / dis;
			
			//进行减血
			o.GetComponent<S4Enemy>().Attacked(blood);  

		}

	}
	//扇形攻击代码复制就行
	private bool UmbrellaAttact( Transform  attacker ,Transform attacked  ,float  angle, float radius)
	{
        
		Vector3 deltaA = attacked.position - attacker.position;
        
		float tmpAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, attacker.forward)) * Mathf.Rad2Deg;
        
		if (tmpAngle < angle * 0.5f && deltaA.magnitude < radius)
		{
			return true;
		}
		return false;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值