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!
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;
}
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