纳金网

标题: 点击按钮实例化 - 跟随鼠标把物体放到自己想放的位置 [打印本页]

作者: 烟雨    时间: 2015-9-29 01:00
标题: 点击按钮实例化 - 跟随鼠标把物体放到自己想放的位置

点击按钮 实例化 预设 跟随鼠标 把物体放到自己想放的位置

  1. using unityEngine;
  2. using System.Collections;
  3. public class NewBehaviourScript : MonoBehaviour
  4. {
  5.     private Vector3 m_MousePosion; //鼠标位置
  6.     private RaycastHit m_hit;//射线碰撞点
  7.     private Ray m_ray;//射线
  8.     public GameObject m_Pref; //预设
  9.     private GameObject go;
  10.     private bool m_is;
  11.     public Camera m_cam;//相机
  12.     // Use this for initialization
  13.     void Start()
  14.     {
  15.         if (!m_Pref)
  16.         {
  17.             Debug.Log("Add Pref");
  18.         }
  19.     }
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.         m_ray = m_cam.ScreenPointToRay(Input.mousePosition);
  24.         if (Physics.Raycast(m_ray, out m_hit))//射线检测
  25.         {
  26.             if (m_hit.collider.name == "Cub1")
  27.             {
  28.                 m_is = false;
  29.                 go.transform.position = m_hit.transform.position; //物体摆放位置
  30.             }
  31.         }
  32.     }
  33.     void OnGUI()
  34.     {
  35.         if (GUILayout.Button("click"))//点击鼠标 实例化 预设物体
  36.         {
  37.             go = (GameObject)Instantiate(m_Pref, m_Pref.transform.position, Quaternion.identity);
  38.             m_is = true;
  39.         }
  40.         if (m_is)
  41.         {
  42.             getMove();
  43.         }
  44.     }
  45.     void getMove()//同步物体随鼠标鼠标
  46.     {
  47.         m_MousePosion = Input.mousePosition;
  48.         go.transform.position = m_cam.ScreenToWorldPoint(new Vector3(m_MousePosion.x, m_MousePosion.y, 5));
  49.     }
  50. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5