Unity 控制射线检测销毁物体

开发环境的安装与配置

unity2021
在这里插入图片描述
链接:https://unity.cn/
vs2022
在这里插入图片描述
链接:https://visualstudio.microsoft.com/zh-hans/

游戏场景搭建

场景搭建创建四个3D物体Cube,Sphere,Cylinder,Capsule
在这里插入图片描述

素材制作与场景完善

将四个3D物体Cube,Sphere,Cylinder,Capsule设为预制体放置在Resources/Prefabs文件夹下
在这里插入图片描述

使用unity的UGUI组件中的Text搭建好屏幕中央的“+”与物体名字显示文本

在这里插入图片描述

游戏应用程序功能实现

实现移动摄像机一项功能
public class MirMove : MonoBehaviour
{
    private GameObject m_Camera;
    public float Speed=30;

    void Awake()
    {
       //找到主相机
        m_Camera = GameObject.Find("Main Camera");
        Txt_ModleName = transform.parent.Find("ShowName").GetComponent<Text>();
        Cube = Resources.Load<GameObject>("Prefabs/Cube");
        Sphere = Resources.Load<GameObject>("Prefabs/Sphere");
        Cylinder = Resources.Load<GameObject>("Prefabs/Cylinder");
        Capsule = Resources.Load<GameObject>("Prefabs/Capsule");
    }
    //相机移动
    private void MirMoveto() {
        h = Input.GetAxis("Horizontal"); ;
        v = Input.GetAxis("Vertical");
        Vector3 moveAt = new Vector3(h, v, 0) * Speed * Time.deltaTime;
        m_Camera.transform.Translate(moveAt);
    }
 }
实现屏幕射线检测物体,并在文本中显示物体名字一项功能

在这里插入图片描述

//射线检测
 private GameObject RayPut() {
        Vector3 Pos = Camera.main.transform.position;
        Ray ray = new Ray(Pos, Camera.main.transform.forward);
        RaycastHit hit;
        GameObject modle = null;
        if (Physics.Raycast(ray, out hit, raycastDistance))
        {
            modle = hit.collider.gameObject;
            Debug.Log("Hit object: " + modle.name);
            Txt_ModleName.text = "已经瞄准:" + modle.name;
        }
        if (modle == null)
        {
            Txt_ModleName.text = "";
        }
        return modle == null?null: modle;
    }
实现鼠标左键点击后销毁物体一项功能
//鼠标点击销毁物体
private void isMouseDown() {
        if (Input.GetMouseButtonDown(0)) {
           GameObject game = RayPut();
            if (game != null) {
                string name = game.name;
                Transform tr=game.transform;
                Destroy(game);
                StartCoroutine(Countdown(name,tr));
            }
        }
    }

实现物体销毁后自动重新生成一项功能

//协程实现倒计时两秒后重新生成销毁物体
IEnumerator Countdown(string name,Transform tr)
    {
        for (int i = time; i >= 0; i--)
        {
            yield return new WaitForSeconds(1f);
            Debug.Log("Countdown: " + i + " seconds");
        }
        GameObject obj = null;
        switch (name)
        {
            case "Cube":
                obj = Instantiate(Cube, tr);
                obj.name = "Cube";
                break;
            case "Sphere":
                obj = Instantiate(Sphere, tr);
                obj.name = "Sphere";
                break;
            case "Cylinder":
                obj = Instantiate(Cylinder, tr);
                obj.name = "Cylinder";
                break;
            case "Capsule":
                obj = Instantiate(Capsule, tr);
                obj.name = "Capsule";
                break;
            default:
                break;
        }
    }

每帧更新

     void Update()
    {
        MirMoveto();
        RayPut(); 
        isMouseDown();
    }
全部代码
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class MirMove : MonoBehaviour
{
    private GameObject m_Camera;
    public float Speed=30;
    private float raycastDistance = 100.0f;
    private int time = 2;
    private Text Txt_ModleName;
    private float h;
    private float v;
    private GameObject Cube;
    private GameObject Sphere;
    private GameObject Cylinder;
    private GameObject Capsule;

    void Awake()
    {
        m_Camera = GameObject.Find("Main Camera");
        Txt_ModleName = transform.parent.Find("ShowName").GetComponent<Text>();
        Cube = Resources.Load<GameObject>("Prefabs/Cube");
        Sphere = Resources.Load<GameObject>("Prefabs/Sphere");
        Cylinder = Resources.Load<GameObject>("Prefabs/Cylinder");
        Capsule = Resources.Load<GameObject>("Prefabs/Capsule");
    }

    void Start()
    {
        //StartCoroutine(Countdown(2)); // 启动一个2秒的倒计时协程
    }

    IEnumerator Countdown(string name,Transform tr)
    {
        for (int i = time; i >= 0; i--)
        {
            yield return new WaitForSeconds(1f);
            Debug.Log("Countdown: " + i + " seconds");
        }
        GameObject obj = null;
        switch (name)
        {
            case "Cube":
                obj = Instantiate(Cube, tr);
                obj.name = "Cube";
                break;
            case "Sphere":
                obj = Instantiate(Sphere, tr);
                obj.name = "Sphere";
                break;
            case "Cylinder":
                obj = Instantiate(Cylinder, tr);
                obj.name = "Cylinder";
                break;
            case "Capsule":
                obj = Instantiate(Capsule, tr);
                obj.name = "Capsule";
                break;
            default:
                break;
        }
    }
    private void MirMoveto() {
        h = Input.GetAxis("Horizontal"); ;
        v = Input.GetAxis("Vertical");
        Vector3 moveAt = new Vector3(h, v, 0) * Speed * Time.deltaTime;
        m_Camera.transform.Translate(moveAt);
    }
    private GameObject RayPut() {
        Vector3 Pos = Camera.main.transform.position;
        Ray ray = new Ray(Pos, Camera.main.transform.forward);
        RaycastHit hit;
        GameObject modle = null;
        if (Physics.Raycast(ray, out hit, raycastDistance))
        {
            modle = hit.collider.gameObject;
            Debug.Log("Hit object: " + modle.name);
            Txt_ModleName.text = "已经瞄准:" + modle.name;
        }
        if (modle == null)
        {
            Txt_ModleName.text = "";
        }
        return modle == null?null: modle;
    }
    private void isMouseDown() {
        if (Input.GetMouseButtonDown(0)) {
           GameObject game = RayPut();
            if (game != null) {
                string name = game.name;
                Transform tr=game.transform;
                Destroy(game);
                StartCoroutine(Countdown(name,tr));
            }
        }
    }
     void Update()
    {
        MirMoveto();
        RayPut(); 
        isMouseDown();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值