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

Spawn Prefab on Android/IOS

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

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Hello, i want to spawn an Prefab, at the position, where I tab on my Android / IOS device. I have this Code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnIt : MonoBehaviour
    6. {
    7.     public int distance = 50;
    8.    
    9.     void Start()
    10.     {
    11.  
    12.  
    13.     }
    14.     public GameObject Object;
    15.     // Update is called once per frame
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.touchCount == 1)
    21.         {
    22.             Touch touch = Input.GetTouch(0);
    23.  
    24.             Ray ray = Camera.main.ScreenPointToRay(touch.position);
    25.  
    26.             RaycastHit hit;
    27.  
    28.             if (Physics.Raycast(ray, out hit, distance))
    29.             {
    30.                 Vector3 spawn = new Vector3
    31.                 (
    32.                     touch.deltaPosition.y,
    33.                     touch.deltaPosition.x,
    34.                     0
    35.  
    36.                 );
    37.  
    38.                 Instantiate(Object, spawn, Quaternion.identity);
    39.  
    40.             }
    41.  
    42.         }
    43.  
    44.  
    45.  
    46.     }
    47. }
    48.  
    it spawn the GameObject when I tab, but not at the position where I tab. Is there anything wrong? And is it possible to do the "touch.deltaPosition" with an Vector 3 and not the Vector2?
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    You're using touch.deltaPosition which is the change in position between the last frame and the current frame.

    Use touch.position instead.
     
  3. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Ok, thanks. Do you know how I can spawn it, at the position where the ray hits my plane?