Unity3D效果实现:怪物的控制和怪物刷新点
https://www.cnblogs.com/TKK-YLF/p/15619268.html
看了上面这篇文章有个不解的点:
public void GetDamage(int damage)
{
if(Hp>0)
{
GetComponentInChildren<HpManager>().ShowText("-" + damage);
Hp -= damage;
}
if(Hp<=0)
{
Instantiate(PotionPre, transform.position, transform.rotation);
animator.SetTrigger("Die");
//给任务系统报告,击杀了一个ID为101的敌人
QuestManager.Instance.AddEnemy(ID);
Destroy(gameObject, 20);
}
}
方法中给任务系统报告,击杀了一个ID为101的敌人,这个id是类上写死的。当随机产生三个怪,怎么知道是哪一个怪呢?
我能想到的方法是在怪物的脚本里面随机生成一个id,然后在玩家的脚本通过碰撞体拿到怪物的脚本,最后获取这个id值。
Unity2D:鼠标选中怪物
参考如下链接即可
https://www.cnblogs.com/flyFreeZn/p/4026081.html
给选中的时候给目标添加一些 效果比如加颜色或者透明
public void SetTranspare nt(bool isTransparent)
{
spriteRenderer.color = new Color(1f, 1f, 1f, isTransparent?0.5f:1f);
}
2023年1月19日08:56:16
尝试了一下,把脚本挂载到敌人或者玩家上,运行时场景中的水塘的box collider2d和玩家身上的box collider2d都能被点击到,但是敌人的box collider2d不能被检测到,不知道为什么?
2023年1月19日09:28:41
有空尝试下如下代码看是否可行,先挂载到player上
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
switch(hit.transform.tag){
case "land":
//todo
break;
case "monster":
curMoster=hit.transform.GetComponent<Enemy>();
destPos=curMonster.transform.position;
nav.SetDestination(destPos);
ani.CrossFade("walk");
break;
}
}
2023年1月19日11:15:10
结题了,仔细检查了一下是我自己弄错了,我把敌人的碰撞写成了box collider,而不是collider2d