Unity之Dotween實現(xiàn)多個物體從開始位置按照弧形軌跡移動到結(jié)束位置

using DG.Tweening;

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class UI_ZhuZhan_Effect : MonoBehaviour {

? ? public GameObject[] m_EffectList;

? ? [Header("貝塞爾曲線點的數(shù)量")]

? ? public int m_VecPointNum = 3;

? ? [Header("貝塞爾曲線坡峰")]

? ? public int m_SlopePeak = 50;

? ? //[Header("到終點的偏移距離叉存,與斜率相乘得到最終位置點")]

? ? //public float m_OffectValue = 130;

? ? //[Header("正弦函數(shù)坡長")]

? ? //public float m_SlopeLength = 3;

? ? //[Header("正弦函數(shù)坡峰數(shù)組")]

? ? //public float[] m_SlopePeak;

? ? public void Init(Vector3 startPoint, Vector3 endPoint, Action action)

? ? {

? ? ? ? float distance = Vector3.Distance(startPoint, endPoint) / 180;

? ? ? ? //m_SlopeLength = 50;

? ? ? ? //float SlopeValue = Mathf.Atan2((endPoint.x - startPoint.x), (endPoint.z - startPoint.z));

? ? ? ? //float angle = SlopeValue * (180 / Mathf.PI);

? ? ? ? //angle = 1 - Mathf.Abs(angle) / 180;

? ? ? ? //m_OffectValue = angle * 200;

? ? ? ? //Debug.Log("斜率:" + angle);

? ? ? ? Vector3 centerPos = new Vector3((startPoint.x + endPoint.x) / 2, endPoint.y, (startPoint.z + endPoint.z) / 2);

? ? ? ? float SlopeValue1 = (endPoint.x - startPoint.x) / (endPoint.z - startPoint.z);

? ? ? ? float SlopeValue2 = (-1)/SlopeValue1;

? ? ? ? float b = centerPos.x - centerPos.z * SlopeValue2;

? ? ? ? Sequence sequence = DOTween.Sequence();

? ? ? ? int index = 0;

? ? ? ? for (int i = 0; i < m_EffectList.Length; i++)

? ? ? ? {

? ? ? ? ? ? m_EffectList[i].transform.position = startPoint;

? ? ? ? ? ? if (index == 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? centerPos = new Vector3(SlopeValue2*((startPoint.z + endPoint.z) / 2 + (m_SlopePeak + distance) / SlopeValue2) + b, endPoint.y, (startPoint.z + endPoint.z) / 2 + (m_SlopePeak + distance) / SlopeValue2);

? ? ? ? ? ? }

? ? ? ? ? ? else if(index == 1)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? centerPos = new Vector3(SlopeValue2 * ((startPoint.z + endPoint.z) / 2 - (m_SlopePeak + distance) / SlopeValue2) + b, endPoint.y, (startPoint.z + endPoint.z) / 2 - (m_SlopePeak + distance) / SlopeValue2);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? centerPos = new Vector3((startPoint.x + endPoint.x) / 2, endPoint.y, (startPoint.z + endPoint.z) / 2);

? ? ? ? ? ? ? ? index = 0;

? ? ? ? ? ? }

? ? ? ? ? ? index++;

? ? ? ? ? ? Vector3[] path = GetBeizerList(startPoint, centerPos, endPoint, m_VecPointNum);//m_SlopePeak[i], 0, m_OffectValue * (SlopeValue <= 0 ? 1 : -1)

? ? ? ? ? ? if (i==0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? sequence.Append(m_EffectList[i].transform.DOLocalPath(path, 1.5f, PathType.CatmullRom));

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? sequence.Insert(0,m_EffectList[i].transform.DOLocalPath(path, 1.5f, PathType.CatmullRom));

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? sequence.AppendCallback(delegate ()

? ? ? ? {

? ? ? ? ? ? action.InvokeIfNotNull();

? ? ? ? });

? ? }

? ? /// <summary>

? ? /// 獲取存儲貝塞爾曲線點的數(shù)組

? ? /// </summary>

? ? /// <param name="startPoint"></param>起始點

? ? /// <param name="controlPoint"></param>控制點

? ? /// <param name="endPoint"></param>目標(biāo)點

? ? /// <param name="segmentNum"></param>采樣點的數(shù)量

? ? /// <returns></returns>存儲貝塞爾曲線點的數(shù)組

? ? public Vector3[] GetBeizerList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int segmentNum)//, float slopePeak, float offectValue

? ? {

? ? ? ? Vector3[] path = new Vector3[segmentNum+1];

? ? ? ? path[0] = startPoint;

? ? ? ? for (int i = 1; i <= segmentNum; i++)

? ? ? ? {

? ? ? ? ? ? float t = i / (float)segmentNum;

? ? ? ? ? ? Vector3 pixel = CalculateCubicBezierPoint(t, startPoint,

? ? ? ? ? ? ? ? controlPoint, endPoint);

? ? ? ? ? ? //if (slopePeak!=0)

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? float innner = (2 * (float)Mathf.PI) / m_SlopeLength;

? ? ? ? ? ? //? ? pixel.x = (float)Mathf.Sin(innner * pixel.z) * slopePeak - offectValue;

? ? ? ? ? ? //}

? ? ? ? ? ? path[i] = pixel;

? ? ? ? }

? ? ? ? path[segmentNum] = endPoint;

? ? ? ? return path;

? ? }

? ? /// <summary>

? ? /// 根據(jù)T值,計算貝塞爾曲線上面相對應(yīng)的點

? ? /// </summary>

? ? /// <param name="t"></param>T值

? ? /// <param name="p0"></param>起始點

? ? /// <param name="p1"></param>控制點

? ? /// <param name="p2"></param>目標(biāo)點

? ? /// <returns></returns>根據(jù)T值計算出來的貝賽爾曲線點

? ? private 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;

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枚荣,一起剝皮案震驚了整個濱河市拌屏,隨后出現(xiàn)的幾起案子澄成,更是在濱河造成了極大的恐慌,老刑警劉巖肌厨,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件内斯,死亡現(xiàn)場離奇詭異杯瞻,居然都是意外死亡,警方通過查閱死者的電腦和手機赡若,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門盒音,熙熙樓的掌柜王于貴愁眉苦臉地迎上來表鳍,“玉大人馅而,你說我怎么就攤上這事∑┦ィ” “怎么了瓮恭?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長厘熟。 經(jīng)常有香客問我屯蹦,道長,這世上最難降的妖魔是什么绳姨? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任登澜,我火速辦了婚禮,結(jié)果婚禮上飘庄,老公的妹妹穿的比我還像新娘脑蠕。我一直安慰自己,他們只是感情好跪削,可當(dāng)我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布谴仙。 她就那樣靜靜地躺著,像睡著了一般切揭。 火紅的嫁衣襯著肌膚如雪狞甚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天廓旬,我揣著相機與錄音哼审,去河邊找鬼。 笑死孕豹,一個胖子當(dāng)著我的面吹牛涩盾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播励背,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼春霍,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了叶眉?” 一聲冷哼從身側(cè)響起址儒,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎衅疙,沒想到半個月后莲趣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡饱溢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年喧伞,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡潘鲫,死狀恐怖翁逞,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情溉仑,我是刑警寧澤挖函,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站彼念,受9級特大地震影響挪圾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜逐沙,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一哲思、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吩案,春花似錦棚赔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至残揉,卻和暖如春胧后,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抱环。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工壳快, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人镇草。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓眶痰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親梯啤。 傳聞我的和親對象是個殘疾皇子竖伯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容