- Home /
Movement along with touch of finger
I have script which I use to move my character along the Y axis with the touch of finger. The problem is I don't want a secondary effect that comes with it.
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
if((fp.y - myPos.y) < 1)
{
myPos = transform.position;
Touch touch = Input.GetTouch(0);
fp = Camera.main.ScreenToWorldPoint(new Vector2(touch.position.x, touch.position.y));
myPos.y = fp.y;
transform.position = Vector3.Slerp(transform.position,myPos,Time.deltaTime*myvelocity*sFac );
}
//transform.position = new Vector3(myPos.x,
// Mathf.SmoothStep(myPos.y, fp.y, Time.deltaTime*4),0);
}
Alright so the problem is that when I touch anywhere on the screen it instantly transfers player at that Y point. Which is kind of not what I want, I want user to at least click near the Y point of player (not on the player but anywhere on screen) and drag the finger up-down to move the player.
I tried putting up a condition before the Slerp if((fp.y - myPos.y)<2) { //slerp } else { //nothing }
but obviously it doesn't work, someone with more experience on Touch inputs might be able to clear this up. Thanks guys.
The touch and position updates at same time. I tried putting a Debug.Log(fp.y + lp.y) and they always returned same value even when I put it just below the nested if.
If this couldn't be done, probably someone can make a new script for the same.. ?
Could you be more specific about what you are looking for? It's hard for me to tell what you want because to me,
I want user to at least click near the Y point of player (not on the player but anywhere on screen)
sounds like you are saying you want to verify the click is near the player but can also be anywhere on the screen. Which seems to be contradictory.
$$anonymous$$hh, by "anywhere on the screen" I mean the X point doesn't matter, suppose if X = 3, and Y is 9. The player moves to Y->9. And even if X was 8 the player would still have only moved to Y->9.
What I would want from it is that it don't snap long distances in Y points. Like jump from the bottom of the screen to top.
Answer by Andres-Fernandez · Jul 24, 2014 at 07:07 AM
The problem might be that you are setting a lerp position in the update function. You should be using a coroutine (or any other equivalent) instead. Whenever you detect the touch, get the position and start the coroutine that lerps your transform.position (you are doing that fine, just move it to a corountine). You just have to remember to update the target position and the time of the lerp within the coroutine.
Yes, FixedUpdate. A Co-routine as such IEnumerator ? The I'll have to call the Coroutine in place of the transform.position. Well Okay I'll try that.
Nope, doesn't helps. Almost identical to before except probably a bit smooth haha~
I don't know how your coroutine code is, but you should only call the coroutine when you detect the touchphase.began, and then check for the position of the finger within the loop. What usually works for me is (pseudocode):
Inside Update:
If (touchphase.began && fingerId == null) {
call coroutine;
storedFinger = fingerId;
targetPosition = touch.position;
}
if (touchphase.ended && storedFinger == fingerId) { storedFinger = null;
StopCoroutine();
}
Coroutine:
timeToTargetPosition = distance / speed;
time = 0;
while (time < 1) {
if (touch[storedFinger].position != targetPosition) {
targetPosition = touch[storedFinger].position;
timeToTargetPosition = distance / speed;
time = 0;
}
time += Time.deltaTime / timeToTargetPosition;
transform.position = Lerp();
yield return null;
}
The coroutine is a simple coroutine to make an object chase another object. You can figure out the code.
I use storedFinger to store the fingerId of the touch I use to make the object move. That way, I know which finger is moving the object. It is important to store the fingerId ins$$anonymous$$d of the touch id because the id of the touch may vary depending on the number and order of the touches on the device (even if you didn't change the finger, its touch id may have changed between frames because of the rest of the fingers touching the device). That fingerId can also replace some other boolean to check if you are dragging a finger. Just use a value like -1 to represent a null value. So whenever the fingerId equals -1 it means you are not dragging the finger on screen, and if it equals 0 or bigger it means you are dragging a finger on screen.
TargetPosition is the value of the position you want your objective to move towards (if you are not moving it directly with the finger position, but with a stablished speed ins$$anonymous$$d). You will use it in the lerp function, so it will look something like:
transform.position = Vector3.Lerp(originPosition, targetPosition, time);
With time being a value between 0 and 1 obtained as I posted above.
The check inside the if is used to see if your target position has changed (you have moved the finger, therefore you want the object to go towards the new position of the finger). So if the position of the touch which contains your fingerId isn't the same as the your targetPosition (the finger has moved), you need to update the target position to the new position of the finger. You also need to change the time and the origin position, since you want to maintain the same speed all the time.
(Again, sorry for the pseudocode, I'll try to post some real code.)
FINALLY. Got it working, well not "as is" but a little modification. Thanks!
Your answer
Follow this Question
Related Questions
Move an object to Input.Touch location 0 Answers
Rotate player to LookAt a touch position 2 Answers
Odd touch input problem 0 Answers
Input.GetTouch(0) doesn't work 2 Answers
Drag-and-drop dot on mobile 2D 0 Answers