J
J
Jotyu2021-04-11 13:11:53
Unity
Jotyu, 2021-04-11 13:11:53

How to spawn an object on click (2D)?

I did this:

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

public class Coin_spawn : MonoBehaviour
{
    private GameObject Coin;
    private Touch touch;
    private Vector2 touchPos;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            touchPos = Camera.main.ScreenToWorldPoint(touch.position);
            Instantiate(Coin, touchPos, Quaternion.identity);
        }
    }

How to get position on mouse click? In general, they usually do it through touch.position. But, as far as I know, this is for 3D objects.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question