- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- unity怎么實(shí)現鼠標拖住3D物體
本篇內容介紹了“unity怎么實(shí)現鼠標拖住3D物體”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!
把該腳本直接掛在要拖拽的物體上即可
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ModelDrages : MonoBehaviour {//發(fā)射射線(xiàn)的攝像機private Camera cam;//射線(xiàn)碰撞的物體private GameObject go;//射線(xiàn)碰撞物體的名字public static string btnName;private Vector3 screenSpace;private Vector3 offset;private bool isDrage = false;// Use this for initializationvoid Start (){ cam = Camera.main;}// Update is called once per frame void Update (){ //整體初始位置 Ray ray = cam.ScreenPointToRay(Input.mousePosition); //從攝像機發(fā)出到點(diǎn)擊坐標的射線(xiàn) RaycastHit hitInfo; if (isDrage == false) { if(Physics .Raycast (ray,out hitInfo)) { //劃出射線(xiàn) 只有在Scene視圖中才能看到 Debug.DrawLine(ray.origin, hitInfo.point); go = hitInfo.collider.gameObject; print(btnName); screenSpace = cam.WorldToScreenPoint(go.transform.position); offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z)); //物體的名字 btnName = go.name; //組件的名字 } else { btnName = null; } } if(Input.GetMouseButton(0)) { Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset; if (btnName != null) { go.transform.position = currentPosition; } isDrage = true; } else { isDrage = false; } }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站