zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Best Time to Buy and Sell Stock I II III
    数据挖掘算法面试题
    C# 从CIL代码了解委托,匿名方法,Lambda 表达式和闭包本质
    ASP.NET MVC 5
    net破解一(反编译,反混淆-剥壳,工具推荐)
    面试题及相关参考答案
    Linux 查看内核版本命令的相关说明
    c# 获取应用程序exe文件路径及退出应用程序的几种方法
    C# WebBrowser设置代理
    c# combobox控件的使用
  • 原文地址:https://www.cnblogs.com/Dean27/p/8603983.html
Copyright © 2011-2022 走看看