捕鱼里金币掉落的效果

沃皓轩
2023-12-01

最近做了个模仿捕鱼里金币掉落,弹两下旋转然后被炮塔吸过去的效果,这个是生成金币的脚本

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

public class Timer : MonoBehaviour
{
    GameObject canv;
    //得分
    public int score;
    //生成金币数量
    int coinNum;
    void Start()
    {       
        canv = GameObject.FindGameObjectWithTag("Canvas");
        //_coinOfY = _creat.position.y;
        //_coinOfX = _creat.position.x+100f;
        oddOffsetX = _creat.position.x;
        oddOffsetY = _creat.position.y + 100f;
        coinNum = score / 100;
        evenOffsetX = _creat.position.x;
        evenOffsetY = _creat.position.y + 100f;
        StartCoroutine(StartCon());
    }
    /// <summary>
    /// 生成点(鱼死亡位置)
    /// </summary>
    public Transform _creat;
    /// <summary>
    /// 金币预制体
    /// </summary>
    public GameObject _prefabCoin;
    //float _coinOfY,_coinOfX;
    //奇数/偶数偏移量
    float oddOffsetX, oddOffsetY, evenOffsetX, evenOffsetY;
    //当前金币
    int current;
    IEnumerator StartCon()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.05f);
            //偶数项        
            print(coinNum);
            if (coinNum % 2 == 0)
            {
                evenOffsetX += 100f;
                //evenOffsetY += 100f;
                //上排
                GameObject obj = Instantiate(_prefabCoin, new Vector2(evenOffsetX, evenOffsetY + 100f), Quaternion.identity) as GameObject;
                //下排
                GameObject obj1 = Instantiate(_prefabCoin, new Vector2(evenOffsetX, evenOffsetY), Quaternion.identity) as GameObject;
                obj.transform.parent = canv.transform;
                obj1.transform.parent = canv.transform;
                current += 2;
                print("生成" + coinNum + "个金币---");
            }
            //奇数项
            if (coinNum % 2 != 0)
            {
                //if (coinNum > 10)
                //{
                //    print("大于10");
                //}
                oddOffsetX += 100f;
                //oddOffsetY += 100f;
                GameObject obj = Instantiate(_prefabCoin, new Vector2(oddOffsetX, oddOffsetY), Quaternion.identity) as GameObject;
                obj.transform.parent = canv.transform;
                current++;
                print("生成" + coinNum + "个金币+++");

            }
            if (current > coinNum - 1)
            {
                StopAllCoroutines();
                print("准备退出");
                break;
            }
        
        }
    }
}

这个是金币的跳跃和移动

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

public class CoinMove : MonoBehaviour
{
    /// <summary>
    /// 目标位置
    /// </summary>
    Transform _target;
    /// <summary>
    /// 开始时金币生成的位置
    /// </summary>
    Vector3 _startPosition;
    void Start()
    {        
        _startPosition = transform.position;
        Init();
    }
    public void Init()
    {
        _target = GameObject.FindWithTag("Player").transform;
    }
    void Update()
    {        
        ControllerJump();        
    }
    bool _isCanJump = true;
    float _jumpCount = 2;
    float _jumpCountMax = 2;
    //高度
    public float offsetY;

    void ControllerJump()
    {        
        if (_jumpCount > 0)
        {
            if (_isCanJump)
            {
                transform.position = Vector3.MoveTowards(transform.position,
                    new Vector3(_startPosition.x, _startPosition.y + offsetY*_jumpCount/_jumpCountMax, _startPosition.z), 
                    Time.deltaTime * 800);
                //print("start Y轴为" + _startPosition.y + offsetY * _jumpCount / _jumpCountMax + "金币Y轴为" + transform.position.y);
                if (transform.position.y == _startPosition.y + offsetY * _jumpCount / _jumpCountMax)
                {
                    _isCanJump = !_isCanJump;
                    //print("_jumpCount为" + _jumpCount+"开始下落");
                }
            }
            if (!_isCanJump)
            {
                transform.position = Vector3.MoveTowards(transform.position, _startPosition,  Time.deltaTime * 800);
                //print("上升start Y轴为" + _startPosition.y + "金币Y轴为" + transform.position.y);
                if (transform.position.y == _startPosition.y)
                {
                    _isCanJump = !_isCanJump;
                    _jumpCount--;
                    //print("_jumpCount为" + _jumpCount+"开始上升");
                }
            }           
        }
        if (_jumpCount == 0)
        {
            StartCoroutine("CoinOfMove");
        }
    }
    //缩小倍率
    float ScaleNum=0.3f ;
    IEnumerator CoinOfMove()
    {
        yield return new WaitForSeconds(0.8f);
        transform.position = Vector3.MoveTowards(transform.position, _target.position, Time.deltaTime * 1500);
        transform.localScale -= new Vector3(ScaleNum * Time.deltaTime, ScaleNum * Time.deltaTime, ScaleNum * Time.deltaTime);
    }
    //void OnTriggerEnter2D(Collider2D other)
    //{

    //    if (other.tag=="Pleyer")
    //    {
    //        print("1");
    //        Destroy(gameObject);
    //    }
    //}

}

代码没啥优化只是勉强实现功能,还有没实现的
遇到好多坑,留这以后看一下自己多蠢

 类似资料: