Search Unity

instantiate at mouse pos

Discussion in 'Scripting' started by Truls_, Apr 16, 2017.

  1. Truls_

    Truls_

    Joined:
    Dec 4, 2016
    Posts:
    105
    I have looked all over the place for a simple answer but I can't find a simple yet working sollution. How do I instantiate an object at the mouse position? just as simple as that. where ever the mouse clicks, I want the object to appear.
     
  2. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    Break this down to make it easier to find what you want.

    How to get the position of the mouse on the screen?

    How to convert a position from screen space to world space?

    How to check if the mouse has been clicked?

    How to instantiate an object at a position in 3d space?
     
  3. Truls_

    Truls_

    Joined:
    Dec 4, 2016
    Posts:
    105
    it's a 2d game. a cookie clicker-like game. when you click -anywhere on the screen- I want an object to be instantiated at the exact position you clicked. how do I instantiate it at the position that you click?
     
  4. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    tiagomelobr and ovirta like this.
  5. Truls_

    Truls_

    Joined:
    Dec 4, 2016
    Posts:
    105
    this literally doesn't help anything. it might just be me who's bad at searching for stuff but I can't find any sollution. I have looked on google and youtube and I can't find anything. how hard can it be to instantiate an object at the mouse position in a 2d space? (apparently very hard for me)
     
  6. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    I'm sorry, I was trying to give you just enough so that you could find the answer yourself- it's a skill you'll really find useful in your future endeavors.

    There is actually an example of doing almost exactly what you want in the very first link of the Google search I gave you.

    It looks like this-
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour
    5. {
    6.     public GameObject particle;
    7.     void Update()
    8.     {
    9.         if (Input.GetButtonDown("Fire1"))
    10.         {
    11.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.             if (Physics.Raycast(ray))
    13.                 Instantiate(particle, transform.position, transform.rotation);
    14.         }
    15.     }
    16. }
    The Input.GetButtonDown line returns true only when the specified input is pressed. "Fire1" is the name of an input in the input manager.

    Camera.main.ScreenPointToRay uses the main camera to find a ray from the camera to where you clicked in world space.

    Raycast checks if the ray mentioned previously hit anything. Raycast can give you the point it hits as a raycasthit- see the api refereceived for details.

    Instantiate it sounds like you know already. It takes a reference to a gameObject, a vector3 position and a quaternion rotation.

    Since you are going 2D, you can strip out the z value of the vector3 position by setting it to zero.
     
    Risible_Kitty and DroidifyDevs like this.
  7. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    A trick I recently discovered in Instantiate is that you can spawn an object as a child of a parent transform, so it takes up to 4 references. https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
     
    LMan likes this.
  8. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
  9. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Its similar to what @LMan had posted, but his works for 3D and not 2D. It also requires 3D colliders to work.

    Code (CSharp):
    1. Vector2 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. Instantiate(myPrefab, worldPosition, Quaternion.identity);
    This is probably the simplest way to do it, you can put that code into an if statement or wherever you want to get it to instantiate.

    I'm not sure what you tried to search for, but my first hit on the first attempt to find a solution was the correct solution and i didn't even need to leave the google page. I searched for "Unity spawn object at mouse position"
     
    Last edited: Apr 16, 2017
  10. hexbyte

    hexbyte

    Joined:
    Aug 24, 2014
    Posts:
    4
    Sorry to add to such and old question - have my solution working perfectly using transform parent with instantiate except for one caveat - works 100% with no camera rotation but if my camera is rotated 25 degrees on X to see the player perfectly it doesn't work. -- any ideas to take into account camera rotation??


    CurrentPrefab= Instantiate(TrailPrefab, transform);



    Also I have tried searching for several search variations on google search for
    "Instantiate with mouse position and camera rotation"