




版權(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025福建一建集團招聘見習生1人的模擬試卷及參考答案詳解一套
- 2025年濟柴動力有限公司春季高校畢業(yè)生招聘(10人)考前自測高頻考點模擬試題及參考答案詳解1套
- 2025華東理工大學材料科學與工程學院高分子材料人工智能研發(fā)創(chuàng)新團隊招聘(上海)模擬試卷含答案詳解
- 2025江蘇淮安市金湖縣面向鄉(xiāng)村定向師范生招聘及公開招聘教師73人模擬試卷及參考答案詳解1套
- 2025年煙臺市衛(wèi)生健康委員會所屬事業(yè)單位衛(wèi)生類崗位公開招聘工作人員(385人)模擬試卷完整答案詳解
- 2025年知識產(chǎn)權(quán)授權(quán)合同協(xié)議
- PARG-IN-6-生命科學試劑-MCE
- p38-MAPK-IN-8-生命科學試劑-MCE
- Onychocin-B-生命科學試劑-MCE
- 安全守護測試題及答案
- 注銷代理協(xié)議書
- 2025-2026學年高一上學期第一次月考化學試卷(福建)
- 2025年“世界糧食日”主題活動知識競賽考試題庫150題(含答案)
- LED道路照明燈具采購安裝合同(含安全防護與智能化升級)
- 《童年》整本書解讀與教學設(shè)計
- 卡林巴琴課件
- 甘肅工裝裝修施工方案
- 2025江蘇省藥品監(jiān)督管理局審評中心招聘3人考試參考題庫及答案解析
- 課堂觀察記錄與學生行為分析報告
- 黑龍江省齊齊哈爾市九校2025-2026學年高三上學期期初聯(lián)考英語試題(含答案)
- AI技術(shù)在遺傳學課程教學中的創(chuàng)新路徑
評論
0/150
提交評論