How to instantiate object at touch position?

Hello, first, I wanna say I tried a lot before asking, I checked the scripting references, I checked another questions, and all that helped, but I can’t figure out how to do it.

I’m trying to make a “Fruit Ninja” like effect. When the player drag the finger over the screen, a trail will appear and follow the movement.

My script is ALMOST working… Every time I touch the screen, my prefab empty gameObject called “Trail” instantiate, and I can move it around and leave a trail with a Trail Renderer. When the finger leaves, I destroy it from the scene.

The problem is. It’s NOT instantiating correctly. First it would instantiate at the center of screen, even if I touched the corner and started moving my finger from there. I changed the script a little, and now is instantiating from the corner (0,0).

My situation is: Inside the prefab I have the movement script. In the scene I have a script calling the prefab when I touch the screen. A total of 2 scripts. (1 for moving around, which is inside the prefab, and 1 for instantiating to the scene, which is not working right).

What I want is to instantiate exactly where the finger touches the screen.

I tried a Raycast, tried a ScreenToWorldPoint, a WorldToScreenPoint… I don’t know exactly what to do.

Here goes the Movement Script:

using UnityEngine;
using System.Collections;

public class Finger_Move : MonoBehaviour
{

    float swipeSpeed = 0.1F;
    public static float inputX;
    public static float inputY;

    void Update ()
    {
       if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
         Destroy (gameObject);
       }

    }

    void FixedUpdate ()
    {
       if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
         Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
         inputX = touchDeltaPosition.x * swipeSpeed;
         inputY = touchDeltaPosition.y * swipeSpeed;
         transform.Translate (inputX, inputY, 0);


         //Debug.Log ("X, Y: " + touchDeltaPosition.x + ", " + touchDeltaPosition.y);


       }
    }
}

And now the Instantiate Script (which I think is wrong):

using UnityEngine;
using System.Collections;

public class Create_Trail : MonoBehaviour {

    // Use this for initialization
    public Transform trail;
    public GameObject clone;
    void Start () {


    }

    // Update is called once per frame
    public void Update () {
       if(Input.touchCount > 0 && Input.GetTouch(0).phase==TouchPhase.Began){
       Vector3 v = new Vector3(Finger_Move.inputX,
                                Finger_Move.inputY,
                                      5);
        Vector3 ray = Camera.main.ScreenToWorldPoint(v);
        clone = Instantiate(trail, ray, transform.rotation) as GameObject;  



       }

    }
}

Can anyone help me? Thanks!

Note: You should only really use FixedUpdate() for physics.

Firstly, I’d just experiment with a sphere or cube. Keep it simple, then when the basics are working, expand on it.

ScreenToWorldPoint sounds correct from what you describe.

Remember that the z returned from ScreenToWorldPoint is in units away from the camera. Depending on your camera, 5 might appear to be quite far away, and if you’re not using an orthographic camera, it could appear to be quite far into the screen.

Well, I fixed.
I started over, with a sphere, and it worked. I changed the instantiate script to this:

using UnityEngine;
using System.Collections;

public class Create_Trail : MonoBehaviour
{

	// Use this for initialization
	public Transform trail;
	public GameObject clone;
	
	void Start ()
	{
		
	
	}
	
	// Update is called once per frame
	public void FixedUpdate ()
	{
		if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
			Vector3 fingerPos = Input.GetTouch (0).position;
			fingerPos.z = 5;
			Vector3 objPos = Camera.main.ScreenToWorldPoint (fingerPos);
			clone = Instantiate (trail, objPos, Quaternion.identity) as GameObject;	
			
		}
	}
}

Thank you!

In last version of unity you can use Trail Renderer for this