查看: 1829|回复: 0
打印 上一主题 下一主题

[经验分享] 游戏框架-技能范围检测

[复制链接]
may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52304
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2018-12-31 20:53:48 |只看该作者 |倒序浏览

來自:UnityTerminator
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;

  4. public class SkillRange  {
  5.         /// <summary>
  6.         /// 扇形范围判定
  7.         /// </summary>
  8.         /// <returns><c>true</c>, if range was curved, <c>false</c> otherwise.</returns>
  9.         /// <param name="self">Player.</param>
  10.         /// <param name="enemy">Enemy.</param>
  11.         /// <param name="maxDiatance">Max diatance.</param>
  12.         /// <param name="maxAngle">Max angle.</param>
  13.         public static bool CurveRange(Transform self,Transform enemy,float maxDiatance,float maxAngle){
  14.                 Vector3 playerDir = self.forward;
  15.                 Vector3 enemydir = (enemy.position - self.position).normalized;
  16.                 float angle = Vector3.Angle (playerDir, enemydir);
  17.                 float distance = Vector3.Distance (enemy.position, self.position);
  18.                 if (angle <= maxAngle / 2 && distance <= maxDiatance) {
  19.                         return true;
  20.                 } else {
  21.                         return false;
  22.                 }
  23.         }
  24.         /// <summary>
  25.         /// 圆形范围判定
  26.         /// </summary>
  27.         /// <returns><c>true</c>, if range was curved, <c>false</c> otherwise.</returns>
  28.         /// <param name="self">Player.</param>
  29.         /// <param name="enemy">Enemy.</param>
  30.         /// <param name="maxDiatance">Max diatance.</param>
  31.         public static bool CurveRange(Transform self,Transform enemy,float maxDiatance){
  32.                 float distance = Vector3.Distance (enemy.position, self.position);
  33.                 if (distance <= maxDiatance) {
  34.                         return true;
  35.                 } else {
  36.                         return false;
  37.                 }
  38.         }

  39.     /// <summary>
  40.     /// 矩形范围判定
  41.     /// </summary>
  42.     /// <param name="self"></param>
  43.     /// <param name="enemy"></param>
  44.     /// <param name="maxWidth"></param>
  45.     /// <param name="maxHeight"></param>
  46.     /// <returns></returns>
  47.     public static bool SquareRange(Transform self, Transform enemy, float maxWidth, float maxHeight)
  48.     {
  49.         Vector3 enemyDir = enemy.position - self.position;
  50.         float distance=Vector3.Distance(enemy.position,self.position);
  51.         
  52.         float angle = Vector3.Angle(enemyDir, self.forward);
  53.         if ( angle<= 90)
  54.         {
  55.             float z = distance * Mathf.Cos(angle * Mathf.Deg2Rad);
  56.             float x = distance * Mathf.Sin(angle * Mathf.Deg2Rad);
  57.             if (x<=maxWidth / 2 && z<=maxHeight)
  58.             {
  59.                 return true;
  60.             }
  61.         }
  62.         return false;
  63.     }
  64. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-25 19:22 , Processed in 0.085888 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部