- Home /
Restructuring my code solved the problem and it is no longer an issue.
Reset touch Vector after movement
Sorry in advance, I'm not sure how best to describe the problem.
I have a simple game where the character moves between 3 lanes, left, middle and right. On the keyboard it works perfectly using this sort of thing:-
.
if ((Input.GetKeyDown(KeyCode.A)) && (positionNumber == 1)) { positionNumber--; StartMove(transform, Quaternion.Euler(0,0,0), new Vector2(-2, transform.position.y), 0.1f); }
.
But when I test it on a touch screen it works horribly because when i swipe, it still remembers the coordinates of the last time i touched so the Vector2 ("newTouch.x - touchOrigin.x") gives an incorrect result which is actually based on the last time i touched the screen not the current touch.
.
So when i do this, it always moves left:
if (swipe >= 100) { //move right }
.
What i want is to be able to only move one lane at a time so to move another lane in either direction you have to remove you finger and start a new swipe.
I hope i have explained this well enough
Ok, I understand what you are asking. $$anonymous$$y question is are you making your own touch input system? I personally use one of the many free versions on the asset store and alter them to fit my own uses. -just a thought...
I'm not using one off the asset store but i think its fairly standard. I use the phases Begin, $$anonymous$$oved to see if you swiped left or right and then execute the appropriate code for moving. I might have a look on the asset store anyway as it might help, thanks.
Answer by Snipe76 · Jan 02, 2019 at 07:16 PM
You can try and check in which phase the touch is in. try using:
if (Input.touches[0].phase == TouchPhase.Ended)
{
}
to see if the finger was lifted from the screen, and inside that IF resetting the movement vector.
I have tried resetting everything in TouchPhase.Ended but somehow it has no effect at all. I have also tried putting the actual movement part of the script in the Ended phase which worked, however having to remove your finger from the screen before the character moved made it feel very sluggish and reaction time does play a part in the game.
Answer by JxWolfe · Jan 02, 2019 at 07:19 PM
Ok, here's my thoughts on the matter.
What if you set touchOrigin.x to null once you release the device, then when you touch it again, you check weither or not touchOrigin.x == null, and if it does, then set touchOrigin.x to where you are currently touching.
Hope that helps Jaimieself,
I have tried setting the touchOrigin = new Vector2(0,0); to see if that resets it but still doesn't work. I have a feeling I have structured my code incorrectly so i'm going through it now.
O$$anonymous$$ I fixed it. It was because i had structured the code incorrectly and It seems i was basically letting the code choose its next movement based on previous movements which is just madness. So what i have done is nested the "if" statements for left movement and the "if" statements for the right movement inside there own "if" statements rather than have them all individually sitting in the update function. I don't know if this makes sense but it is working perfectly.
Follow this Question
Related Questions
Unity Touch Help! 0 Answers
check touch position 2 Answers
A touch'es state is always Began and the position doesn't change 1 Answer
How to show and hide an image by swiping 0 Answers
define touch area 2 Answers