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;
}
}
球形范围判定
最新推荐文章于 2024-01-12 19:00:11 发布