Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Summon a Prefab

Discussion in 'Scripting' started by TechnoObi, Nov 12, 2014.

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Hello,

    how can I summon a prefab, at the position, where I tap on my Smartphone?
     
  2. MattB79

    MattB79

    Joined:
    Sep 12, 2014
    Posts:
    20
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    I love that you said "summon a prefab".

    Just brilliant.

    And yeah, MattB79 answer works.
     
  4. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    Summon? Lol xD, I love it.

    Just instantiate it where your finger touches.
     
  5. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    +1

    I think the method should be renamed.
     
  6. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    687
    Invoke the words of power whilst gesturing intensely, and thy prefab shall be summoned.
     
    joni-giuro likes this.
  7. joni-giuro

    joni-giuro

    Joined:
    Nov 21, 2013
    Posts:
    435
    Expecto Prefabbus!

    On a serious note. You need to cast a ray from the camera position in the direction of the tap. When the ray hits the ground instantiate your prefab

    Something like (not tested):

    Code (CSharp):
    1. private RaycastHit hit;  
    2.    
    3. void Update () {
    4.          Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
    5.              if (Physics.Raycast(ray, out hit))
    6.                  if(hit.transform.name == "Floor")
    7.                     Instantiate(prefab, hit.point,  as Transform;
    8.        
    9.    
    10.      }
    11. }
     
    Last edited: Nov 13, 2014