(1)点积判断
Vector3 forward = hero_a.forward;
Vector3 direction = hero_b.position - hero_a.position;
float dot = Vector3.Dot(forward,direction);
if (dot >0)
{
}
(2)水平方向投影判断
float horizontal = Vector3.Project(direction, hero_a.right).magnitude;//水平方向投影
if (horizontal <= length/2)
{
}
(3)垂直方向投影判断
float vertical = Vector3.Project(direction, hero_a.forward).magnitude;//垂直方向投影
if (vertical <= width)
{
}
(4)绘制攻击范围
private void OnDrawGizmos()
{
Handles.color = isShow ? Color.cyan : Color.red;
Vector3 cornerA = hero_a.position + Vector3.left * width * .5f;
Vector3 cornerB = hero_a.position + Vector3.right * width * .5f;
Vector3 cornerC = cornerB + Vector3.forward * length;
Vector3 cornerD = cornerA + Vector3.forward * length;
Handles.DrawLine(cornerA, cornerB);
Handles.DrawLine(cornerB, cornerC);
Handles.DrawLine(cornerC, cornerD);
Handles.DrawLine(cornerD, cornerA);
}
完整脚本:
using UnityEngine;
using UnityEditor;
public class SkillRang : MonoBehaviour
{
public float length = 1f;//矩形长
public float width = 1f;//矩形宽
private bool isShow = false;//是否处于攻击范围
private Transform hero_a;//我方英雄
private Transform hero_b;//敌方英雄
// Start is called before the first frame update
void Start()
{
hero_a = GameObject.Find("hero_a").transform;
hero_b = GameObject.Find("hero_b").transform;
}
// Update is called once per frame
void Update()
{
RectRang();
}
private void RectRang()
{
//方形区域
Vector3 forward = hero_a.forward;
Vector3 direction = hero_b.position - hero_a.position;
float dot = Vector3.Dot(forward,direction);
if (dot >0)
{
//在前方
float vertical = Vector3.Project(direction, hero_a.forward).magnitude;//垂直方向投影
float horizontal = Vector3.Project(direction, hero_a.right).magnitude;//水平方向投影
if (vertical <= width && horizontal <= length/2)
{
isShow = true;
return;
}
}
isShow = false;
}
private void OnDrawGizmos()
{
Handles.color = isShow ? Color.cyan : Color.red;
Vector3 cornerA = hero_a.position + Vector3.left * length * .5f;
Vector3 cornerB = hero_a.position + Vector3.right * length * .5f;
Vector3 cornerC = cornerB + Vector3.forward * width;
Vector3 cornerD = cornerA + Vector3.forward * width;
Handles.DrawLine(cornerA, cornerB);
Handles.DrawLine(cornerB, cornerC);
Handles.DrawLine(cornerC, cornerD);
Handles.DrawLine(cornerD, cornerA);
}
}
圆形攻击范围
释放一个技能,当攻击范围为一个矩形(半径为r)时,判断敌方是否受到伤害,需满足一个条件:(1)敌人处在一个圆内或者圆上。该圆是以释放技能处未圆心,r为半径的圆。游戏中场景转换成如下示意图:微信搜索公众号 [爱上游戏开发],回复 “资料”,免费领取 200G 学习资料!
翻译成数学语言:(1)
假设攻击范围半径为radius
(1)距离计算
Vector3 pos_a = hero_a.position;
Vector3 pos_b = hero_b.position;
float distence = Mathf.Sqrt((pos_a.x-pos_b.x)*(pos_a.x-pos_b.x)+(pos_a.y-pos_b.y)*(pos_a.y-pos_b.y));
(2)距离判断
if (distence <= radius)
{
}
(3)绘制攻击范围
private void OnDrawGizmos()
{
Handles.color = isShow ? Color.cyan : Color.red;
Handles.CircleHandleCap(0,hero_a.position,hero_a.rotation * Quaternion.LookRotation(Vector3.up),radius,EventType.Repaint);
}
完整脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class SkillRang : MonoBehaviour
{
public float radius = 1f;//圆形半径
private bool isShow = false;//是否处于攻击范围
private Transform hero_a;//我方英雄
private Transform hero_b;//敌方英雄
// Start is called before the first frame update
void Start()
{
hero_a = GameObject.Find("hero_a").transform;
hero_b = GameObject.Find("hero_b").transform;
}
// Update is called once per frame
void Update()
{
CricleRang();
}
private void CricleRang()
{
// 圆形区域
Vector3 pos_a = hero_a.position;
Vector3 pos_b = hero_b.position;
float distence = Mathf.Sqrt((pos_a.x-pos_b.x)*(pos_a.x-pos_b.x)+(pos_a.y-pos_b.y)*(pos_a.y-pos_b.y));
if (distence <= radius)
{
isShow = true;
}
else
{
isShow = false;
}
}
private void OnDrawGizmos()
{
Handles.color = isShow ? Color.cyan : Color.red;
Handles.CircleHandleCap(0,hero_a.position,hero_a.rotation * Quaternion.LookRotation(Vector3.up),radius,EventType.Repaint);
}
}
工程源码:https://gitee.com/shirln/skill-rang注意:本文参考自https://blog.csdn.net/qq_42139931/article/details/120712908