U3D 战斗场景中怪物位置的箭头指示
2014-09-11 11:10
337 查看
在游戏中的战斗场景中 每个怪物都有一个行为控制器(一个控制器就是一个gameobject)
MLActionController 继承自MonoBehaviour 当MonoBehaviour启用时,其Update在每一帧被调用。在此函数中可以判断怪物是否在玩家视野范围内以屏幕的一半为基准
void ScreenVisibleUpdate()
{
if (!IsPlayerSelf
&& !IsBreakItem
&& IsActive)
{
Vector3 monsterPosition = Util.WorldToScreenPoint(ThisTransform.position);
if (MLLevelManager.Singleton.Hero)
{
Vector3 temp = MLLevelManager.Singleton.Hero.ThisTransform.position;
Vector3 HeroPosition = Util.WorldToScreenPoint(temp);
if ((Math.Abs(HeroPosition.x - monsterPosition.x) > (Screen.width / 2 + Screen.width * 10 / 200))
|| (Math.Abs(monsterPosition.y - HeroPosition.y) > (Screen.height / 2 + Screen.height * 10 / 200)))
{
if (!IsPlayerPartner)
{
OnACBecameInvisible(monsterPosition, true);
}
else
{
OnACBecameInvisible(monsterPosition, false);
}
}
else
{
OnACBecameVisible(monsterPosition);
}
}
}
}
////////////////怪物进视野或者出视野的判定
public void OnACBecameVisible(Vector3 Monsterposition)
{
if (!IsPlayerSelf
&& IsActive)
{
MLSceneArrow win = MLUIManager.Singleton.FindUIController<MLSceneArrow>();
win.DeleteMonster(ThisObject);
}
}
//出视野
public void OnACBecameInvisible(Vector3 Monsterposition, bool IfMonster)
{
if (!IsPlayerSelf
&& IsActive
&& IsAlived)
{
MLSceneArrow win = MLUIManager.Singleton.FindUIController<MLSceneArrow>();
win.InitDlg();
win.AddMonster(ThisObject, Monsterposition, IfMonster);
}
}
//////////////////在scenearrow的UI中对于箭头的显示 因为重复性 所以选用了prefabs
GameObject ArrowObject = MLResourceLoader.Instance.LoadAsset("UIPrefab/Logics/monster_arrow", typeof(GameObject)) as GameObject;; //箭头
///根据怪物位置的箭头转向处理函数
public void AddMonster(GameObject Monster, Vector3 MonsterPosition,bool IfMonster)
{
Vector3 HeroPosition = Camera.main.WorldToScreenPoint(MLLevelManager.Singleton.Hero.ThisTransform.position);
double x = MonsterPosition.x - HeroPosition.x;
double y = MonsterPosition.y - HeroPosition.y;
//MLResourceLoader.Instance.LoadAsset("UIPrefab/Logics/monster_arrow", typeof(MLImageComponent));
if (!_monsterArrow.ContainsKey(Monster))
{
GameObject obj;
if (IfMonster)
{
obj = MLPrefabInstancePool<MLPrefabInstanceQueuePersistant>.Singleton.Instanciate(ArrowObject);
}
else
{
obj = MLPrefabInstancePool<MLPrefabInstanceQueuePersistant>.Singleton.Instanciate(ParnterObject);
}
if(obj != null)
{
MLImageComponent imageObj;
imageObj = obj.GetComponent<MLImageComponent>();
_monsterArrow.Add(Monster, imageObj);
}
}
int HalfWidth = _mwindth / 2;
int HalfHight = _mHight / 2;
float MinusX = Math.Abs(MonsterPosition.x - HeroPosition.x);
float MinusY = Math.Abs(MonsterPosition.y - HeroPosition.y);
if(_monsterArrow[Monster] != null)
{
double a1 = Math.Atan(Math.Abs(y / x));
Vector3 postion = new Vector3();
Vector3 rot = Vector3.zero;
//8象限计算
if ((MonsterPosition.x >= HeroPosition.x) && MinusY < HalfHight)
{
postion.x = _mwindth;
postion.y = MonsterPosition.y - HeroPosition.y;
rot.z = 0;
}
if((MonsterPosition.y > HeroPosition.y) &&(MinusX < HalfWidth))
{
postion.x = MonsterPosition.x - HeroPosition.x;
postion.y = _mHight;
rot.z = 90;
}
if((MonsterPosition.x < HeroPosition.x) &&(MinusY < HalfHight))
{
postion.x = 0 - _mwindth;
postion.y = MonsterPosition.y - HeroPosition.y;
rot.z = 180;
}
if((MonsterPosition.y < HeroPosition.y) && (MinusX < HalfWidth))
{
postion.x = MonsterPosition.x - HeroPosition.x;
postion.y = 0 - _mHight;
rot.z = 270;
}
if(MonsterPosition.x > HeroPosition.x && MonsterPosition.y > HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = _mwindth;
postion.y = _mHight;
rot.z = (float)((a1 * 180) / Math.PI); //旋转角度
}
if(MonsterPosition.x < HeroPosition.x && MonsterPosition.y > HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = 0 - _mwindth;
postion.y = _mHight;
rot.z = (float)((Math.PI - a1) * 180 / Math.PI);
}
if(MonsterPosition.x < HeroPosition.x && MonsterPosition.y < HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = 0 - _mwindth;
postion.y = 0 - _mHight;
rot.z = (float)((Math.PI + a1) * 180 / Math.PI);
}
if(MonsterPosition.x > HeroPosition.x && MonsterPosition.y < HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = _mwindth;
postion.y = 0 - _mHight;
rot.z = (float)((2 * Math.PI - a1) * 180 / Math.PI);
}
_monsterArrow[Monster].transform.parent = mEmptyPanel.Root.transform;
_monsterArrow[Monster].transform.localPosition = postion;
Vector3 Scale= new Vector3(0.75f,0.75f,0.75f);
_monsterArrow[Monster].transform.localScale = Scale;
_monsterArrow[Monster].SetLocalRotation(rot);
}
}
MLActionController 继承自MonoBehaviour 当MonoBehaviour启用时,其Update在每一帧被调用。在此函数中可以判断怪物是否在玩家视野范围内以屏幕的一半为基准
void ScreenVisibleUpdate()
{
if (!IsPlayerSelf
&& !IsBreakItem
&& IsActive)
{
Vector3 monsterPosition = Util.WorldToScreenPoint(ThisTransform.position);
if (MLLevelManager.Singleton.Hero)
{
Vector3 temp = MLLevelManager.Singleton.Hero.ThisTransform.position;
Vector3 HeroPosition = Util.WorldToScreenPoint(temp);
if ((Math.Abs(HeroPosition.x - monsterPosition.x) > (Screen.width / 2 + Screen.width * 10 / 200))
|| (Math.Abs(monsterPosition.y - HeroPosition.y) > (Screen.height / 2 + Screen.height * 10 / 200)))
{
if (!IsPlayerPartner)
{
OnACBecameInvisible(monsterPosition, true);
}
else
{
OnACBecameInvisible(monsterPosition, false);
}
}
else
{
OnACBecameVisible(monsterPosition);
}
}
}
}
////////////////怪物进视野或者出视野的判定
public void OnACBecameVisible(Vector3 Monsterposition)
{
if (!IsPlayerSelf
&& IsActive)
{
MLSceneArrow win = MLUIManager.Singleton.FindUIController<MLSceneArrow>();
win.DeleteMonster(ThisObject);
}
}
//出视野
public void OnACBecameInvisible(Vector3 Monsterposition, bool IfMonster)
{
if (!IsPlayerSelf
&& IsActive
&& IsAlived)
{
MLSceneArrow win = MLUIManager.Singleton.FindUIController<MLSceneArrow>();
win.InitDlg();
win.AddMonster(ThisObject, Monsterposition, IfMonster);
}
}
//////////////////在scenearrow的UI中对于箭头的显示 因为重复性 所以选用了prefabs
GameObject ArrowObject = MLResourceLoader.Instance.LoadAsset("UIPrefab/Logics/monster_arrow", typeof(GameObject)) as GameObject;; //箭头
///根据怪物位置的箭头转向处理函数
public void AddMonster(GameObject Monster, Vector3 MonsterPosition,bool IfMonster)
{
Vector3 HeroPosition = Camera.main.WorldToScreenPoint(MLLevelManager.Singleton.Hero.ThisTransform.position);
double x = MonsterPosition.x - HeroPosition.x;
double y = MonsterPosition.y - HeroPosition.y;
//MLResourceLoader.Instance.LoadAsset("UIPrefab/Logics/monster_arrow", typeof(MLImageComponent));
if (!_monsterArrow.ContainsKey(Monster))
{
GameObject obj;
if (IfMonster)
{
obj = MLPrefabInstancePool<MLPrefabInstanceQueuePersistant>.Singleton.Instanciate(ArrowObject);
}
else
{
obj = MLPrefabInstancePool<MLPrefabInstanceQueuePersistant>.Singleton.Instanciate(ParnterObject);
}
if(obj != null)
{
MLImageComponent imageObj;
imageObj = obj.GetComponent<MLImageComponent>();
_monsterArrow.Add(Monster, imageObj);
}
}
int HalfWidth = _mwindth / 2;
int HalfHight = _mHight / 2;
float MinusX = Math.Abs(MonsterPosition.x - HeroPosition.x);
float MinusY = Math.Abs(MonsterPosition.y - HeroPosition.y);
if(_monsterArrow[Monster] != null)
{
double a1 = Math.Atan(Math.Abs(y / x));
Vector3 postion = new Vector3();
Vector3 rot = Vector3.zero;
//8象限计算
if ((MonsterPosition.x >= HeroPosition.x) && MinusY < HalfHight)
{
postion.x = _mwindth;
postion.y = MonsterPosition.y - HeroPosition.y;
rot.z = 0;
}
if((MonsterPosition.y > HeroPosition.y) &&(MinusX < HalfWidth))
{
postion.x = MonsterPosition.x - HeroPosition.x;
postion.y = _mHight;
rot.z = 90;
}
if((MonsterPosition.x < HeroPosition.x) &&(MinusY < HalfHight))
{
postion.x = 0 - _mwindth;
postion.y = MonsterPosition.y - HeroPosition.y;
rot.z = 180;
}
if((MonsterPosition.y < HeroPosition.y) && (MinusX < HalfWidth))
{
postion.x = MonsterPosition.x - HeroPosition.x;
postion.y = 0 - _mHight;
rot.z = 270;
}
if(MonsterPosition.x > HeroPosition.x && MonsterPosition.y > HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = _mwindth;
postion.y = _mHight;
rot.z = (float)((a1 * 180) / Math.PI); //旋转角度
}
if(MonsterPosition.x < HeroPosition.x && MonsterPosition.y > HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = 0 - _mwindth;
postion.y = _mHight;
rot.z = (float)((Math.PI - a1) * 180 / Math.PI);
}
if(MonsterPosition.x < HeroPosition.x && MonsterPosition.y < HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = 0 - _mwindth;
postion.y = 0 - _mHight;
rot.z = (float)((Math.PI + a1) * 180 / Math.PI);
}
if(MonsterPosition.x > HeroPosition.x && MonsterPosition.y < HeroPosition.y && MinusX > HalfWidth && MinusY > HalfHight)
{
postion.x = _mwindth;
postion.y = 0 - _mHight;
rot.z = (float)((2 * Math.PI - a1) * 180 / Math.PI);
}
_monsterArrow[Monster].transform.parent = mEmptyPanel.Root.transform;
_monsterArrow[Monster].transform.localPosition = postion;
Vector3 Scale= new Vector3(0.75f,0.75f,0.75f);
_monsterArrow[Monster].transform.localScale = Scale;
_monsterArrow[Monster].SetLocalRotation(rot);
}
}
相关文章推荐
- UGUI 实现屏幕外怪物的指示箭头
- 战斗场景制作之怪物的AI
- U3D 场景快速搭建教程
- 不能使用箭头函数的场景
- this.movingBg() 战斗场景地图移动 主场景地图围绕人移动
- 动态获取设置提示框和小箭头的位置
- u3d引擎移动都有哪些方法?又都适用于什么场景?
- unity3d 3d场景中人物的2d头像/血条位置计算
- 方便的弹出视图,可随意定义箭头方向,自动计算位置
- [QGLViewer]3D场景鼠标点击位置
- 基于U3D的VR开发教程:(3)基础场景搭建
- 如何设置Android studio 3.0显示光标返回上一次浏览位置的箭头图标
- cocos2dx3.2开发 RPG《Flighting》(七)回到战斗场景,更新角色的叠放次序
- Android Studio 3.0显示光标返回上一次浏览位置的箭头图标消失了怎么办
- 场景中所有对象的相对位置如何整体迁移
- ExtJS下拉列表箭头位置在左
- u3d资源打包只能打包场景材质,不能打包脚本
- 3.4.1_指示文本输入位置的光标
- U3D中对物体的平移旋转缩放位置操作
- U3d菜单栏变化:Camera的Smooth Follow属性位置,U3d自带地势资源包的导入,天空盒设置