unity實(shí)現(xiàn)鼠標(biāo)拖住3D物體
本文實(shí)例為大家分享了unity實(shí)現(xiàn)鼠標(biāo)拖住3D物體的具體代碼,供大家參考,具體內(nèi)容如下
把該腳本直接掛在要拖拽的物體上即可
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 initialization void 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; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)把txt文本數(shù)據(jù)快速讀取到excel中
這篇文章主要介紹了C#實(shí)現(xiàn)把txt文本數(shù)據(jù)快速讀取到excel中,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06C#中的IEnumerable簡(jiǎn)介及簡(jiǎn)單實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了C#中的IEnumerable簡(jiǎn)介及簡(jiǎn)單實(shí)現(xiàn)實(shí)例,本文講解了IEnumerable一些知識(shí)并給出了一個(gè)簡(jiǎn)單的實(shí)現(xiàn),需要的朋友可以參考下2015-03-03C#實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能
最近小編接到一個(gè)功能需要實(shí)現(xiàn)一個(gè)提示框并且能自動(dòng)關(guān)閉的,看到這個(gè)需求真是懵了,四處搜集資料才搞定,接下來(lái)通過(guò)本文給大家分享C#實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉提示框功能,感興趣的朋友跟隨小編一起看看吧2019-07-07C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射實(shí)例分析
這篇文章主要介紹了C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射的方法,實(shí)例分析了反射的原理、特性與ORM的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#對(duì)WPF數(shù)據(jù)綁定的菜單插入Seperator分隔
這篇文章介紹了C#對(duì)WPF數(shù)據(jù)綁定的菜單插入Seperator分隔的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06