日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现鼠标控制场景的视野及移动

發布時間:2024/1/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现鼠标控制场景的视野及移动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

1.移動場景中的物體

using UnityEngine; using System.Collections;public class DragAndDrog : MonoBehaviour {private GameObject target;private bool isMouseDrag;private Vector3 screenPosition;private Vector3 offset;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {GameObjectDragAndDrog();}//任意拖拽private GameObject ReturnGameObjectDrag(out RaycastHit hit){target = null;Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);if (Physics.Raycast(ray.origin, ray.direction * 10, out hit)){target = hit.collider.gameObject;}return target;}//拖拽Updataprivate void GameObjectDragAndDrog(){if (Input.GetMouseButtonDown (0)){RaycastHit hitInfo;target = ReturnGameObjectDrag(out hitInfo);if (target != null){isMouseDrag = true; //將目標對象的世界坐標轉化為屏幕坐標screenPosition = Camera.main.WorldToScreenPoint(target.transform.position); //得到鼠標的世界坐標與目標世界坐標的偏移量offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));} }if (Input.GetMouseButtonUp(0)){isMouseDrag = false;}if (isMouseDrag){Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z); //鼠標的世界坐標加上偏移量,即為目標對象的終點Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset; //target.transform.localPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z);}}}本腳本可以加載任意一個對象GameObject,場景中所有帶Collider的對象,當鼠標點擊拖動時候都可以沿x和z軸拖動。之前,都是要拖動那個GameObject就會寫一個腳本掛在上面,今天實現了不用每一個要拖動的GameObject都掛載腳本,也不用將移動的GameObject賦給某個變量。

?

2。鼠標滾輪控制視野

void Update () {//鼠標滾輪的效果//Camera.main.fieldOfView 攝像機的視野//Camera.main.orthographicSize 攝像機的正交投影//Zoom outif (Input.GetAxis("Mouse ScrollWheel") < 0){if (Camera.main.fieldOfView <= 100)Camera.main.fieldOfView += 2;if (Camera.main.orthographicSize <= 20)Camera.main.orthographicSize += 0.5F;}//Zoom inif (Input.GetAxis("Mouse ScrollWheel") > 0){if (Camera.main.fieldOfView > 2)Camera.main.fieldOfView -= 2;if (Camera.main.orthographicSize >= 1)Camera.main.orthographicSize -= 0.5F;}}

?

總結

以上是生活随笔為你收集整理的实现鼠标控制场景的视野及移动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。