C#實(shí)現(xiàn)貝塞爾曲線的方法
本文實(shí)例為大家分享了C#實(shí)現(xiàn)貝塞爾曲線的具體代碼,供大家參考,具體內(nèi)容如下
話不多直接上代碼
public Transform[] controlPoints; //曲線的控制點(diǎn) ,最少三個(gè),起點(diǎn),弧度點(diǎn),終點(diǎn)
? ? public GameObject codeGameObject; //要?jiǎng)拥奈矬w
? ? private int _segmentNum = 50; //運(yùn)動(dòng)物體過程分的段數(shù)
? ? 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;
? ? }項(xiàng)目里的截圖:

運(yùn)行就可以看到球在三個(gè)方塊之間移動(dòng)的曲線了
第二種方案使用DOTweenPath
/**
? ? ? ? ?* @brief ? 播放道具飛行動(dòng)畫
? ? ? ? ?*
? ? ? ? ?* @param ? prop ? ?道具
? ? ? ? ?* @param ? sendPos 起始點(diǎn)
? ? ? ? ?* @param ? endP ? ?終點(diǎn)
? ? ? ? ?* @param ? fHeight 貝塞爾曲線中間點(diǎn)的高度(控制曲線)
? ? ? ? ?*
? ? ? ? ?* @return ?飛行動(dòng)畫時(shí)間
? ? ? ? ?*/
? ? 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">時(shí)間</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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)高性能文件批量處理器的示例代碼
文件批量處理器可以用于數(shù)字資產(chǎn)管理,數(shù)據(jù)遷移工程,日志文件處理和安全審計(jì)場(chǎng)景,本文將使用C#開發(fā)一個(gè)高性能文件批量處理器,希望對(duì)大家有所幫助2025-03-03
C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié)
這篇文章主要介紹了C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
C#基于簡(jiǎn)單工廠模式實(shí)現(xiàn)的計(jì)算器功能示例
這篇文章主要介紹了C#基于簡(jiǎn)單工廠模式實(shí)現(xiàn)的計(jì)算器功能,結(jié)合簡(jiǎn)單實(shí)例形式分析了C#使用工廠模式的數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
c#基于WinForm的Socket實(shí)現(xiàn)簡(jiǎn)單的聊天室 IM
這篇文章主要介紹了c#基于WinForm的Socket實(shí)現(xiàn)簡(jiǎn)單的聊天室 IM的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-05-05
c# HttpWebRequest通過代理服務(wù)器抓取網(wǎng)頁(yè)內(nèi)容應(yīng)用介紹
在C#項(xiàng)目開發(fā)過程中可能會(huì)有些特殊的需求比如:用HttpWebRequest通過代理服務(wù)器驗(yàn)證后抓取網(wǎng)頁(yè)內(nèi)容,要想實(shí)現(xiàn)此方法并不容易,本文整理了一下,有需求的朋友可以參考下2012-11-11
C#連接ClickHouse數(shù)據(jù)庫(kù)的步驟指南
在 C# 中連接 ClickHouse 數(shù)據(jù)庫(kù),您可以使用 ClickHouse.Client 庫(kù),這個(gè)庫(kù)提供了對(duì) ClickHouse 數(shù)據(jù)庫(kù)的高效訪問,以下是詳細(xì)的步驟指南,幫助您在 C# 項(xiàng)目中連接和操作 ClickHouse 數(shù)據(jù)庫(kù),需要的朋友可以參考下2024-12-12

