• Unity中关于射线的运用——第03节 射线的实际运用


        1.设计思路:界面会在某个区域随机生成球形,并且当我们点击鼠标右键并且点中球形并且鼠标抬起,则会销毁球形。

        2.创建球形脚本:

    #region 模块信息
    // **********************************************************************
    // Copyright (C) 2018 The company name
    //
    // 文件名(File Name):             Test_SceneController.cs
    // 作者(Author):                  Dean1874
    // 创建时间(CreateTime):          2018-03-19 17:35:20
    // 修改者列表(modifier):
    // 模块描述(Module description):  Test级别的场景控制器
    // 
    // **********************************************************************
    #endregion
    
    using System;
    using UnityEngine;
    
    public class Test_SceneController : MonoBehaviour 
    {
        /// <summary>
        /// 创建球形的区域
        /// </summary>
        private Transform transCreateSphereArea;
    
        /// <summary>
        /// 球形的父物体
        /// </summary>
        private Transform sphereParent;
    
        /// <summary>
        /// 球形预设
        /// </summary>
        private GameObject m_SpherePrefab;
    
        /// <summary>
        /// 球形的上限值
        /// </summary>
        private int m_MaxCount = 10;
    
        /// <summary>
        /// 下次克隆时间
        /// </summary>
        private float m_NextCloneTime = 0.0f;
    
        /// <summary>
        /// 当前箱子数量
        /// </summary>
        private int m_CurrentCount = 0;
    
        private void Awake()
        {
            transCreateSphereArea = GameObject.Find("CreateSphereArea").GetComponent<Transform>();
            sphereParent = GameObject.Find("SphereAll").GetComponent<Transform>();
        }
    
        private void Start()
        {
            m_SpherePrefab = Resources.Load<GameObject>("Test/Sphere_00");
            Debug.Log("m_SpherePrefab = " + m_SpherePrefab);
        }
    
        private void Update()
        {
            if (m_CurrentCount < m_MaxCount)
            {
                if (Time.time > m_NextCloneTime)
                {
                    //克隆
                    Clone();
                }
            }
        }
    
        private void Clone()
        {
            m_NextCloneTime = Time.time + 0.5f;
            GameObject objClone = Instantiate(m_SpherePrefab);
            objClone.transform.SetParent(sphereParent, false);
            objClone.transform.position = transCreateSphereArea.transform.TransformPoint(new Vector3(UnityEngine.Random.Range(-0.5f, 0.5f), 0, UnityEngine.Random.Range(-0.5f, 0.5f)));
            Test_BoxController test_BoxController = objClone.GetComponent<Test_BoxController>();
            if (test_BoxController != null)
            {
                test_BoxController.OnHit = SphereHit;
            }
            m_CurrentCount += 1;
        }
    
        private void SphereHit(GameObject obj)
        {
            m_CurrentCount -= 1;
            Destroy(obj);
        }
    }

        3.挂载在球形上的脚本

    #region 模块信息
    // **********************************************************************
    // Copyright (C) 2018 The company name
    //
    // 文件名(File Name):             Test_BoxController.cs
    // 作者(Author):                  Dean1874
    // 创建时间(CreateTime):          2018-03-18 16:14:07
    // 修改者列表(modifier):          Dean1874
    // 模块描述(Module description):  测试移动脚本
    // 
    // **********************************************************************
    #endregion
    
    using UnityEngine;
    using System;
    
    public class Test_BoxController : MonoBehaviour
    {
        public Action<GameObject> OnHit;
    
        public void Hit()
        {
            if (OnHit != null)
            {
                OnHit(gameObject);
            }
        }
    }

        4.角色控制脚本(部分)

     #region 射线的运用
            if (Input.GetMouseButtonUp(1))
            {
                Ray ray = m_RoleCamera.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Item")))
                {
                    //Destroy(hit.collider.gameObject);
                    Test_BoxController boxController = hit.collider.GetComponent<Test_BoxController>();
                    if (boxController != null)
                    {
                        boxController.Hit();
                    }
                    //Debug.Log(hit.collider.name);
                }
            }
    
            #endregion
  • 相关阅读:
    Deploying an Application on X11 Platforms
    使用xenu查找web站点死链接使用方法及结果分析 Binbby 博客园
    About | Channel 9
    Amber is an implementation of the Smalltalk language that runs on top of the JavaScript runtime.
    linux下qt静态编译_自由出土文物的空间_百度空间
    A year of Qt ecosystem growth with Digia nurturing Qt Commercial
    华汇超市二层的小餐馆可当MBA案例了
    js web tools
    [转]PAC Manager: Ubuntu 上强大的 SSH 帐号管理工具,可取代 SecureCRT_Miracle_百度空间
    qq云输入法也支持五笔了,太适合我了,特别是在ubuntu下有时候输入法
  • 原文地址:https://www.cnblogs.com/Dean27/p/8603983.html
Copyright © 2020-2023  润新知