Unity使用DoTween實現(xiàn)拋物線效果_第1頁
Unity使用DoTween實現(xiàn)拋物線效果_第2頁
Unity使用DoTween實現(xiàn)拋物線效果_第3頁
Unity使用DoTween實現(xiàn)拋物線效果_第4頁
Unity使用DoTween實現(xiàn)拋物線效果_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第Unity使用DoTween實現(xiàn)拋物線效果Debug.Log(value);

ball.position=Parabola(start.position,target.position,10,value);

},startValue:0,endValue:1,duration:5)

.SetEase(Ease.Linear);

}

效果演示

之前小編收藏了一段拋物線代碼,分享給大家:unity實現(xiàn)炮彈運動軌跡(拋物線)

usingSystem.Collections;

usingSystem.Collections.Generic;

usingUnityEngine;

publicclassParabol:MonoBehaviour

privateRigidbodyrgb;

///summary

///目標

////summary

publicGameObjecttarget;

///summary

///子彈的發(fā)射點

////summary

privateVector3originPoint;

privateVector3aimPoint;

///summary

///無彈道偏移的當前位置

////summary

privateVector3myVirtualPosition;

///summary

///定位最后一幀

////summary

privateVector3myPreviousPosition;

///summary

///是否可以發(fā)射

////summary

privateboolsw=false;

privateboolactived=false;

///summary

///最大發(fā)射距離

////summary

publicfloatmaxLaunch=1f;

///summary

///加速度計算計數(shù)器

////summary

privatefloatcounter;

///summary

///剛剛啟動時的速度

////summary

publicfloatspeed=0.5f;

///summary

///恒定加速度

////summary

publicfloatspeedUpOverTime=0.1f;

///summary

///彈道偏移量(與目標的距離)

////summary

publicfloatballisticOffset=0.5f;

voidStart()

rgb=GetComponentRigidbody

sw=true;

if(target==null)

Destroy(gameObject);

else

aimPoint=target.transform.position;

originPoint=myVirtualPosition=myPreviousPosition=transform.position;

voidUpdate()

if(target!=null)

if(actived==false)

actived=true;

PreLaunch();

else

if(sw==true)

if(rgb.isKinematic==false)

Move();

privatevoidPreLaunch()

floatxTarget=target.transform.position.x;

floatyTarget=target.transform.position.y;

floatzTarget=target.transform.position.z;

floatxCurrent=transform.position.x;

floatyCurrent=transform.position.y;

floatzCurrent=transform.position.z;

//目標之間的值

floatxDistance=Mathf.Abs(xTarget-xCurrent);

floatyDistance=yTarget-yCurrent;

floatzDistance=Mathf.Abs(zTarget-zCurrent);

floatfireAngle=1.57075f-(Mathf.Atan((Mathf.Pow(maxLaunch,2f)+Mathf.Sqrt(Mathf.Pow(maxLaunch,4f)-9.8f*(9.8f*Mathf.Pow(xDistance,2f)+2f*yDistance*Mathf.Pow(maxLaunch,2f)+2f*zDistance*Mathf.Pow(maxLaunch,2f))))/(9.8f*xDistance)));

floatxSpeed=Mathf.Sin(fireAngle)*maxLaunch;

floatySpeed=Mathf.Cos(fireAngle)*maxLaunch;

floatzSpeed=Mathf.Tan(fireAngle)*maxLaunch;

//判斷在左邊還是右邊

if((xTarget-xCurrent)0f){xSpeed=-xSpeed;}

if((zTarget-zCurrent)0f){zSpeed=-zSpeed;}

Calculation(ySpeed);

sw=true;

privatevoidCalculation(floatspeedy)

NextPosition(Time.time%((speedy/9.81f)*2));

privatevoidNextPosition(floatairtime)

floatxTarget=target.transform.position.x;

floatyTarget=target.transform.position.y;

floatzTarget=target.transform.position.z;

floatspeedy=target.GetComponentRigidbody().velocity.y;

floatspeedx=target.GetComponentRigidbody().velocity.x;

floatspeedz=target.GetComponentRigidbody().velocity.z;

Launch(xTarget+(speedx*airtime),yTarget+(speedy*airtime),zTarget+(speedz*airtime));

privatevoidLaunch(floatxTarget,floatyTarget,floatzTarget)

rgb.isKinematic=false;

floatxCurrent=transform.position.x;

floatyCurrent=transform.position.y;

floatzCurrent=transform.position.z;

floatxDistance=Mathf.Abs(xTarget-xCurrent);

floatyDistance=yTarget-yCurrent;

floatzDistance=Mathf.Abs(zTarget-zCurrent);

floatfireAngle=1.57075f-(Mathf.Atan((Mathf.Pow(maxLaunch,2f)+Mathf.Sqrt(Mathf.Pow(maxLaunch,4f)-9.8f*(9.8f*Mathf.Pow(xDistance,2f)+2f*yDistance*Mathf.Pow(maxLaunch,2f)+2f*zDistance*Mathf.Pow(maxLaunch,2f))))/(9.8f*xDistance)));

floatxSpeed=Mathf.Sin(fireAngle)*maxLaunch;

floatySpeed=Mathf.Cos(fireAngle)*maxLaunch;

floatzSpeed=Mathf.Tan(fireAngle)*maxLaunch;

//判斷在左邊還是右邊

if((xTarget-xCurrent)0f){xSpeed=-xSpeed;}

if(!float.IsNaN(xSpeed)!float.IsNaN(ySpeed))

rgb.velocity=newVector3(xSpeed,ySpeed,zSpeed);

else

maxLaunch=maxLaunch+0.3f;

PreLaunch();

privatevoidMove()

counter+=Time.fixedDeltaTime;

//加速度提升

speed+=Time.fixedDeltaTime*speedUpOverTime;

if(target!=null)

aimPoint=target.transform.position;

//計算從發(fā)射點到目標的距離

Vector3originDistance=aimPoint-originPoint;

//計算剩余距離

Vector3distanceToAim=aimPoint-myVirtualPosition;//發(fā)射點和目標之間的矢量距離

//移動到目標

myVirtualPosition=Vector3.Lerp(originPoint,aimPoint,counter*speed/originDistance.magnitude);//vectornisuygiavtríbanuvàmctiêu

//向軌跡添加彈道偏移

transform.position=AddBallisticOffset(originDistance.magnitude,distanceToAim.magnitude);

//將子彈旋轉(zhuǎn)至彈道

//Debug.Log("最后一幀的位置:"+myPreviousPosition);

LookAtDirection(transform.position-myPreviousPosition);

myPreviousPosition=transform.position;

privateVector3AddBallisticOffset(floatoriginDistance,floatdistanceToAim)

if(ballisticOffset0f)

//計算彎曲處偏移

floatoffset=Mathf.Sin(Mathf.PI*((originDistance-distanceToAim)/originDistance));

offset*=originDistance;

//向軌跡添加偏移

returnmyVirtualPosition+(ballisticOffset*offset*Vector3.up);

else

returnmyVirtualPosition;

///summary

///朝向目標

////summary

///param

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論