一些额外功能的铺垫

public class Health : MonoBehaviour
{
    public Animator[] healthItem;
    public Animator geo;

    // Start is called before the first frame update
    void Start()
    {
  。
    }

    // Update is called once per frame
    public void Hurt()
    {
        // 当调用 Hurt 方法时,触发第一个 healthItem 动画的 "Hurt" 触发器
        healthItem[0].SetTrigger("Hurt");
    }

    public IEnumerator ShowHealthItems()
    {
        // 用协程逐个显示 healthItem 数组中的动画
        for (int i = 0; i < healthItem.Length; i++)
        {
            healthItem[i].SetTrigger("Respawn");
            yield return new WaitForSeconds(0.2f);
        }
        // 等待0.2秒后,播放 geo 动画的 "Enter" 状态
        yield return new WaitForSeconds(0.2f);
        geo.Play("Enter");
    }

    public void HideHealthItems()
    {
        // 播放 geo 动画的 "Exit" 状态
        geo.Play("Exit");
        // 逐个触发 healthItem 数组中动画的 "Hide" 触发器
        for (int i = 0; i < healthItem.Length; i++)
        {
            healthItem[i].SetTrigger("Hide");
        }
    }
}

代码功能解析:

1.变量声明和赋值:


2.Animator[] healthItem;:这是一个 Animator 类型的数组,用于存储健康物品的动画组件。
3.Animator geo;:这是一个单独的 Animator 类型变量,可能用于管理整体健康系统的动画效果。
6.Hurt 方法:


7.public void Hurt() 方法,当调用时会触发 healthItem 数组中第一个元素(索引为0)的动画,通过设置 "Hurt" 触发器。


8.ShowHealthItems 协程方法:


9.public IEnumerator ShowHealthItems() 是一个协程方法,用于逐个显示 healthItem 数组中的动画。
10.使用 SetTrigger("Respawn") 方法触发每个 healthItem 的 "Respawn" 触发器。
11.每次触发后,通过 yield return new WaitForSeconds(0.2f); 等待0.2秒,以实现逐个显示的效果。
12.完成显示后,再等待0.2秒后,播放 geo 对象的 "Enter" 动画状态。


13.HideHealthItems 方法:


14.public void HideHealthItems() 方法,用于隐藏所有的健康物品动画。
15.先播放 geo 对象的 "Exit" 动画状态。
16.然后逐个触发 healthItem 数组中每个动画的 "Hide" 触发器,以实现隐藏效果。

总结:
这段代码通过 Unity 中的 Animator 组件管理了健康系统的动画效果,包括受伤效果 (Hurt() 方法)、显示健康物品 (ShowHealthItems() 方法) 和隐藏健康物品 (HideHealthItems() 方法)。使用了协程来逐个控制动画的显示,并通过触发器来管理动画状态的切换,从而实现了一套健康系统的动画逻辑。

这段代码是一个2D游戏中的角色或物体的碰撞处理和一些额外金币掉落功能的实现。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemy : Breakable 
{
    int ranDomCount;
    
    GameObject coin;
    [SerializeField] protected int minSpawnCoins = 2;
    [SerializeField] protected  bool isFacingRight;
    [SerializeField] protected int maxSpawnCoins = 5;
    [SerializeField] protected float maxBumpXForce = 100;
    [SerializeField] protected float minBumpYForce = 300;
    [SerializeField] protected float maxBumpYForce = 500;
    [SerializeField] float moveX;
    Vector3 flippedScale = new Vector3(-1, 1, 1);

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }


   protected  private void Direction()
    {
        if (moveX > 0)
        {
            transform.localScale = flippedScale;

        }
        else if (moveX < 0)
        {
            transform.localScale = Vector3.one;
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        DetectCollisionEnter2D(collision);
    }
    protected virtual void  DetectCollisionEnter2D(Collision2D collision  )
    {
        if(collision .gameObject.layer == LayerMask.NameToLayer("HeroDetector")) 
                {
            FindAnyObjectByType<NewBehaviourScript>().Takedamage();
        }
        if(isDead&&collision.gameObject.layer==LayerMask.NameToLayer("Terrain")     )
        {
            GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
            GetComponent<BoxCollider2D>().enabled = false;
        }
    }
    protected override void  Dead()
    {
        base.Dead();
        SpawnCoins();
    }
    public virtual void SpawnCoins()
    {
        ranDomCount = Random.Range(minSpawnCoins, maxSpawnCoins);
        for(int i=0;i<ranDomCount;i++)
        {
            SpawnCoins方法:
17.
18.
csharp
19.
public virtual void SpawnCoins()
{
    ranDomCount = Random.Range(minSpawnCoins, maxSpawnCoins);
    for (int i = 0; i < ranDomCount; i++)
    {
        GameObject geo = Instantiate(coin, transform.position, Quaternion.identity, transform.parent);
        Vector2 force = new Vector2(Random.Range(-maxBumpXForce, maxBumpXForce), Random.Range(minBumpYForce, maxBumpYForce));
        geo.GetComponent<Rigidbody2D>().AddForce(force, ForceMode2D.Impulse);
    }
}
20.
这个方法生成一定数量的金币(或其他物品),并将它们随机散布在物体的位置附近。
每个金币都会添加一个随机的冲量,使它们看起来像是从物体周围的位置弹射出去。
综上所述,这段代码是一个基础的Unity脚本,处理物体的碰撞事件,并在物体死亡时生成金币。它利用了Unity的2D物理系统和随机数生成功能来实现游戏中的一些基本功能。

 

  • 32
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值