網站首頁 編程語言 正文
本文實例為大家分享了Unity實現物體跟隨鼠標移動的具體代碼,供大家參考,具體內容如下
相關函數
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);
著時間的推移,逐漸改變一個向量朝向預期的方向移動
向量由一些像彈簧阻尼器函數平滑,這將用遠不會超過,最常見的用途是跟隨相機
實現跟隨鼠標運動
public class Demo : MonoBehaviour { ? ? ? void Start () { ? ? } ? ? ? void Update () { ? ? ? ? // 此時的攝像機必須轉換 2D攝像機 來實現效果(即:攝像機屬性Projection --> Orthographic) ? ? ? ? Vector3 dis = Camera.main.ScreenToWorldPoint(Input.mousePosition); //獲取鼠標位置并轉換成世界坐標 ? ? ? ? dis.z = this.transform.position.z; //固定z軸 ? ? ? ? this.transform.position = dis; //使物體跟隨鼠標移動 ? ? ? ? Debug.Log(dis); //輸出變化的位置 ? ? ? ? //使用Lerp方法實現 這里的Time.deltaTime是指移動速度可以自己添加變量方便控制 ? ? ? ? this.transform.position= Vector3.Lerp(this.transform.position,dis,Time.deltaTime); ? ? ? ? //使用MoveTowards方法實現,這個方法是勻速運動 ? ? ? ? this.transform.position = Vector3.MoveTowards(this.transform.position, dis, Time.deltaTime); ? ? ? ? //使用SmoothDamp方式實現,給定時間可以獲取到速度 ? ? ? ? Vector3 speed = Vector3.zero; ? ? ? ? this.transform.position = Vector3.SmoothDamp(this.transform.position, dis,ref speed, 0.1f); ? ? ? ? Debug.Log(speed); ? ? } }
根據鼠標點下位置移動物體:
public class Move : MonoBehaviour { ? ? void Start() ? ? { ? ? ? ?? ? ? } ? ? ? void Update() ? ? { ? ? ? ? if (Input.GetMouseButton(0)) ? ? ? ? { ? ? ? ? ? ? //獲取需要移動物體的世界轉屏幕坐標 ? ? ? ? ? ? Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position); ? ? ? ? ? ? //獲取鼠標位置 ? ? ? ? ? ? Vector3 mousePos = Input.mousePosition; ? ? ? ? ? ? //因為鼠標只有X,Y軸,所以要賦予給鼠標Z軸 ? ? ? ? ? ? mousePos.z = screenPos.z; ? ? ? ? ? ? //把鼠標的屏幕坐標轉換成世界坐標 ? ? ? ? ? ? Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos); ? ? ? ? ? ? //控制物體移動 ? ? ? ? ? ? transform.position = worldPos; ? ? ? ? ? ? //剛體的方式 ? ? ? ? ? ? //transform.GetComponent<Rigidbody>().MovePosition(worldPos); ? ? ? ? } ? ? } }
原文鏈接:https://czhenya.blog.csdn.net/article/details/76615325
相關推薦
- 2022-08-22 pyecharts繪制時間輪播圖柱形圖+餅圖+玫瑰圖+折線圖_python
- 2022-07-10 uniapp創建自定義組件報錯
- 2022-03-05 C語言宏函數container?of()簡介_C 語言
- 2022-03-31 利用OpenCV實現質心跟蹤算法_python
- 2022-01-09 ng-zorro中樹(nz-tree)的拖拽
- 2022-10-17 一文教會你用nginx+uwsgi部署自己的django項目_python
- 2022-04-27 Numpy的各種下標操作的示例代碼_python
- 2022-04-05 本地文件提交到gitee上報錯的問題:error: src refspec master does
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支