unity中战利品跟随人物,设置生成次数和生成几率

本文介绍了在Unity中创建一个处理物品生成几率和数量的类LootSetting,以及一个跟随人物并可被拾取的LootItem类。当敌人死亡时,通过LootSpawner在敌人位置生成随机战利品。系统结合了随机数、动画和音频效果,为游戏添加了互动元素。
摘要由CSDN通过智能技术生成

创建处理生成次数和生成几率的类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 [System.Serializable] class LootSetting 
{
    public GameObject prefab;
    [SerializeField, Range(0f, 100f)] public float dropPercentage;//生成率
    [SerializeField, Range(0f, 100f)] public int numebr;//生成个数
    public void Spaw(Vector3 postion)
    {
        for(int i=0; i<numebr; i++)
        {
            if(Random.Range(0f,100f)<=dropPercentage)
            {
                poolManager.Release(prefab,postion);
            }
        }

    }
}

创建跟随人物的类,这个物体在人物右边,向左边人物靠近

挂在在物体预制体上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LootItem : MonoBehaviour
{
    protected  Player player;
    [SerializeField] float minSpeed;
    [SerializeField] float maxSpeed;
    [SerializeField] protected AudioData pickUpSFX;

    protected Text LootText;
    Animator animator;

    int pickUpStateID = Animator.StringToHash("PickUp");
    private void Awake()
    {
        animator = GetComponent<Animator>();
        player =FindObjectOfType<Player>();//找到挂有Player的物体的脚本
        LootText= GetComponentInChildren<Text>(true);//true 获得禁用时的组件
    }
    private void OnEnable()
    {
        StartCoroutine(nameof(MoveCorotine));
    }
    //跟随玩家移动
    IEnumerator MoveCorotine()
    {
        float speed=Random.Range(minSpeed,maxSpeed);
        Vector3 direction = Vector3.left;//向左   
        while(isActiveAndEnabled)
        {
            if(player.isActiveAndEnabled)
            {
                direction=(player.transform.position-transform.position).normalized;

            }
            transform.Translate(direction*speed*Time.deltaTime);
            yield return null;  
        }

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        PickUp();
    }
   protected virtual void PickUp()
    {
        StopAllCoroutines();
        animator.Play(pickUpStateID);
        AudioManager.instance.PlaySFXaudio(pickUpSFX); 

    }

}

创建可以生成不同战利品的类,在敌人预制体上挂着

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LootSpawner : MonoBehaviour
{
    [SerializeField] LootSetting[] lootSettings;
    public void Spaw(Vector2 position)
    {
        foreach (var item in lootSettings)
        {
            item.Spaw(position+Random.insideUnitCircle);//insideUnitCircle在单位圆范围内 取一个 随机坐标。范围为1
        }
    }
}

后面在敌人死亡时调用即可

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : Character
{
    LootSpawner lootSpawner;
    protected virtual void Awake()
    {
        lootSpawner= GetComponent<LootSpawner>();
    }

    public override void Die()
    {     

        lootSpawner.Spaw(transform.position);//生成奖品
    }



}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值