- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
 
- 纳金币
- 53488
- 精华
- 316
|
点击按钮 实例化 预设 跟随鼠标 把物体放到自己想放的位置
- using unityEngine;
- using System.Collections;
- public class NewBehaviourScript : MonoBehaviour
- {
- private Vector3 m_MousePosion; //鼠标位置
- private RaycastHit m_hit;//射线碰撞点
- private Ray m_ray;//射线
- public GameObject m_Pref; //预设
- private GameObject go;
- private bool m_is;
- public Camera m_cam;//相机
- // Use this for initialization
- void Start()
- {
- if (!m_Pref)
- {
- Debug.Log("Add Pref");
- }
- }
- // Update is called once per frame
- void Update()
- {
- m_ray = m_cam.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(m_ray, out m_hit))//射线检测
- {
- if (m_hit.collider.name == "Cub1")
- {
- m_is = false;
- go.transform.position = m_hit.transform.position; //物体摆放位置
- }
- }
- }
- void OnGUI()
- {
- if (GUILayout.Button("click"))//点击鼠标 实例化 预设物体
- {
- go = (GameObject)Instantiate(m_Pref, m_Pref.transform.position, Quaternion.identity);
- m_is = true;
- }
- if (m_is)
- {
- getMove();
- }
- }
- void getMove()//同步物体随鼠标鼠标
- {
- m_MousePosion = Input.mousePosition;
- go.transform.position = m_cam.ScreenToWorldPoint(new Vector3(m_MousePosion.x, m_MousePosion.y, 5));
- }
- }
复制代码 |
|