Moving objects to point of touch on iPhone.

Hello. I’m hoping someone will point me in the right direction, even if this might be a dumb question…
I’m working on an iPhone AR app and I’m able to get an object to show up where I tap my finger. But I would like it to move rather than just show up! So I guess I should use Translate, not position, but I can’t get that to work with the ScreenToWorldPoint thing. Tried numerous different combinations of code.

Is this maybe much more complex than I think?

I’d be very grateful if somebody could help me out!

Iv’e read this post (among many others) but it doesn’t get me all the way there… Moving an Object to the position of touch - Questions & Answers - Unity Discussions

function Update () {

for(var touch : Touch in Input.touches) {
if(touch.phase == TouchPhase.Ended) {
    Debug.Log("Touched...");
	
	
	var curX = Input.GetTouch(0).position.x;
	var curY = Input.GetTouch(0).position.y;
	
	//transform.Translate = Camera.main.ScreenToWorldPoint(new Vector3 (curX, curY, 1000));
	transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (curX, curY, 1000));

} 
}
}

EDIT: I’ve now been trying to combine Vector3.MoveTowards or Vector3.Lerp with Camera.main.ScreenToWorldPoint. Can this be done?

You’re only checking when the user releases a finger (TouchPhase.Ended). To Translate effectively, start a coroutine (don’t try to run this code!):

if(touch.phase == TouchPhase.Ended) {

MoveObjectHereInXSeconds(Input.GetTouch(i).position, 1.2); //1.2 s for example
}
}
function MoveObjectHereInXSeconds(fingerPos:Vector2, time : float)
{
    //write your own function here, starting with while(theObjectIsNoteWhereIWantIt)
    //MoveIt
    //Don't forget to yield;
}

Ok well is it more of a glide motion you are looking for?
If so try this out.

var speeds : Int

for(var touch : Touch in Input.touches) 
{
    if(touch.phase == TouchPhase.Ended) 
    {
        Debug.Log("Touched...");
        var curX = Input.GetTouch(0).position.x;
        var curY = Input.GetTouch(0).position.y;
        Pos = Instantiate(curX, curY, 1000); 
        Pos.rigidbody.AddForce(tran­sform.forward * speeds);
    }
}

Try this

I am assuming you want it to move to the point you tap not just jump to the position. If thats the case you will have to set a variable to be your destination and then Translate over a set time. This is not tested code but can get you in the right direction. Another option is to use an code animation tool like iTween.

var destination : Vector3;
var speed : float = 10;
function Update() {
    if(touch.phase == TouchPhaseEnded)
    {
        var curX = Input.GetTouch(0).position.x;
        var curY = Input.GetTouch(0).position.y;
        destination = Camera.main.ScreenToWorldPoint(new Vector3 (curX, curY, 1000));
    }

    if(destination != transform.position)
    {
        transform.position = (transform.position - destination).normilize * speed;
    }
}

I will look into this, but I just want to check what you mean by “You’re only checking when the user releases a finger (TouchPhase.Ended)”.
What I want is to tap the screen, and then for an object to move to the location of the tap, is this not correct then? Just making sure you don’t think I mean dragging things :slight_smile:

Thank you for your time!