網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(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); ? ? ? ? } ? ? } }
原文鏈接:https://czhenya.blog.csdn.net/article/details/76615325
相關(guān)推薦
- 2022-12-15 深入了解Golang中的格式化輸出_Golang
- 2022-08-31 ES6變量賦值和基本數(shù)據(jù)類型詳解_基礎(chǔ)知識(shí)
- 2022-06-09 ASP.NET?Core中的通用主機(jī)HostBuilder_基礎(chǔ)應(yīng)用
- 2022-10-30 在C#程序中注入惡意DLL的方法詳解_C#教程
- 2023-02-17 pytorch中nn.Flatten()函數(shù)詳解及示例_python
- 2022-07-07 Python常用內(nèi)置函數(shù)和關(guān)鍵字使用詳解_python
- 2022-12-21 Redis數(shù)據(jù)庫(kù)原理深入刨析_Redis
- 2022-11-08 python安裝包出現(xiàn)Retrying?(Retry(total=4,?connect=None,?
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支