本篇內(nèi)容介紹了“unity怎么實(shí)現(xiàn)鼠標(biāo)拖住3D物體”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站是一家專業(yè)提供三江侗企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為三江侗眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
把該腳本直接掛在要拖拽的物體上即可
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ModelDrages : MonoBehaviour {//發(fā)射射線的攝像機(jī)private Camera cam;//射線碰撞的物體private GameObject go;//射線碰撞物體的名字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); //從攝像機(jī)發(fā)出到點(diǎn)擊坐標(biāo)的射線 RaycastHit hitInfo; if (isDrage == false) { if(Physics .Raycast (ray,out hitInfo)) { //劃出射線 只有在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; } }}“unity怎么實(shí)現(xiàn)鼠標(biāo)拖住3D物體”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前文章:unity怎么實(shí)現(xiàn)鼠標(biāo)拖住3D物體
鏈接URL:http://chinadenli.net/article28/ipcgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站收錄、用戶體驗(yàn)、移動網(wǎng)站建設(shè)、網(wǎng)站制作、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)