游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   UnityTerminator

游戏框架-技能范围检测 热度 4

个人分类: MMO游戏设计框架 | 2018-7-9 20:37
标签:攻击范围
0 个评论 | 阅读 197 | 收藏 | 举报

[code]csharpcode:

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

public class SkillRange  {
	/// <summary>
	/// 扇形范围判定
	/// </summary>
	/// <returns><c>true</c>, if range was curved, <c>false</c> otherwise.</returns>
	/// <param name="self">Player.</param>
	/// <param name="enemy">Enemy.</param>
	/// <param name="maxDiatance">Max diatance.</param>
	/// <param name="maxAngle">Max angle.</param>
	public static bool CurveRange(Transform self,Transform enemy,float maxDiatance,float maxAngle){
		Vector3 playerDir = self.forward;
		Vector3 enemydir = (enemy.position - self.position).normalized;
		float angle = Vector3.Angle (playerDir, enemydir);
		float distance = Vector3.Distance (enemy.position, self.position);
		if (angle <= maxAngle / 2 && distance <= maxDiatance) {
			return true;
		} else {
			return false;
		}
	}
	/// <summary>
	/// 圆形范围判定
	/// </summary>
	/// <returns><c>true</c>, if range was curved, <c>false</c> otherwise.</returns>
	/// <param name="self">Player.</param>
	/// <param name="enemy">Enemy.</param>
	/// <param name="maxDiatance">Max diatance.</param>
	public static bool CurveRange(Transform self,Transform enemy,float maxDiatance){
		float distance = Vector3.Distance (enemy.position, self.position);
		if (distance <= maxDiatance) {
			return true;
		} else {
			return false;
		}
	}

    /// <summary>
    /// 矩形范围判定
    /// </summary>
    /// <param name="self"></param>
    /// <param name="enemy"></param>
    /// <param name="maxWidth"></param>
    /// <param name="maxHeight"></param>
    /// <returns></returns>
    public static bool SquareRange(Transform self, Transform enemy, float maxWidth, float maxHeight)
    {
        Vector3 enemyDir = enemy.position - self.position;
        float distance=Vector3.Distance(enemy.position,self.position);
        
        float angle = Vector3.Angle(enemyDir, self.forward);
        if ( angle<= 90)
        {
            float z = distance * Mathf.Cos(angle * Mathf.Deg2Rad);
            float x = distance * Mathf.Sin(angle * Mathf.Deg2Rad);
            if (x<=maxWidth / 2 && z<=maxHeight)
            {
                return true;
            }
        }
        return false;
    }
}

3 1

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部