• 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 /
This question was closed Feb 12, 2016 at 01:08 PM by Ali-hatem for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ali-hatem · Feb 06, 2016 at 11:20 AM · offsetfinger

how to offset finger touch ?

Hi : my object falow my finger very well but it teleport where ever i touch and i want it to move the object with offset distance between the object and the finger. and of course the offset distance should be variant if i lifted my finger and touched the screen in different position again. her is my code :

  void finger()
  {
      
   if(Input.touchCount > 0)
        {
    Vector3 pl = new Vector3(Input.GetTouch(0).position.x,halfH,10);
    transform.position = Camera.main.ScreenToWorldPoint (pl);
                         
        }
    
   }








Comment
Add comment · Show 2
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 hickna · Feb 06, 2016 at 05:05 PM 0
Share

@Ali haten Are you using collider? If yes... All screen has objects with colliders? In other words... Any place that you touch will generate some collision? The Y axis is static or you only want to change the X position like a pong game?

avatar image Ali-hatem hickna · Feb 09, 2016 at 06:05 PM 0
Share

there is no collision with finger it is strange !

1 Reply

  • Sort: 
avatar image
0

Answer by Ali-hatem · Feb 12, 2016 at 01:00 PM

finally problem solved ! by this code and moving the camera 3 points to the right so i don't get negative numbers when i touch the left half of the screen because if camera at zero position and the camera size is 5 then i will get -2.5 to the left and 2.5 to the right. and it's mater because of the offset calculation below and it's hard to explain every thing i had to add texts to the screen the touch position & the transform position & the offset distance to understand every thing but now any one can use this script

  if (Input.touchCount > 0) 
             {
                 // converting the touch position from ScreenSpace To WorldPoint
                 Vector3 tpos = Input.GetTouch (0).position;
                 Vector3 pos = Camera.main.ScreenToWorldPoint (tpos);

                  // i is a float variable 
                  i = pos.x; // geting only the X position of touch
 
                    // setting the offset distance only when touch began
                     if(Input.GetTouch(0).phase == TouchPhase.Began)
                     {
                         // offset is a float variable 
                         offset = transform.position.x - i;
                     }
     
                     if(Input.GetTouch(0).phase == TouchPhase.Moved)
                     {
                         transform.position = new Vector2(i+offset,-3);
                     }
     
                 }
             }
     

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

touch input error - finger position offset 2 Answers

How I make finger touch move texture offset 0 Answers

How do I offset the scaling center for a cube 1 Answer

gun sway, gun swing, gun offset 0 Answers

Look to the side-Scripting Problem 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges