[开发者心得] 用Tween模拟运动器

[复制链接]
996 |2
十二月雪景 发表于 2023-6-11 01:11:53 | 显示全部楼层 |阅读模式
本帖最后由 十二月雪景 于 2023-6-11 01:11 编辑


//方块位置
    private _cubePos:Type.Vector;
    //右侧点
    private _rightPos:Type.Vector;
    //左侧点
    private _leftPos:Type.Vector;
    //方块
    private _cube:Core.GameObject;
    /** 当脚本被实例后,会在第一帧更新前调用此函数 */
    protected async onStart(): Promise<void> {
        this.useUpdate=true;
        if(SystemUtil.isClient()){
                        
         this._cube=await Core.GameObject.asyncFind('1AFBA7B9');
         this._cubePos=this._cube.worldLocation;
         this._rightPos=(await Core.GameObject.asyncFind('292F1F5E')).worldLocation;
         this._leftPos=(await Core.GameObject.asyncFind('15C2DFCD')).worldLocation;

         const letfY= this._leftPos.y;
         const orignalY= this._cubePos.y;
         const rightY= this._rightPos.y;

         const rightToOriginalTween=new TweenUtil.Tween({y:rightY})
         .to({y:orignalY},3000)
         .onUpdate((object)=>{
      
            this._cube.worldLocation=new Type.Vector(this._cube.worldLocation.x,object.y,this._cube.worldLocation.z);
          });
         
          const leftToOriginalTween=new TweenUtil.Tween({y:letfY})
         .to({y:orignalY},3000)
         .onUpdate((object)=>{
         
            this._cube.worldLocation=new Type.Vector(this._cube.worldLocation.x,object.y,this._cube.worldLocation.z);
          });

        const toLeftTween=new TweenUtil.Tween({y:orignalY})
        .to({y:letfY},3000)
        .onUpdate((object)=>{
   
            this._cube.worldLocation=new Type.Vector(this._cube.worldLocation.x,object.y,this._cube.worldLocation.z);
        });

         const toRightTween=new TweenUtil.Tween({y:orignalY})
         .to({y:rightY},3000)
         .onUpdate((object)=>{      
        
            this._cube.worldLocation=new Type.Vector(this._cube.worldLocation.x,object.y,this._cube.worldLocation.z);
          });
          //一次方块来回动画耗时(ms)
          const takeTime=3000*4;
   
          toLeftTween.chain(leftToOriginalTween.chain(toRightTween.chain(rightToOriginalTween))).start();
          setInterval(()=>{
            toLeftTween.chain(leftToOriginalTween.chain(toRightTween.chain(rightToOriginalTween))).start();
          },takeTime)
        }
    }









利用tween动画实现运动器的效果,注意这里的chain()函数使用,不能直接一直chain动画,而需要嵌套chain动画。

simulatedIntegratedMover.rar

48.59 KB, 下载次数: 47

回复

使用道具 举报

空伊伊 发表于 2023-6-11 09:42:26 | 显示全部楼层
👍👍👍👍👍👍👍
回复

使用道具 举报

kk 发表于 2023-6-11 11:32:48 | 显示全部楼层
6
回复

使用道具 举报

热门版块
快速回复 返回顶部 返回列表