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

