攻击动画

This commit is contained in:
wulongxiao 2024-12-14 23:20:36 +08:00
parent 5e92a0630d
commit d266869186
3 changed files with 16 additions and 20 deletions

View File

@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Debug = UnityEngine.Debug; using Debug = UnityEngine.Debug;
@ -54,14 +55,18 @@ public class Attack : MonoBehaviour
Role targetRole = collider.GetComponent<Role>(); Role targetRole = collider.GetComponent<Role>();
if (targetRole && targetRole.camp != role.camp) if (targetRole && targetRole.camp != role.camp)
{ {
Debug.Log("检测到碰撞器: " + collider.name); role.animationHighlight = 1;
attack(targetRole); attack(targetRole);
lastAttackTime = Time.time; // 更新上次攻击时间 lastAttackTime = Time.time; // 更新上次攻击时间
break; // 只攻击一个目标 break; // 只攻击一个目标
} }
else
{
role.animationHighlight = 0;
} }
} }
// Debug.Log("=====--");
}
await Task.Delay(100); // 延迟,避免过于频繁地检测 await Task.Delay(100); // 延迟,避免过于频繁地检测
} }
else else
@ -73,12 +78,15 @@ public class Attack : MonoBehaviour
public void attack(Role targetRole) public void attack(Role targetRole)
{ {
if (bulletPrefab == null) if (bulletPrefab == null)
{ {
Debug.LogError("子弹预制体为空"); Debug.LogError("子弹预制体为空");
return; return;
} }
Vector2 direction = (targetRole.transform.position - transform.position).normalized; Vector2 direction = (targetRole.transform.position - transform.position).normalized;
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying) if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
@ -103,7 +111,6 @@ public class Attack : MonoBehaviour
BulletGamobj.transform.position = transform.position; BulletGamobj.transform.position = transform.position;
bulltes.Add(BulletGamobj); bulltes.Add(BulletGamobj);
} }
} }
} }

View File

@ -110,7 +110,7 @@ public class Bullet : MonoBehaviour
if (myBulletType!=BulletType.Spraying) if (myBulletType!=BulletType.Spraying)
{ {
attackObj.bulltes.Remove(this.gameObject); attackObj.bulltes.Remove(this.gameObject);
Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count); //Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count);
Destroy(this.gameObject); Destroy(this.gameObject);
} }
} }
@ -119,14 +119,14 @@ public class Bullet : MonoBehaviour
private void OnTriggerExit2D(Collider2D collision) private void OnTriggerExit2D(Collider2D collision)
{ {
Debug.Log("进入检测范围"); //Debug.Log("进入检测范围");
Role Crole = collision.gameObject.GetComponent<Role>(); Role Crole = collision.gameObject.GetComponent<Role>();
if (Crole) if (Crole)
{ {
if (Crole.camp != role.camp) if (Crole.camp != role.camp)
{ {
attackObj.bulltes.Remove(this.gameObject); attackObj.bulltes.Remove(this.gameObject);
Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count); //Debug.Log("对象已成功移除。当前列表数量:" + attackObj.bulltes.Count);
Destroy(this.gameObject); Destroy(this.gameObject);
} }
} }

View File

@ -43,6 +43,7 @@ public class Role : Fun
[Header("hp填充")] public Image Hpfiil; [Header("hp填充")] public Image Hpfiil;
[Header("扣血填充")] public GameObject HpTextPrefab; [Header("扣血填充")] public GameObject HpTextPrefab;
[Header("自己的画布")]public Canvas _Canvas; [Header("自己的画布")]public Canvas _Canvas;
public float Hp public float Hp
{ {
get => hp; get => hp;
@ -123,14 +124,10 @@ public class Role : Fun
if (Application.isPlaying) if (Application.isPlaying)
{ {
if (attackClass) if (attackClass)
{ {
attackClass.role = this; attackClass.role = this;
} }
UpdateBuff(); UpdateBuff();
//Navigation.MoveToNextWaypoint(gameObject); //Navigation.MoveToNextWaypoint(gameObject);
} }
@ -167,7 +164,6 @@ public class Role : Fun
return; return;
}; };
OnAnimationStart?.Invoke(animationHighlight); OnAnimationStart?.Invoke(animationHighlight);
CurrentIndex = 0;
var lsanimationHighlight = animationHighlight; var lsanimationHighlight = animationHighlight;
foreach (Sprite sprite in LightSprite) foreach (Sprite sprite in LightSprite)
{ {
@ -187,8 +183,6 @@ public class Role : Fun
OnAnimationIng?.Invoke(animationHighlight); OnAnimationIng?.Invoke(animationHighlight);
await Task.Delay(AnimationTree[animationHighlight].CharacterAnimationFrameInterval); await Task.Delay(AnimationTree[animationHighlight].CharacterAnimationFrameInterval);
} }
CurrentIndex++;
} }
else else
{ {
@ -200,6 +194,7 @@ public class Role : Fun
if (lsanimationHighlight == dieIndex) if (lsanimationHighlight == dieIndex)
{ {
SpawnPool.intance.ReturnEnemyToPool(this.gameObject); // 回收当前敌人到池中 SpawnPool.intance.ReturnEnemyToPool(this.gameObject); // 回收当前敌人到池中
ResetAllStatus(); ResetAllStatus();
} }
@ -294,13 +289,7 @@ public class Role : Fun
mycollider.enabled = true; // 启用碰撞体 mycollider.enabled = true; // 启用碰撞体
} }
// 如果有其他需要重置的状态,可以在这里添加 // 如果有其他需要重置的状态,可以在这里添加
} }
} }