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 /
avatar image
0
Question by UnityMadDev · Jun 06, 2016 at 02:21 PM · androidcontrollerdelta position

How to use touch.deltaPosition properly?

I use following code for measure touch delata position:

 private Touch firstDetectedTouch;    
 public Vector3 dragDistance;
 
 void Update() {
         if (Input.touchCount > 0) {
 
             firstDetectedTouch = Input.GetTouch(0);
 
             if (Input.GetTouch(0).phase == TouchPhase.Moved) {
 
                 dragDistance = firstDetectedTouch.deltaPosition;
             }
         }
 }

Then I sent dragDistance to my Player script and use it to move character as follow:

 body2D.AddForce(speed * touchPhase.dragDistance * Time.deltaTime, ForceMode2D.Impulse);

On some devices(e.g Samsung Galaxy s3) it works as I expect. I can smooth control my character. But lately I had oportunity to test it on another device like Sony Xperia M2 and the result is terrible. To be honest it's unplayable. Sometimes character moves very slow other times it's rush as crazy. I don't know it's because I use this function wrong or missunderstand concept of delataPosition?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by imaxus · Jun 06, 2016 at 02:26 PM

Hi,

with every physics activity and movement it's better to use FixedUpdate that Update. Because Update is called every frame, so on phones with good graphic chip it will be fast and on the others user experience will be poor. FixedUpdate is called every time the phisics is calculated (ex. 0.2s)

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 UnityMadDev · Jun 06, 2016 at 02:41 PM 0
Share

So, should I measure deltaPosition in FixedUpdate() also? This line of code:

 body2D.AddForce(speed * touchPhase.dragDistance * Time.deltaTime, Force$$anonymous$$ode2D.Impulse);

is in Fixedpdate() in another script.

avatar image imaxus UnityMadDev · Jun 06, 2016 at 06:07 PM 0
Share

Edit : sorry i read this in bus, need to think a while on it.

avatar image imaxus UnityMadDev · Jun 06, 2016 at 06:26 PM 0
Share

If i were u i will try put everything in Fixed update, in first step in the same file to check if it work or not. If yes, then separate code to your two different scripts.

avatar image UnityMadDev imaxus · Jun 06, 2016 at 09:27 PM 1
Share

I tried both and don't work. But ins$$anonymous$$d of Touch.deltaPosition I create own method and it works perfect so far.

 currentPosition.x = Input.GetTouch(0).position.x - pastPosition.x;
 currentPosition.y = Input.GetTouch(0).position.y - pastPosition.y;
 pastPosition = Input.GetTouch(0).position;

Thanks for your time and help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Android controller 0 Answers

Table Tennis game using PC as screen and smartphone as Controller 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do you get input from a moga controller? 1 Answer

Unity android bluetooth controller disconnect doesn't make array empty 1 Answer


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