Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 02, 2019 at 08:45 PM by Jaimieself for the following reason:

Restructuring my code solved the problem and it is no longer an issue.

avatar image
0
Question by Jaimieself · Jan 02, 2019 at 06:53 PM · touchtouch controlsvector2touchscreentouchphase

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JxWolfe · Jan 02, 2019 at 07:16 PM 0
Share

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...

avatar image Jaimieself JxWolfe · Jan 02, 2019 at 07:41 PM 0
Share

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.

2 Replies

  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jaimieself · Jan 02, 2019 at 08:29 PM 0
Share

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.

avatar image
0

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,

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JxWolfe · Jan 02, 2019 at 07:20 PM 0
Share

lol he posted while I typed... same basic concept

avatar image Jaimieself · Jan 02, 2019 at 08:34 PM 0
Share

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.

avatar image Jaimieself · Jan 02, 2019 at 08:44 PM 0
Share

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.

avatar image JxWolfe · Jan 02, 2019 at 09:04 PM 0
Share

Well, I'm glad to hear that you've fixed your problem!

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges