Dotween测试

发布时间:2023年12月18日

Dopath移到某点

  //二维移到某点 曲线1/折线2
    public static  Tween MovePathTo(this Transform trans,Vector2 posEnd,float timeAll=0.8f,int type=1)
    {
        Vector2 posBegin = trans.position;
         List<Vector3> posAll = new List<Vector3>();
        posAll.Add(posBegin);
        var dis = Vector3.Distance(posBegin, posEnd);
        var lerp = Vector3.Lerp(posBegin, posEnd, Random.Range(0.3f, 0.6f));
        posAll.Add(  lerp +      1.RandomCircleDir()*dis*Random.Range(0.1f,0.3f)   );
        posAll.Add(posEnd);
        return   trans.DOPath(posAll.ToArray(), timeAll, type==1? PathType.CatmullRom:PathType.Linear).SetEase(Ease.Linear).OnComplete(delegate {
        });
    }

调用演示

  void move1() {
        transform.position = A.position;
        Tween c = transform.MovePathTo(B.position);
        c.OnComplete(delegate {

            Move2();
        });
    }
    void Move2() {
    transform.MovePathTo(B.position + new Vector3(800, 800)).OnComplete(
      delegate {
           move1();
         });
   }

Around:
   //二维绕旋转
    public static Tween RotateAroundWithAngle(this Transform trans, Vector2 posCenter, float angle=90f,  float timeAll = 0.8f) {
       return  trans.DOScaleZ(1,timeAll).SetEase(Ease.Linear).OnUpdate(
            delegate {
                float speed = angle / (timeAll+0.01f);
                trans.RotateAround(posCenter,Vector3.forward,speed*Time.deltaTime);
               // trans.RotateAroundLocal(Vector3.forward, speed * Time.deltaTime);//self local round
            }
            );
    }
    {
        //type:create point move then around it
        //type:create points,create from points then around it.
        //type:double around ,a around b, b around a
        //type:create point then move half circle , then around new point toward target dir.or only change points's position 
        //type:around with one point , change radius everytimes.left->right big around 180 then right->left
        //type:cal radius with selfpos and targetpos ,then become 1/4circle(or 1/2 circle),move 1/2 
        //type:create b then move toward then around with one verticals dir's point 6 circles,then bo.speed slower
        //type :ajust dir, create b then move around,change pos then loop this action
        transform.RotateAroundWithAngle(B.position,90f);
    }
three different height line with rotate speeed
test

Follow?random?and loop;

   public static void CreateAndFollowLoopTypeLine(this Transform b, Vector3 createPos, Transform target, int loop = 3, float timeMove = 0.3f, float moveDisOnce = 5f, float angleDirRandom = 35f, int pathType = 1)
    {

        b.CreateAndFollowLoop( createPos,  target, loop ,  timeMove,  moveDisOnce,  angleDirRandom,, 1);
    }

     public static void  CreateAndFollowLoop(this Transform b,Vector3 createPos, Transform target, int loop =3, float timeMove = 0.3f ,float moveDisOnce=5f, float angleDirRandom = 35f, int pathType = 1) 
    {
        Transform trans=   GameObject.Instantiate(b);

        trans.name = "ccc";
        trans.position = createPos;
        float originScale = 0.3f;
        trans.localScale = Vector3.one * originScale;
        Vector2 dir = default;
        Tween t = null;
        int index = 0;
        void move(int number)
        {
            Vector2 dir0 = (target.position - trans.position);
            var dis = dir0.magnitude;
            var moveDisOnce0 = moveDisOnce>dis?dis:moveDisOnce;
            dir = dir0.ToVectorChangeAngle(Random.Range(-angleDirRandom, angleDirRandom)).normalized;
            trans.DOScale(0.5f, timeMove*0.4f).SetEase(Ease.Linear).OnComplete(
                delegate {
                    trans.DOScale(1f, timeMove * 0.3f).SetEase(Ease.Linear);
                }
                );
            t = trans.MovePathTo(trans.position + (Vector3)dir * moveDisOnce0, timeMove, pathType).OnComplete(
                    delegate
                    {
                        --number;
                        if (number > 0)
                        {
                            move(number);
                        }
                        else
                        {
                            trans.DOScale(0, 0.1f).SetEase(Ease.Linear).OnComplete(
                                delegate{
                                    GameObject.Destroy(trans.gameObject);
                                });
                        }
                    });
        }
        move(loop);

    }


 cube2.CreateAndFollowLoop(transform.position,B,30,0.3f,5,35f);
        cube1.CreateAndFollowLoop(transform.position, B, 30, 0.5f,3f,15f);

