国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

unity實(shí)現(xiàn)鼠標(biāo)拖住3D物體

2020-01-24 00:03:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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 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; } }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石阡县| 抚州市| 乌恰县| 张家川| 大邑县| 左云县| 隆安县| 万载县| 张北县| 海南省| 商城县| 班玛县| 和林格尔县| 和顺县| 西吉县| 南平市| 海城市| 三门县| 舞钢市| 高尔夫| 河源市| 贵阳市| 云安县| 正宁县| 怀安县| 信宜市| 太和县| 从江县| 杭锦旗| 兰西县| 剑阁县| 繁昌县| 措勤县| 北京市| 土默特右旗| 江西省| 道孚县| 邵阳市| 达拉特旗| 封开县| 金乡县|