Move to tap position

Hi folks,

I am having a little trouble with moving a GameObject to a finger tap position, I dare say it is quite simple but I am very new to scripting (I’m using c# btw) and have experimented for so long that I have become a little lost… so any help would be muchly appreciated.

I have a simple scene with an uneven terrain (and a few objects) and I would like to be able to move a GameObject from it’s position to the position of the touch.
In my previous test I was able to instantiate a prefab at the touch position so I have used that script as my starting point, and I can get my GameObject to move but it moves in increments to a random position in the approximate direction of my tap… not exactly what I had in mind.

So this is my script thus far and it is attached to the GameObject I am trying to move.

using UnityEngine;
using System.Collections;

public class MoveToTap : MonoBehaviour {
	

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Began)
		{
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
			
			if(Physics.Raycast(ray,out hit))
				if(hit.collider != null)
			{		
			transform.Translate(hit.point);
			}
		}
	
	}
}

Just modify your if like this. but this is not multitouch solution.

		if(Input.touchCount > 0)

Thanks Lacost, and sorry for the slow reply (been away from the computer for a few)
I tried your suggestion but had no joy… I guess it could be a Unity Remote thing but my original code was closer to what I need. I would also prefer a Multitouch compatible solution… But thank you so very much for your help.

Just a question, is transform.Translate an incremental change or an absolute? I’m thinking that may be my problem… maybe?

Thanks again

Well I got it to work… I can now move a GameObject (with this C# script attached) to the touched point on any collider in my scene (phew and yay!) I think my main issue was not giving the GameObject a starting point in the transform equation… Vector3.Lerp was my hero in the end.
Just in case there are any other newbies out there having the same problem I thought I would post my solution (may not be ideal but it works thus far)

using UnityEngine;
using System.Collections;

public class MoveToTap : MonoBehaviour {
		

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
	if(Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Began)
		
		{
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
			
			if(Physics.Raycast(ray,out hit))
				if(hit.collider != null)
			{
			transform.position = Vector3.Lerp(transform.position,hit.point,Time.time);
			}
			
		}
	
	}
}

1513277--86032--$tumblr_lil27wefcP1qdshi4o1_400.gif

zeroiq50 you’re my hero…

But really, thank you! Had a little dance after finally getting my object to move this way.