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#中Dictionary<TKey,TValue>的存儲(chǔ)結(jié)構(gòu)
無(wú)論是實(shí)際的項(xiàng)目中,還是在我們學(xué)習(xí)的過(guò)程中,都會(huì)重點(diǎn)的應(yīng)用到Dictionary<TKey,?TValue>這個(gè)存儲(chǔ)類型,所以本文就來(lái)為大家介紹一下這一存儲(chǔ)結(jié)構(gòu)的相關(guān)知識(shí),希望對(duì)大家有所幫助2023-11-11
c#實(shí)現(xiàn)一個(gè)超實(shí)用的證件照換底色小工具(附源碼)
這篇文章主要給大家介紹了關(guān)于利用c#實(shí)現(xiàn)一個(gè)超實(shí)用的證件照換底色小工具的相關(guān)資料,通過(guò)這個(gè)小工具大家可以很方便的進(jìn)行底色的切換,不用再因?yàn)榈咨脑蝾^疼了,需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
C#中如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫界面
這篇文章講解了如何使用Winform實(shí)現(xiàn)炫酷的透明動(dòng)畫界面,Winform相對(duì)于Wpf使用更簡(jiǎn)單一些,系統(tǒng)要求更低,需要了解的朋友可以參考下2015-07-07
c#創(chuàng)建Graphics對(duì)象的三種方法
通常我們使用下述三種方法來(lái)創(chuàng)建一個(gè)Graphics對(duì)象。2013-05-05

