Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 The 3D Ninja · Nov 12, 2010 at 04:56 AM · iostouchdragoffset

How to apply offset to touch position when dragging object

Hi,

I've been working on a script for dragging an object using touch position for iOS. I currently have a the object following along with a 1 finger touch as it moves across the screen. However, when the finger begins to move, the gameObject snaps it's center to the touch position. I know I need to apply an offset of the gameObject's transform.position from the touch position, but I'm not sure how to go about it. Can someone please help me to get on the right track. Here is a section of the script that handles the dragging. It's C#, which I'm not currently very good with.

Thanks!

// one finger drag gameObject movement if (iPhoneInput.touchCount == 1) {

         Ray ray = camera.ScreenPointToRay(Input.GetTouch(0).position);
         RaycastHit hit;

         iPhoneTouch f0 = iPhoneInput.GetTouch(0);
         if (Physics.Raycast (ray, out hit, 100)) {
             //Check to see if object hit is been set to moveable
             if(hit.collider.tag == "Moveable"){
                 if(f0.phase == iPhoneTouchPhase.Moved || f0.phase == iPhoneTouchPhase.Began) {

                 Vector3 touchPos = Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, Camera.main.farClipPlane));

                 //add the x and z position to new vector3
                 Vector3 newPos = transform.position;

                 newPos.x = touchPos.x;
                 newPos.z = touchPos.z;

                 transform.position = newPos;//Vector3 is a struct must be set from new Vector3
             }   
         }
     }       
 }

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

4 Replies

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

Answer by The 3D Ninja · Nov 15, 2010 at 01:01 AM

I was able to get everything working. Steven Fulfer (twitter @foofman) and John Lacoviello (twitter @mystaticself) sent me some code that solved the problem and I wanted to post their solutions here for others who find this question.

SOLUTION 1: Steven Fulfer

// one finger drag gameObject movement if (Input.touchCount == 1) { //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); RaycastHit hit;

         //check to see if item has been hit and set start and hit variables
         if (Physics.Raycast (ray, out hit, 100)) {
             //Check to see if object hit is been set to moveable
             if(hit.collider.tag == "Moveable"){
                 if (!startTouch) {
                     hitDistance = hit.distance;
                     //mouse
                     //startTouchPos =   Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, hitDistance)); //Camera.main.farClipPlane
                     //touch
                     startTouchPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, hitDistance)); //Camera.main.farClipPlane                        
                     startTouch = true;
                 }
             }
         }

         if (startTouch) {
             //mouse
             //Vector3 curntTouchPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, hitDistance));   
             //touch
             Vector3 curntTouchPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, hitDistance));   
             Vector3 newPos = transform.position;

             newPos.x = curntTouchPos.x;
             newPos.z = curntTouchPos.z;


             newPos.x = startItemPosition.x + (curntTouchPos.x - startTouchPos.x);
             newPos.z = startItemPosition.z + (curntTouchPos.z - startTouchPos.z);

             transform.position = newPos;//Vector3 is a struct must be set from new Vector3
         }      
     } else {
         //mouse button has been released
         startTouch = false;
         //needs to start where it left off
         startItemPosition = transform.position;
     } 

SOLUTION 2: John Lacoviello

// one finger drag gameObject movement if (iPhoneInput.touchCount == 1) {

         Touch f0 = Input.GetTouch(0);
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         RaycastHit hit;

         if (Physics.Raycast (ray, out hit, 100)) {
             //Check to see if object hit is been set to moveable
             if(hit.collider.tag == "Moveable"){
                 if (f0.phase == TouchPhase.Began) {
                     screenPos = Camera.main.WorldToScreenPoint(transform.position);
                     offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, screenPos.z));   
                 }else if(f0.phase == TouchPhase.Moved){
                     currentScreenPos = new Vector3(f0.position.x, f0.position.y, screenPos.z);
                     currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
                     transform.position = currentPos;
             }
         }   
     }       
 }

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 gabrielclapclap · Mar 09, 2019 at 01:00 PM 0
Share

I'm gonna fucking cry. Have been wrestling with this for days and this finally did it! Thank you so much!

avatar image
1

Answer by DukeNuke3m · Apr 03, 2018 at 08:46 AM

Well i might be late,but i have some solution,i was making a game and i had that offset,i did like this

void Update() {

   if (Input.GetMouseButtonDown (0)) {
                 mouseposition = Input.mousePosition;
                 Vector3 MousePositionToWorld = Camera.main.ScreenToWorldPoint (mouseposition);
                 offset = transform.position - MousePositionToWorld;
                 offset.z = 0;
             }
             if (Input.GetMouseButton (0)) {
                 mouseposition = Input.mousePosition;
                 mouseposition.z = 1;
 
                 transform.position = Camera.main.ScreenToWorldPoint (mouseposition) + offset;
             }
 }
 

Just add raycast to the collider of object you want to drag and i think youre good to go

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 Grumblekeen · Nov 12, 2010 at 02:34 PM

I don't have an easy way to test this, so you might have to tweak it slightly. I think the approach is correct, though I have trouble visualizing things like this without trying them, so I may have gotten the order wrong on the offset math. (edit to fix typo, sigh)

// we already know we are within the object bounds // so calculate the offset between the object center and the touch point Vector3 offsetPosition = transform.position - touchPos;

//add the x and z position to new vector3 Vector3 newPos = touchPos;

newPos.x += offsetPosition.x; newPos.z += offsetPosition.z; newPos.y = transform.position.y;

transform.position = newPos; //Vector3 is a struct must be set from new Vector3

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 The 3D Ninja · Nov 12, 2010 at 02:53 PM 0
Share

Thanks for the help! I'm not trying to move the object towards the touch position over time. I need to stop the object's center from snapping to the touch position.

The test item I'm dragging is a cube and my scene is rendered from a top down view. So, viewing the cube from the top-down view, if I touch the cube off-center like towards the bottom or side and begin to drag, the cube's center snaps to the finger. However, I'd like to have the cube just be dragged from where the finger position is.

I think I need to offset the cube's position by the finger position, but I'm not sure how.

avatar image Grumblekeen · Nov 12, 2010 at 02:59 PM 0
Share

I think I understand what you mean now. :) I'll fix my answer.

avatar image The 3D Ninja · Nov 12, 2010 at 08:02 PM 0
Share

I gave it a go and couldn't get the offset to work correctly. The gameObject seemed to be pushed away from the touch point. I tried subtracting touchPos - transform.position, but it gives the same results.

Thanks for the help! I'll keep messing with it and post my results.

avatar image
0

Answer by stford · Dec 11, 2011 at 01:43 AM

Im trying to re write it in java script what dose the "out" in the ray cast do i cant get it to work?

steve,ok im trying to do it in java script what is the "out" in the ray cast doing??

thanks steve

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate on drag for IOS? 1 Answer

Unity2D iOS Dragable items by touch 0 Answers

EventSystem IDragHandler not working on iOS 1 Answer

Tap to Move Drag to Look Null Reference 1 Answer

Rotating a gameobject via dragging finger 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