Spawnowanie 2D po kliknięciu myszą

Jak dodawać prefaby do sceny w miejscu kliknięcia myszy w Unity 2D?

    [SerializeField] GameObject prefab;
    Camera cam;
    void Start()
    {
        cam = Camera.main;
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
            mousePos.z = 0f;
            Instantiate(prefab, mousePos, Quaternion.identity);
        }
    }
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Scroll to Top