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 hd27 · Dec 04, 2012 at 10:30 AM · touchfollow

An Object Following Touch Position Problem

I know this is a common question but I searched and couldnt find an answer to my specific problem. I have an Object that Follows the touch position, my problem is that when I touch the screen with another finger while dragging the object the object tries to follow the new touch poistion and gets positiond between the two fingers instead of following the position of the first finger. this is my code for following the touch position:

 function FixedUpdate () {
     transform.position.z = 0;
     if(Input.touchCount > 0){
         if(Input.GetTouch(0).phase == TouchPhase.Began){
             if(Input.touchCount == 1){
                 transform.position = cam.ScreenToWorldPoint(Input.GetTouch(0).position);
                 transform.position.z = 0;
             }
         }
     }
 }


Maybe I should mention that this object is not visible and theres another object that follows it using a joint.

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 Fattie · Dec 04, 2012 at 02:28 PM 1
Share

it's possible this very long post could be relevant to you, pls vote it up if useful! :)

http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by azmat786n · Dec 04, 2012 at 05:19 PM

 //select your camera here
 var mainCam:Camera;
 //make screen into ray point
 var touchPos: Ray = mainCam.ScreenPointToRay(Input.mousePosition);
 
 function Update() {
     if(Input.touchCount>0) {
         transform.Translate(touchPos.origin.x * Time.deltaTime, touchPos.y * Time.deltaTime, 0);  
     }
 }
 

i hope this is helpful.. :)

Comment
Add comment · Show 3 · 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 hd27 · Dec 04, 2012 at 10:06 PM 0
Share

Should I switch Input.mousePosition with Input.GetTouch(0).position?

avatar image MibZ · Dec 04, 2012 at 10:16 PM 0
Share

Inside the Editor you can test Touch positioning with mouse positioning, but when testing it on an actual touchscreen device there will never be mouse input whatsoever, so yes.

avatar image azmat786n · Dec 05, 2012 at 12:02 PM 0
Share

you can use mouse position on touch screen but you have to make camera screen point into ray like above script.. it tested on my device also feel easy to use it. :)

avatar image
0

Answer by Toastfighter · Apr 11, 2014 at 10:18 PM

I have the same problem – any suggestions? The touch position is not on the right place. And for the code snippet from azmat786n i’ve got the following error message: 'y' is not a member of 'UnityEngine.Ray'. Have anyone an idea how can i fix this?

My code:

using UnityEngine; using System.Collections;

public class PlayerTouch : MonoBehaviour {

 public float noSpeed = 0;

 void Update ()
 {


     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
         Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
         transform.Translate(-touchDeltaPosition.x * noSpeed, touchDeltaPosition.y, 0);
     }
 }

}

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 Hamza Hasan · Nov 21, 2015 at 06:30 PM 0
Share

try touchPos.origin.y

avatar image
0

Answer by Vunpac · Nov 21, 2015 at 08:37 PM

I had this problem I solved it using the pointerID

 int numberOfTouches = Input.touchCount;
 if (numberOfTouches == 1)
 {
     touchID = Input.GetTouch(0).fingerId;
 }
 int index = 0;
 for (int i = 0; i < nbTouches; i++)
 {
     if (Input.GetTouch(i).fingerId == touchID)
     index = i;
 }
 transform.position = cam.ScreenToWorldPoint(Input.GetTouch(index).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

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

14 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

Related Questions

Can you use a Vectrosity points array with iTween? 0 Answers

how make this script follow a target. 1 Answer

How to make the object player followsmooth2D the mouse or the touch? 3 Answers

Player not following touch after camera is rotated? 0 Answers

How to make 2 or more Objects to follow 2 or more fingers 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