Unity實(shí)現(xiàn)弧形移動(dòng)效果
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)弧形移動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
一、實(shí)現(xiàn)效果
二、第一種實(shí)現(xiàn)方法——弧形插值
using UnityEngine; public class ArcMove : MonoBehaviour { public GameObject curGo;//當(dāng)前物體 public GameObject targetGo;//目標(biāo)物體 public float value; private void Update() { //計(jì)算中心點(diǎn) Vector3 center = (curGo.transform.position + targetGo.transform.position) / 2; center -= new Vector3(0, value, 0); Vector3 start = curGo.transform.position - center; Vector3 end = targetGo.transform.position - center; //弧形插值 curGo.transform.position = Vector3.Slerp(start, end, Time.time / 200); curGo.transform.position += center; //判定是否到達(dá)目標(biāo)點(diǎn) float dis = Vector3.Distance(curGo.transform.position, targetGo.transform.position); if (dis <= 1) { Debug.Log("到達(dá)目標(biāo)點(diǎn)"); } } }
三、第二種實(shí)現(xiàn)方法——計(jì)算弧線中的夾角
using UnityEngine; using System.Collections; public class ArcMove : MonoBehaviour { public GameObject curGo;//當(dāng)前物體 public GameObject targetGo;//目標(biāo)物體 public float speed = 2;//速度 public int rotationAngle = 60;//旋轉(zhuǎn)的角度 private float distanceToTarget;//兩者之間的距離 private bool move = true;//是否移動(dòng) void Start() { //計(jì)算兩者之間的距離 distanceToTarget = Vector3.Distance(curGo.transform.position, targetGo.transform.position); StartCoroutine(Move()); } /// <summary> /// 移動(dòng) /// </summary> private IEnumerator Move() { //移動(dòng)到目標(biāo)點(diǎn)停止移動(dòng) while (move) { Vector3 targetPos = targetGo.transform.position; targetPos.z = 0; //讓它始終朝著目標(biāo) curGo.transform.LookAt(targetPos); //計(jì)算弧線中的夾角 float angle = Mathf.Min(1, Vector3.Distance(curGo.transform.position, targetPos) / distanceToTarget) * rotationAngle; curGo.transform.rotation = curGo.transform.rotation * Quaternion.Euler(Mathf.Clamp(-angle, -42, 42), 0, 0); float currentDist = Vector3.Distance(curGo.transform.position, targetGo.transform.position); if (currentDist < 0.5f) { move = false; Debug.Log("到達(dá)目標(biāo)點(diǎn)"); } curGo.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist)); yield return null; } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)
- Unity鍵盤WASD實(shí)現(xiàn)物體移動(dòng)
- Unity3D實(shí)現(xiàn)虛擬按鈕控制人物移動(dòng)效果
- Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)效果
- Unity實(shí)現(xiàn)物體左右移動(dòng)效果
- Unity3D控件Easytouch控制主角移動(dòng)
- Unity3D實(shí)現(xiàn)人物轉(zhuǎn)向與移動(dòng)
- Unity調(diào)取移動(dòng)端的麥克風(fēng)進(jìn)行錄音并播放
- Unity3D實(shí)現(xiàn)人物移動(dòng)示例
- Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果
相關(guān)文章
C#/VB.NET實(shí)現(xiàn)創(chuàng)建PDF/UA文件的示例代碼
PDF/UA,即Universally?Accessible?PDF,該格式的PDF文件是于2012年8月以ISO標(biāo)準(zhǔn)14289-1發(fā)布的、具有普遍可訪問的PDF文檔標(biāo)準(zhǔn)。本文將用C#實(shí)現(xiàn)DF/UA文件的創(chuàng)建,需要的可以參考一下2022-08-08C# Socket連接請求超時(shí)機(jī)制實(shí)現(xiàn)代碼分享
這篇文章主要介紹了C# Socket連接請求超時(shí)機(jī)制實(shí)現(xiàn),下面提供代碼分享,大家可以參考使用2013-12-12Unity實(shí)現(xiàn)弧形移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)弧形移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Stream.Write 與 StreamWriter.Write 的不同
Stream.Write 與 StreamWriter.Write 是我們在向流中寫數(shù)據(jù)時(shí),最常用的方法。下面就詳細(xì)講解這兩個(gè)方法。2013-04-04C#操作DataTable方法實(shí)現(xiàn)過濾、取前N條數(shù)據(jù)及獲取指定列數(shù)據(jù)列表的方法
這篇文章主要介紹了C#操作DataTable方法實(shí)現(xiàn)過濾、取前N條數(shù)據(jù)及獲取指定列數(shù)據(jù)列表的方法,實(shí)例分析了C#操作DataTable的各種常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04