C#實現貝塞爾曲線的方法

本文實例為大傢分享瞭C#實現貝塞爾曲線的具體代碼,供大傢參考,具體內容如下

話不多直接上代碼

public Transform[] controlPoints; //曲線的控制點 ,最少三個,起點,弧度點,終點
    public GameObject codeGameObject; //要動的物體

    private int _segmentNum = 50; //運動物體過程分的段數
    private int numIndex = 1;
    
void Start()
    {
        moveGameObject(2);
    }

void moveGameObject(float time)
    {
        numIndex = 1;
        InvokeRepeating("setInterval", 0, time/50);
    }

void setInterval()
    {
        int nodeIndex = 0;
        float t = numIndex / (float)_segmentNum;
        Vector3 pixel = CalculateCubicBezierPoint(t, controlPoints[nodeIndex].position,
            controlPoints[nodeIndex + 1].position, controlPoints[nodeIndex + 2].position);
        codeGameObject.gameObject.transform.position= pixel;
        numIndex++;

        if(numIndex> _segmentNum)
        {
            CancelInvoke("setInterval");
        }
    }
Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
    {
        float u = 1 - t;
        float tt = t * t;
        float uu = u * u;

        Vector3 p = uu * p0;
        p += 2 * u * t * p1;
        p += tt * p2;

        return p;
    }

項目裡的截圖:

運行就可以看到球在三個方塊之間移動的曲線瞭

第二種方案使用DOTweenPath

/**
         * @brief   播放道具飛行動畫
         *
         * @param   prop    道具
         * @param   sendPos 起始點
         * @param   endP    終點
         * @param   fHeight 貝塞爾曲線中間點的高度(控制曲線)
         *
         * @return  飛行動畫時間
         */
    public float PlaySendFlyAnim(GameObject prop, Vector3 sendPos, Vector3 endP, float fHeight, float time)
    {
        float fTime = 0.0f;
        if (prop == null)
        {
            return fTime;
        }
        
        Vector3 startP = sendPos;
        prop.transform.position = startP;

        float x = Mathf.Min(startP.x, endP.x) + Mathf.Abs(startP.x - endP.x) / 2f;
        float y = Mathf.Min(startP.y, endP.y) + Mathf.Abs(startP.y - endP.y) / 2f;
        float z = startP.z;
        Vector3 midP = new Vector3(x, y, z);
        double length = Math.Sqrt(Math.Pow(sendPos.x - midP.x, 2) + Math.Pow(sendPos.y - midP.y, 2));
        //midP.x = fHeight / (float)length * (sendPos.x - midP.x) + midP.x;
        //midP.y = fHeight / (float)length * (sendPos.y - midP.y) + midP.y;
        int rangeRadomNum = UnityEngine.Random.Range(0, 2);
        if(rangeRadomNum == 1)
        {
            midP.x = fHeight / (float)length * (endP.x - midP.x) + midP.x;
            midP.y = endP.y;
        }
        else
        {
            midP.y = fHeight / (float)length * (endP.y - midP.y) + midP.y;
            midP.x = endP.x;
        }
        //fTime = 2.0f;
        fTime = time;
        List<Vector3> arrRecPos = new List<Vector3>();

        arrRecPos.Add(startP);
        arrRecPos.Add(midP);
        arrRecPos.Add(endP);
        prop.transform.DOPath(arrRecPos.ToArray(), fTime, PathType.CatmullRom, PathMode.Full3D).SetEase(Ease.Linear);

        return fTime;
    }
/// <param name="sendPos"> 玩傢頭像位置</param>
    /// <param name="endP">篩子停止位置</param>
    /// <param name="i">篩子大小</param>
    /// <param name="radian">弧度</param>
    /// <param name="time">時間</param>
    /// <returns></returns>
    public float PlayAddFriendAnim(Vector3 sendPos, Vector3 endP,int i,int radian = 0, float time = 1.5f)
    {
        GameObject shaizi = this.transform.Find("shaizi_anmi4_0").gameObject;
        shaizi.GetComponent<Animator>().enabled = true;

        SpriteRenderer _shaizhiV = shaizi.GetComponent<SpriteRenderer>();
        float fTime = PlaySendFlyAnim(shaizi, sendPos, endP, radian, time);

        DOTween.Sequence().AppendInterval(fTime).AppendCallback(() =>
        {
            if (shaizi != null)
            {
                shaizi.GetComponent<Animator>().enabled = false;
                _shaizhiV.sprite = shaiziData[i-1];
                StartCoroutine(Destroyshaizi(shaizi));
            }
        });

        return fTime;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: