• 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 JayFitz91 · Jun 23, 2014 at 08:25 PM · touchfollowaccurate

Unity touch controls, accurate finger following

Hi, I'm recently new to Unity and touch controls, I have been following the Unity scripting API's and found this piece of code:

 // Moves the object according to finger movement on the screen
 
 var speed : float = 0.1f;
 
 function Update()
 {
     if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
     {
         
         //Get movement of the finger since the last frame
         var touchDeltaPosition : Vector2 = Input.GetTouch(0).deltaPosition;
         
         // Move object across the X and Y plane
         transform.Translate (-touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
     }
 }

This manages what I want to achieve, the problem is, the object doesn't accurately follow my finger, sometimes it will be too fast or sometimes it will be too slow, I would like it to stay underneath my finger at all times without straying off. The speed variable doesn't seem to make much of a difference.

Here is a video to the problem I'm having, hopefully give people and idea of what's wrong: https://www.youtube.com/watch?v=9g_BHnaz7f4&feature=youtu.be

Is there a way I can accomplish this?

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

2 Replies

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

Answer by JayFitz91 · Jun 24, 2014 at 08:48 PM

 #pragma strict
 
 private var pos : Vector3;
 
 function Start () {
 
 }
 
 function Update()
 {
     
     pos = Camera.main.ScreenToWorldPoint (Vector3 (Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 5));
     
     transform.position = new Vector3(pos.x, pos.y, pos.z);
 }
     

Thanks Andres Fernandez, I was playing around for a while and managed to get it working with Camera.main.ScreenToPointWorld and Input.GetTouch(0)

As of now, I have it following my finger on the x and y axis with a fixed z position, should I improve upon this, I will update my answer.

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 Andres-Fernandez · Jun 25, 2014 at 06:29 AM 0
Share

Cool. Good job.

avatar image KasunL · Dec 09, 2015 at 02:35 AM 0
Share

Working! Thanks!

avatar image Ams0 · Aug 03, 2016 at 01:06 PM 0
Share

First off, your code crashes at the start. Since there is no touch right at the first update call, and GetTouch(0) returns OutOfBoundException. $$anonymous$$oving on, when inserting your code into an "if" statement to make sure the above exception doesnt get thrown, it still wont do what its suppose to do! I have absolutely zero clue to how Andres or $$anonymous$$asunL found your solution as "good job" or "working!". $$anonymous$$y clear frustration rises from misinformation. Please update if you have a correct solution or refrain from wasting others time. Thanks!

avatar image afors Ams0 · Aug 11, 2016 at 05:52 PM 0
Share

When inserting an "if" statement to avoid crash, this code behaves just like I want it to. Ams0 maybe you're missing something else?

avatar image Ams0 afors · Aug 14, 2016 at 08:41 AM 0
Share

could you please care to point it out "if" I am missing something? That infact could help (as opposed to just pointing it out)

avatar image
3

Answer by Andres-Fernandez · Jun 24, 2014 at 01:20 PM

The problem is that this code moves the object based on the movement of the finger, but it doesn't make the object follow the finger.

If you want the object to follow the finger you need to:

  1. Get the coordinates of the finger in world space (instead of screen, which is what the touch gives you). You can use ScreenToWorldPoint (or similar) and touch.position.

  2. Make the object translate to that position over time (Using something like SmoothDamp)

Try that and let us know.

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 JayFitz91 · Jun 24, 2014 at 08:48 PM 1
Share

Added an answer with a possible solution, thanks

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Game Object Follow finger 1 Answer

Need help with physics 1 Answer

An Object Following Touch Position Problem 3 Answers

I want to draw a line(curved) as finger moves on screen after that move a instantited gameobject/rigidbody following that path. 2D 0 Answers

How to make the object player followsmooth2D the mouse or the touch? 3 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