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
-1
Question by coolbird22 · May 06, 2014 at 04:41 PM · touchdeltatime

Changing the speed variable does not change the speed of the motion of my touch based object.

No matter what the value of the speed variable is, my object travels at the same speed. This has been eating at my brains for a while now. Multiplying with only Time.deltaTime only too doesn't work. What am I doing wrong ?

     public float speed = 0.000000002F;
     void Update() {
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             transform.Translate(touchDeltaPosition.x * speed * Time.deltaTime, touchDeltaPosition.y * speed * Time.deltaTime, 0);
         }
 
Comment
Add comment · Show 5
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 xortrox · May 06, 2014 at 05:24 PM 0
Share

$$anonymous$$y first thought is that 0.000000002F is a really small number to use for movement, you should firstly try using a much larger number to begin with and rather scale downwards from that point.

Also:

I would assume that multiplying 0.000000002F by Time.deltaTime could result in a float that simply is zero if 0.000000002F isn't changed to zero in the first place.

An example deltaTime when running at 60 FPS would be 0.016 (16ms per frame) and multiplying 0.016 by your speed variable gives us 0.000000000032 Taken that float has a precision of 7 digits and 0.000000000032 has 12 digits I would assume that 0.000000000032 becomes zero.

However I'm not certain about all of this, I just did some quick research on floating point numbers, you could try using a double for speed and see where that takes you.

avatar image xortrox · May 06, 2014 at 05:26 PM 0
Share

Some info on float(single)/double etc

$$anonymous$$ore specifically just float

avatar image coolbird22 · May 06, 2014 at 05:31 PM 0
Share

For a small movement of touch drag on the screen, my object seems to be having a lot faster motion. For example, if I drag on the screen for 1cm, the object moves a lot more than 1cm. To $$anonymous$$imize this increment in the speed, I'm trying to multiple the movement by the speed float variable, but no amount of value seems to be affecting the speed as it seems to be the same.

avatar image Jeff-Kesselman · May 06, 2014 at 10:27 PM 0
Share

If you want it to be under your finger, don't multiply the mouse position by anything.

avatar image coolbird22 · May 07, 2014 at 03:05 AM 0
Share

Not multiplying it by anything is still faster than I want it to be.

2 Replies

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

Answer by Jeff-Kesselman · May 06, 2014 at 04:45 PM

There is no animation code here.

Instead you are instantaneously teleporting the object to a new location which lis the one currently under your finger.

That location is being slightly modified by speed but my guess is its being balanced by a very small number of ms.

If you aren't actually looking for animation, but just want it to stay under your finger, then DONT multiply the touch position by anything.

If you want it to slide slowly to your finger, you will have to implement animation code.

Comment
Add comment · Show 5 · 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 coolbird22 · May 06, 2014 at 04:51 PM 0
Share

Well that code makes the object travel on an X,Y axis, when a touch is dragged on the surface of the device. And it is not teleporting to any new location. So, if I touch the surface and drag upwards, my object will move up, and so on and so forth for any kind of path that I make with my finger, while pressing down on the screen.

avatar image Jeff-Kesselman · May 06, 2014 at 05:10 PM 0
Share

NO, sorry, you are wrong.

What that code does is teleport the object to under your finger (approximately), as you move your finger, it teleports to the new finger location.

The "animation' is co$$anonymous$$g from you moving your hand.

avatar image coolbird22 · May 06, 2014 at 06:52 PM 0
Share

That is quite odd, since I have this exact code working as I described it to be working in Unity right now as I type this. I've been using it for the past 2 days now.

avatar image Jeff-Kesselman · May 06, 2014 at 10:23 PM 0
Share

Yes and it is doing exactly what you describe, which is what you don't want.

I've explained to you WHY its doing that.

If you want to argue rather then listen and learn, thats your issue.

//plonk

avatar image coolbird22 · May 07, 2014 at 02:52 AM 0
Share

Firstly, I DON'T want it to teleport to wherever my finger touches. Secondly, it is NOT teleporting to wherever my finger touches currently. So this is good. For a small distance that I touch-dragged on the screen, the amount the object moves is more than I want it to be, which is my sole concern here. I'm trying to multiply that with the speed variable but the speed doesn't change. So, I'd like to know why is this happening and what can I do to make the distance travelled by my object less than what it is travelling right now. I don't see what would make you assume that I'm arguing when I'm trying to elaborate my situation better so it is clearer for readers here to understand.

avatar image
0

Answer by AlucardJay · May 07, 2014 at 04:10 AM

Jeff is absolutely correct.

It seems you want to calculate a movement direction based on the touchDelta. This is done by normalizing the touchDelta to create a directional vector, then multiply that by speed and Time.deltaTime :

 public float speed = 2.0f;
 void Update() {
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
         Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
         Vector2 movePosition = touchDeltaPosition.normalized * speed * Time.deltaTime;
         transform.Translate(movePosition.x, movePosition.y, 0);
     }
 }


I don't have a touch device to test the above, but this script shows the theory :

 #pragma strict
 
 public var speed : float = 2.0;
 
 private var myTransform : Transform;
 
 private var lastPos : Vector2;
 private var currentPos : Vector2;
 
 
 function Start() 
 {
     myTransform = transform;
 }
 
 function Update() 
 {
     #if UNITY_EDITOR
         
         if ( Input.GetMouseButtonDown(0) )
         {
             OnTouchBegan( new Vector2( Input.mousePosition.x, Input.mousePosition.y ) );
         }
         else if ( Input.GetMouseButton(0) )
         {
             OnTouchMoved( new Vector2( Input.mousePosition.x, Input.mousePosition.y ) );
         }
         else if ( Input.GetMouseButtonUp(0) )
         {
             OnTouchEnded( new Vector2( Input.mousePosition.x, Input.mousePosition.y ) );
         }
             
     #else
         
         if ( Input.touchCount > 0 )
         {
             if ( Input.GetTouch(0).phase == TouchPhase.Began )
             {
                 OnTouchBegan( Input.GetTouch(0).position );
             }
             else if ( Input.GetTouch(0).phase == TouchPhase.Moved )
             {
                 OnTouchMoved( Input.GetTouch(0).position );
             }
             else if ( Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled )
             {
                 OnTouchEnded( Input.GetTouch(0).position );
             }
         }
         
     #endif
 }
 
 function OnTouchBegan( thePos : Vector2 ) 
 {
     lastPos = thePos;
     currentPos = thePos;
 }
 
 function OnTouchMoved( thePos : Vector2 )  
 {
     currentPos = thePos;
     
     var moveDirection : Vector2 = ( currentPos - lastPos ).normalized;
     
     var movePosition : Vector2 = moveDirection * speed * Time.deltaTime;
     
     myTransform.Translate( movePosition.x, movePosition.y, 0 );
     
     lastPos = thePos;
 }
 
 function OnTouchEnded( thePos : Vector2 )  
 {
     
 }
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

23 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

Related Questions

Touch.deltaTime returning a negative number on Android 0 Answers

What exactly is in Touch.deltaTime? 1 Answer

Touch Input in Editor | Microsoft Surface Pro 4 0 Answers

Shump Touch Control 1 Answer

How to make the back image appear before the front image where it is scratched? 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