甜品消消乐 10 移动填充的携程

本文介绍了一种在Unity游戏引擎中利用C#脚本和协程实现甜品移动动画的方法。通过自定义的MovedSweet类,实现了甜品在游戏世界中的平滑移动,使用Vector3.Lerp函数来创建缓动效果,确保了移动过程的流畅性和视觉吸引力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

13需要一个甜品自身的移动协程,需要他自己的一个移动动画。
用协程让甜品位置有一个缓动效果

public class MovedSweet : MonoBehaviour {

    private GameSweet sweet;
    //这样得到其他指令的时候我们可以终止这个协程
    private IEnumerator moveCoroutine;

    private void Awake()
    {
        sweet = GetComponent<GameSweet>();
    }

    //开启或者结束一个协程
    public void Move(int newX,int newY,float time)
    {
        //防止两个协程同时运行
        if (moveCoroutine!=null)
        {
            StopCoroutine(moveCoroutine);
        }

        moveCoroutine = MoveCoroutine(newX, newY, time);
        StartCoroutine(moveCoroutine);
    }

    //负责移动的协同程序
    private IEnumerator MoveCoroutine(int newX,int newY,float time)
    {
        sweet.X = newX;
        sweet.Y = newY;

        //每一帧移动一点点
        Vector3 startPos = transform.position;
        Vector3 endPos = sweet.gameManager.CorrectPositon(newX, newY);

        for (float t = 0; t < time; t+=Time.deltaTime)
        {
            sweet.transform.position = Vector3.Lerp(startPos, endPos, t / time);
            yield return 0;
        }

        sweet.transform.position = endPos;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值