找回密码
 注册帐号

扫一扫,访问微社区

UnityTerminator

游戏框架-技能范围检测

热度 12927 2018-7-9 20:37 |个人分类:MMO游戏设计框架| 攻击范围

[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;
    }
}

发表评论 评论 (2 个评论)

回复 思维跳跃大 2018-9-13 08:32
666666666666真是太好了  刚好用得上
回复 LHD大魔王 2019-3-19 16:23

facelist doodle 涂鸦板

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