Unity: come generare oggetti con un clic del mouse

In questo tutorial, mostrerò come generare un oggetto con un clic del mouse in Unity.

Lo script gestirà la generazione dei Prefabbricati sulla posizione del mouse insieme al loro allineamento alla normale della superficie.

Unity 3D genera gli oggetti prefabbricati con un clic del mouse

Passi

  • Crea un nuovo script, chiamalo SC_ClickSpawner e incolla al suo interno il codice seguente:

SC_ClickSpawner.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SC_ClickSpawner : MonoBehaviour
{
    public GameObject[] prefabs; //Prefabs to spawn

    Camera c;
    int selectedPrefab = 0;
    int rayDistance = 300;

    // Start is called before the first frame update
    void Start()
    {
        c = GetComponent<Camera>();
        if(prefabs.Length == 0)
        {
            Debug.LogError("You haven't assigned any Prefabs to spawn");
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedPrefab++;
            if(selectedPrefab >= prefabs.Length)
            {
                selectedPrefab = 0;
            }
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            selectedPrefab--;
            if (selectedPrefab < 0)
            {
                selectedPrefab = prefabs.Length - 1;
            }
        }

        if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftShift))
        {
            //Remove spawned prefab when holding left shift and left clicking
            Transform selectedTransform = GetObjectOnClick();
            if (selectedTransform)
            {
                Destroy(selectedTransform.gameObject);
            }
        }
        else if (Input.GetMouseButtonDown(0))
        {
            //On left click spawn selected prefab and align its rotation to a surface normal
            Vector3[] spawnData = GetClickPositionAndNormal();
            if(spawnData[0] != Vector3.zero)
            {
                GameObject go = Instantiate(prefabs[selectedPrefab], spawnData[0], Quaternion.FromToRotation(prefabs[selectedPrefab].transform.up, spawnData[1]));
                go.name += " _instantiated";
            }
        }
    }

    Vector3[] GetClickPositionAndNormal()
    {
        Vector3[] returnData = new Vector3[] { Vector3.zero, Vector3.zero }; //0 = spawn poisiton, 1 = surface normal
        Ray ray = c.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray, out hit, rayDistance))
        {
            returnData[0] = hit.point;
            returnData[1] = hit.normal;
        }

        return returnData;
    }

    Transform GetObjectOnClick()
    {
        Ray ray = c.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray, out hit, rayDistance))
        {
            Transform root = hit.transform.root;
            if (root.name.EndsWith("_instantiated"))
            {
                return root;
            }
        }

        return null;
    }

    void OnGUI()
    {
        if(prefabs.Length > 0 && selectedPrefab >= 0 && selectedPrefab < prefabs.Length)
        {
            string prefabName = prefabs[selectedPrefab].name;
            GUI.color = new Color(0, 0, 0, 0.84f);
            GUI.Label(new Rect(5 + 1, 5 + 1, 200, 25), prefabName);
            GUI.color = Color.green;
            GUI.Label(new Rect(5, 5, 200, 25), prefabName);
        }  
    }
}
  • Allega lo script SC_ClickSpawner alla fotocamera principale
  • Assegna i Prefabs che intendi generare all'array Prefabs in SC_ClickSpawner

  • Nel mio caso, utilizzerò forme semplici:

Suggerimento: per evitare che i prefabbricati generati siano "buried" in superficie, assicurati che la loro trasformazione radice (o il perno, se si tratta di un singolo oggetto) sia in fondo.

  • Sposta la fotocamera principale davanti a una superficie

Ora quando premi Gioca dovresti essere in grado di generare un oggetto facendo clic con il pulsante sinistro del mouse sulla superficie e rimuovere gli oggetti tenendo premuto Maiusc sinistro + clic sinistro.

Premendo '1' verrà selezionato il prefabbricato successivo nell'elenco e premendo '2' verrà selezionato il precedente.

Articoli suggeriti
Script per creare un interruttore della luce in Unity
Come impostare il controller joystick per il movimento in Unity
Tutorial sull'attacco corpo a corpo 2D per Unity
Script di tiro con pistola basato su Raycast e proiettili per Unity
Selezione dell'unità in stile RTS per Unity
Come utilizzare il controller Xbox in Unity
Script per l'aspetto del mouse per Unity