Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pravusjif · Apr 17, 2013 at 08:03 PM · movementtouchtouch controlstime.deltatimetransform.translate

different devices -> Different touch speed

Hello,

I'm developing a vertical scrolling shooter for Android on Unity 3.x. I'm facing an issue with the touch movement speed of my main character, the touch speed is different on the different devices i try it on.

Here is my working code for the touch movementes (it WORKS, but at DIFFERENT SPEED on DIFFERENT DEVICES):

 void Update () {
         //**********************************************************************************
         //||||||||||||||||||||||||||||| MOVEMENT |||||||||||||||||||||||||||||||||||||||||||
         //**********************************************************************************
         if (Input.touchCount > 0) // TOUCHSCREEN CONTROLS
         {
                 // Get movement of the finger since last frame                         
                 touchDeltaPosition = Input.GetTouch(0).deltaPosition;                  
                 touchDeltaPosition.x *= touchSpeed;
                 touchDeltaPosition.y *= touchSpeed;
  
                 // Move object according to new positions
                 transform.Translate(touchDeltaPosition);
                 pos = transform.position;                      
                 transform.position = pos;
         }    
         //**********************************************************************************
         //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
         //**********************************************************************************
 }

You can check it better at http://pastebin.com/zJC7gX4J .

I'm convinced i have to use the Time.deltaTime somewhere here but i tried several options like: transform.Translate(touchDeltaPosition * Time.deltaTime);

or

transform.position = pos * Time.deltaTime;

or (already looking for ANY kind of solution)

transform.Translate(touchDeltaPosition * Input.GetTouch(0).deltaTime);

or

transform.position = pos * touchDeltaTime;

And none of them worked for me.

Any ideas are appreciated,

Thanks!

Comment
Add comment · Show 1
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 AkooleGame · May 10, 2015 at 09:25 AM 0
Share

found any solution?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Apr 17, 2013 at 10:02 PM

Presumably the devices that you are testing on have different pixel resolutions or DPI. So, the touch position moves more pixels on one device than the other, so your deltaPosition is different. You might want to do some maths that involves a percentage of the screen height and width. That way, if the player moves a finger say 25% of the screen width you don't care if that's 400 pixels or 64 pixels.

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 pravusjif · Apr 18, 2013 at 02:51 PM 0
Share

O$$anonymous$$, thanks for the answer, it seems like a nice idea so i gave it a try, but it's not working, the problem is still there(the movement speed of the object is different in different devices - i've tested on a motorola milestone 3, motorola smart plus and samsung galaxy S2).

Here is the code i used, maybe you can spot some error in my logic:

void Update() { if (Input.touchCount > 0) // TOUCHSCREEN CONTROLS { // Get movement of the finger since last frame
touchDeltaPosition = Input.GetTouch(0).deltaPosition;

     // Getting the percentage of the screen that it moved (result is in decimal format like 0.3 for 30%)
     touchDeltaPositionWindowPercentage.x = (touchDeltaPosition.x * 100 / globalValues.windowWidth) / 100;
     touchDeltaPositionWindowPercentage.y = (touchDeltaPosition.y * 100 / globalValues.windowHeight) / 100;
         
     // Changing the screen percentage to real value
     touchDeltaPosition.x = globalValues.windowWidth * touchDeltaPositionWindowPercentage.x * touchSpeed;
     touchDeltaPosition.y = globalValues.windowHeight * touchDeltaPositionWindowPercentage.y * touchSpeed;

     // $$anonymous$$ove object according to new positions
     transform.Translate(touchDeltaPosition * Time.deltaTime);
     pos = transform.position;            
     transform.position = pos;
 }

}

you can see it better at: http://pastebin.com/uZ9AZAD3

avatar image
0

Answer by SolarFly · Jun 23, 2014 at 10:35 PM

Touch.deltaPosition is just hardware delta. Touchscreen has query frequency. Sum of delta positions on update is a half of delta between positions of TouchPhase.Began and TouchPhase.End (on my Nexus 5). Just save start and end positions and calculate your own delta in current touch position.

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

Answer by jamius19 · Nov 12, 2015 at 04:27 PM

Use this script! :)

It works almost perfectly across devices! :)

       Vector2 worldStartPoint;
     Camera cam;
     public float speed;
 
 
     void Awake ()
     {
         cam = Camera.main;
     }
 
     void Update ()
     {
         
         // only work with one touch
         if (Input.touchCount == 1) {
             Touch currentTouch = Input.GetTouch (0);
             
             if (currentTouch.phase == TouchPhase.Began) {
                 worldStartPoint = cam.ScreenToWorldPoint (currentTouch.position);
             } else if (currentTouch.phase == TouchPhase.Moved) {
                 Vector2 temp = cam.ScreenToWorldPoint (currentTouch.position);
                 Vector2 worldDelta = temp - worldStartPoint;
                 //print (worldDelta);
                 cam.transform.Translate (-worldDelta.x * Time.deltaTime * speed, -worldDelta.y * Time.deltaTime * speed, 0);
             }
         }
     }
Comment
Add comment · 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

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

15 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

Related Questions

How do I move an object with my finger?[C#] 1 Answer

Touch buttons for step movememt 3 Answers

Trying to get the player to face the direction of a touch 1 Answer

How could I implement diagonal movement in this code? 0 Answers

Paddle script for Pong-based video game for Android 0 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