Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動(dòng)
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
相關(guān)函數(shù)
Vector3.Lerp 線性插值
C# => static Vector3 Lerp(Vector3 from, Vector3 to, float t);
Vector3.MoveTpwards 移向
C# => static function MoveTowards(current: Vector3, target: Vector3, maxDistanceDelta: float): Vector3;
Vector3.SmoothDamp 平滑阻尼
C# =>static Vector3 SmoothDamp(Vector3 current, Vector3 target, Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);
著時(shí)間的推移,逐漸改變一個(gè)向量朝向預(yù)期的方向移動(dòng)
向量由一些像彈簧阻尼器函數(shù)平滑,這將用遠(yuǎn)不會(huì)超過(guò),最常見(jiàn)的用途是跟隨相機(jī)
實(shí)現(xiàn)跟隨鼠標(biāo)運(yùn)動(dòng)
public class Demo : MonoBehaviour { ? ? ? void Start () { ? ? } ? ? ? void Update () { ? ? ? ? // 此時(shí)的攝像機(jī)必須轉(zhuǎn)換 2D攝像機(jī) 來(lái)實(shí)現(xiàn)效果(即:攝像機(jī)屬性Projection --> Orthographic) ? ? ? ? Vector3 dis = Camera.main.ScreenToWorldPoint(Input.mousePosition); //獲取鼠標(biāo)位置并轉(zhuǎn)換成世界坐標(biāo) ? ? ? ? dis.z = this.transform.position.z; //固定z軸 ? ? ? ? this.transform.position = dis; //使物體跟隨鼠標(biāo)移動(dòng) ? ? ? ? Debug.Log(dis); //輸出變化的位置 ? ? ? ? //使用Lerp方法實(shí)現(xiàn) 這里的Time.deltaTime是指移動(dòng)速度可以自己添加變量方便控制 ? ? ? ? this.transform.position= Vector3.Lerp(this.transform.position,dis,Time.deltaTime); ? ? ? ? //使用MoveTowards方法實(shí)現(xiàn),這個(gè)方法是勻速運(yùn)動(dòng) ? ? ? ? this.transform.position = Vector3.MoveTowards(this.transform.position, dis, Time.deltaTime); ? ? ? ? //使用SmoothDamp方式實(shí)現(xiàn),給定時(shí)間可以獲取到速度 ? ? ? ? Vector3 speed = Vector3.zero; ? ? ? ? this.transform.position = Vector3.SmoothDamp(this.transform.position, dis,ref speed, 0.1f); ? ? ? ? Debug.Log(speed); ? ? } }
根據(jù)鼠標(biāo)點(diǎn)下位置移動(dòng)物體:
public class Move : MonoBehaviour { ? ? void Start() ? ? { ? ? ? ?? ? ? } ? ? ? void Update() ? ? { ? ? ? ? if (Input.GetMouseButton(0)) ? ? ? ? { ? ? ? ? ? ? //獲取需要移動(dòng)物體的世界轉(zhuǎn)屏幕坐標(biāo) ? ? ? ? ? ? Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position); ? ? ? ? ? ? //獲取鼠標(biāo)位置 ? ? ? ? ? ? Vector3 mousePos = Input.mousePosition; ? ? ? ? ? ? //因?yàn)槭髽?biāo)只有X,Y軸,所以要賦予給鼠標(biāo)Z軸 ? ? ? ? ? ? mousePos.z = screenPos.z; ? ? ? ? ? ? //把鼠標(biāo)的屏幕坐標(biāo)轉(zhuǎn)換成世界坐標(biāo) ? ? ? ? ? ? Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos); ? ? ? ? ? ? //控制物體移動(dòng) ? ? ? ? ? ? transform.position = worldPos; ? ? ? ? ? ? //剛體的方式 ? ? ? ? ? ? //transform.GetComponent<Rigidbody>().MovePosition(worldPos); ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# SqlDataAdapter中的Fill是怎么實(shí)現(xiàn)的
這篇文章主要介紹了c# SqlDataAdapter中的Fill是怎么實(shí)現(xiàn)的,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07C#執(zhí)行表達(dá)式樹(shù)(Expression Tree)的具體使用
本文將深入探討表達(dá)式樹(shù)的基本概念、創(chuàng)建方法、修改和刪除節(jié)點(diǎn)、查詢和遍歷技巧以及在C#中的應(yīng)用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03C# 數(shù)據(jù)庫(kù)鏈接字符串加密解密工具代碼詳解
本文通過(guò)代碼給大家介紹C# 數(shù)據(jù)庫(kù)鏈接字符串加密解密工具的相關(guān)知識(shí),實(shí)現(xiàn)思路大概是使用兩個(gè)數(shù)對(duì)連接字符串進(jìn)行加密,再用這兩個(gè)數(shù)進(jìn)行解密,具體詳細(xì)代碼,大家參考下本文2018-05-05基于C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08