follow cubedir

    public static void CreateAndCubeMoveLoop(this Transform b, Vector3 createPos, Transform target, int loop = 3, float timeMove = 0.3f, float moveDisOnce = 5f)
    {
        Transform trans = GameObject.Instantiate(b);

        trans.name = "ccc";
        trans.position = createPos;
        float originScale = 0.3f;
        trans.localScale = Vector3.one * originScale;
        Vector2 dir = default;
        Tween t = null;
        int index = 0;
        void move(int number)
        {
            Vector2 dir0 = (target.position - trans.position);
            var dis = dir0.magnitude;
            var moveDisOnce0 = moveDisOnce > dis ? dis : moveDisOnce;
            float unit = 90f;
            dir = ((Mathf.Ceil(dir0.ToAngleClock() / unit)) * unit).ToDirTypeClock();//cubedir
            trans.DOScale(0.5f, timeMove * 0.4f).SetEase(Ease.Linear).OnComplete(
                delegate {
                    trans.DOScale(1f, timeMove * 0.3f).SetEase(Ease.Linear);
                }
                );
            t = trans.MovePathTo(trans.position + (Vector3)dir * moveDisOnce0, timeMove).OnComplete(
                    delegate
                    {
                        --number;
                        if (number > 0)
                        {
                            move(number);
                        }
                        else
                        {
                            trans.DOScale(0, Mathf.Max(timeMove * 0.3f, 0.1f)).SetEase(Ease.Linear).OnComplete(
                                delegate {
                                    GameObject.Destroy(trans.gameObject);
                                });
                        }
                    });
        }
        move(loop);
    }

  cube1.CreateAndCubeMoveLoop(transform.position, B, 30, 0.3f, 2f);

physics -精准点位物理抛物轨迹

    //精准物理移动
    //参照https://zhuanlan.zhihu.com/p/472797723
    public static void CreateAndMovephysics(this Transform b, Vector3 createPos, Vector3 targetPos, float angle=35f, float force= 35f,float gravityRatio=2f,float maxTime=2f)
    {
        Transform trans = GameObject.Instantiate(b);
        trans.position = createPos;
        Vector2 dir0 = (targetPos - trans.position);
        var dir = dir0.normalized;
       float speedXY= (dir* force).magnitude;

        float t = 0f;
        float theta = Mathf.Deg2Rad *45f;
        float gravitY = Physics2D.gravity.y * gravityRatio;
     trans.DOScaleZ(1, maxTime).OnUpdate(
        delegate
        {
            t += Time.deltaTime;

                float x = speedXY * t * Mathf.Cos(theta) + createPos.x;
                float y = speedXY * t * Mathf.Sin(theta) + 0.5f * gravitY * t * t + createPos.y;
            trans.position = new Vector3(x,y);
            }

            );
    }

?调用

        cube1.CreateAndMovephysics(transform.position, B.position, 50, 20,8);
        cube1.CreateAndMovephysics(transform.position,B.position,40,25,8);
        cube1.CreateAndMovephysics(transform.position, B.position, 30, 30,8);

抛物线dotween模拟

    //dotween模拟抛物线
    public static void CreateAndMoveCurveTween(this Transform b, Vector3 createPos, Vector3 targetPos, float time, float JumpHeight=10f)
    {
        Transform trans = GameObject.Instantiate(b);
        trans.position = createPos;
        Vector2 dir0 = (targetPos - trans.position);
        Sequence s = DOTween.Sequence();
        s.Append(trans.DOMoveX(targetPos.x , time).SetEase(Ease.Linear));
        //上升
        var targetY0 = targetPos.y < createPos.y ? createPos.y: targetPos.y;
        s.Insert(0, trans.DOMoveY(targetY0 + JumpHeight, time/2).SetEase(Ease.OutCirc));
        //下落
        var targetY = createPos.y < targetPos.y ? createPos.y : targetPos.y;
        s.Insert(time / 2, trans.DOMoveY(targetY, time / 2).SetEase(Ease.InQuad));//Ease.InQuad Ease.InCirc末尾太快
        s.Insert(time, trans.DOScale(0f, 0.2f)).SetEase(Ease.Linear);
        s.OnComplete(delegate {
            GameObject.Destroy(trans.gameObject);
        });
        // Ease.InCirc变化太大 //Ease.InSine不太自然
        //结束bo
     
    }

