Unity controls ray detection to destroy objects

Installation and configuration of development environment

unity2021

Link: https://unity.cn/
vs2022

Link: https://visualstudio.microsoft.com/zh-hans/

Game scene construction

Scene construction creates four 3D objects Cube, Sphere, Cylinder, and Capsule

Material production and scene improvement

Set the four 3D objects Cube, Sphere, Cylinder and Capsule as prefabs and place them in the Resources/Prefabs folder

Use the Text in Unity’s UGUI component to build the “+” in the center of the screen and the object name display text

Game application function implementation

Realize a mobile camera function
public class MirMove : MonoBehaviour
{<!-- -->
    private GameObject m_Camera;
    public float Speed=30;

    voidAwake()
    {<!-- -->
       //Find the main camera
        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");
    }
    //Camera movement
    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);
    }
 }
Implement the function of detecting objects with screen rays and displaying the name of the object in the text

//Ray detection
 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 = "Already targeted:" + modle.name;
        }
        if (modle == null)
        {<!-- -->
            Txt_ModleName.text = "";
        }
        return modle == null?null: modle;
    }
Realize the function of destroying objects after left-clicking the mouse
//Click the mouse to destroy the object
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));
            }
        }
    }

Implementing a function of automatically regenerating an object after it is destroyed

//The coroutine implements a countdown of two seconds to regenerate and destroy objects.
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;
        }
    }

Update every frame

 void Update()
    {<!-- -->
        MirMoveto();
        RayPut();
        isMouseDown();
    }
All code
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;

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

    void Start()
    {
        //StartCoroutine(Countdown(2)); //Start a 2-second countdown coroutine
    }

    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 = "Already targeted:" + 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();
    }
}