- Home /
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
}
}
}
}
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;
}
}
}
}
I'm gonna fucking cry. Have been wrestling with this for days and this finally did it! Thank you so much!
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
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
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.
I think I understand what you mean now. :) I'll fix my answer.
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.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613082821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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
Simultaneous Touch Drag Controls 0 Answers
Help With Touch to Drag Script 1 Answer