调用

  cube1.CreateAndMoveCurveTween(transform.position, B.position+1.RandomCircleDir()*2f+(Vector3)135f.ToDirTypeClock()*5f,  1f, 10f);
        

Projs


    //proj
    //line
    public delegate void callbackCreateProjectileTypeNormal(Transform trans);
    public static void  CreateProjectileTypeNormal(this Transform b,Vector3 createPos, Vector3 targetPos, float time,float moveSpeed=50f, callbackCreateProjectileTypeNormal call=null) {
        float moveDis = moveSpeed * time;
        Transform trans = GameObject.Instantiate(b);
        trans.position = createPos;
        Vector2 dir0 = (targetPos - trans.position);
        var dir = dir0.normalized;
        Sequence s = DOTween.Sequence();
        trans.SetDirectionUp(dir);

        Tween move = trans.DOMove(createPos + (Vector3)dir * moveDis, time).SetEase(Ease.Linear);
        move.OnComplete(delegate { 
        //close collider
        });
        s.Append(move);
        s.Insert(time, trans.DOScale(0f, 0.2f)).SetEase(Ease.Linear);
        s.OnComplete(delegate {
            call(trans);
            GameObject.Destroy(trans.gameObject);
        });
    }
    public static void CreateProjectileTypeDir(this Transform b, Vector3 createPos, Vector3 dir, float time, float moveSpeed = 50f, callbackCreateProjectileTypeNormal call = null)
    {
        var targetPos = createPos + dir *moveSpeed*time;//有点多此一举,无所谓
        b.CreateProjectileTypeNormal(createPos, targetPos, time, moveSpeed, call);
    }




    //follow
    public delegate void callbackCreateProjectileTypeFollow1(Transform trans);
    public static void CreateProjectileTypeFollow1(this Transform b, Vector3 createPos, Transform target ,float time, float moveSpeed=30f,float rotateSpeed=90f, callbackCreateProjectileTypeFollow1 call= null,Vector3 originDir= default)
    {
        if (target == null) return;
        Transform trans = GameObject.Instantiate(b);
        trans.position = createPos;
        Vector2 dirTarget0 = (target.position - trans.position);
        var dirTarget = dirTarget0.normalized;
        if (originDir != default) {
            dirTarget = originDir;
            }
        Sequence s = DOTween.Sequence();
        trans.SetDirectionUp(dirTarget);
        float angleNow = dirTarget.ToAngleClock();
        Tween move = trans.DOScaleZ(1, time).SetEase(Ease.Linear);

        move.OnUpdate(delegate {
            dirTarget0 = (target.position - trans.position);
            dirTarget = dirTarget0.normalized;
            angleNow = trans.up.ToAngleClock(); ;
            float angleDis = (dirTarget.ToAngleClock() - angleNow);
            if (angleDis < 0) angleDis += 360f;
            var moveDir = trans.up.ToVectorChangeAngle((angleDis <180f?1:-1) *rotateSpeed * Time.deltaTime);
            trans.SetDirectionUp(moveDir);
            trans.position += (Vector3)moveDir * moveSpeed * Time.deltaTime;
        });
        move.OnComplete(delegate {
            //close collider
        });
        s.Append(move);
        s.Insert(time, trans.DOScale(0f, 0.2f)).SetEase(Ease.Linear);
        s.OnComplete(delegate {
            call(trans);
            GameObject.Destroy(trans.gameObject);
        });
    }

调用

    //projs1   不如直接方法里调用
    public static void CreateProjsType1(this Transform b,Transform secondProj,  Vector3 createPos, Transform target, float time, float timeSecond, float moveSpeed = 50f, float moveSecondSpeed = 50f,float rotateSecondSpeed=90)
    {
      void  call1(Transform trans){
            secondProj.CreateProjectileTypeFollow1(trans.position, target, timeSecond, moveSecondSpeed, rotateSecondSpeed);
        }
        b.CreateProjectileTypeNormal( createPos, target.position, time, moveSpeed,call1);
   
    }

    //cube1.CreateProjectileTypeNormal(transform.position, B.position, 0.5f,30f, delegate(Transform trans) {
    //      //can create one fx
    //        cube1.CreateProjectileTypeFollow1(trans.position, B, 3f, 30f, 180f);
    //    });

sth changed :

datamanager 计算夹角修改api

文章来源:https://blog.csdn.net/qq_22012149/article/details/131531198
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。