- Home /
Detect screen tap vs. drag
I have tried to edit the drag iphone script to get it so that when I touch the object, the player picks it up, and when it touches again, the object drops. This works, but I do not want the cube to be picked up or dropped if the player drags his finger before or after touching the object. I just cannot make it work. The script i have seems to only reference one or the other, either touchphase.began or touchphase.ended, but it will not register both. How can I solve this issue? The script is below.
using UnityEngine;
using System.Collections;
public class DragIphone : MonoBehaviour {
public float spring = 50.0f;
public float damper = 5.0f;
public float drag = 10.0f;
public float angularDrag = 5.0f;
public float distance = 0.2f;
public bool attachToCenterOfMass = false;
public Transform BoxPosition;
private bool touched = false;
private SpringJoint springJoint;
private Touch touch;
private Touch touch1;
private Vector2 tposition;
void Update (){
// Make sure the user touched the screen
if (Input.touchCount==0){
return;
}
Camera mainCamera = FindCamera();
// We need to actually hit an object
RaycastHit hit = new RaycastHit();
touch = Input.GetTouch(0);
// touch1 = Input.GetTouch(1);
if(touch.phase == TouchPhase.Began){
print("began");
tposition = touch.position;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(touch.position), out hit, 100.0f))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if (!springJoint){
GameObject go = new GameObject("Rigidbody dragger");
//Rigidbody is automatically added by SpringJoint
springJoint = (SpringJoint)go.AddComponent ("SpringJoint");
go.rigidbody.isKinematic = true;
}
springJoint.transform.position = hit.rigidbody.transform.position;
if (attachToCenterOfMass){
Vector3 anchor = hit.rigidbody.transform.position;
// transform.TransformDirection(hit.rigidbody.centerOfMass)
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}else{
springJoint.anchor = Vector3.zero;
}
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine("DragObject", hit.distance);
}
}
private int count;
void start(){
count = 0;
}
IEnumerator DragObject (float distance){
if (touch.phase == TouchPhase.Began && touched == true){
print("yes");
touched = false;}
else if(touch.phase == TouchPhase.Began && touched == false){
touched = true;
}
// else if(touch.phase == iPhoneTouchPhase.Began && touched == true){
// touched = false;
// }
float oldDrag = springJoint.connectedBody.drag;
float oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
Camera mainCamera = FindCamera();
while (touched == true){
// touch.phase != iPhoneTouchPhase.Ended &&
// touch.phase != iPhoneTouchPhase.Canceled
Ray ray = mainCamera.ScreenPointToRay(touch.position);
springJoint.transform.position = new Vector3(BoxPosition.transform.position.x, BoxPosition.transform.position.y, BoxPosition.transform.position.z);
yield return 0;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = 0;
springJoint.connectedBody.angularDrag = 0;
springJoint.connectedBody = null;
}
}
Camera FindCamera (){
if (camera)
return camera;
else
return Camera.main;
}
}
Answer by jahroy · May 24, 2011 at 12:52 AM
var firstFinger = Input.GetTouch(0);
if ( firstFinger.phase == TouchPhase.Moved ) {
Debug.Log("I am dragging my finger...");
}
I have done that, but then below in the ienumerator function all other phases other than that phase are not detected. Why does this happen?
Answer by crevelop · Aug 18, 2011 at 10:44 PM
I've made few tutorials on dragging and rotating an object by touch(Unity iOS).
These are the Youtube videos:
Dragging http://youtu.be/QjUhQ4z6pF0
Rotating http://youtu.be/oOfPMKdJdKk
Get the source code from my website:
Hope it helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop Kinematic Rigidbody from moving through walls 2 Answers
How do i make a continuos touch that triggers more then one "button"? 1 Answer
Touch and drag an object 0 Answers
Click Drag not working on iphone 2 Answers
What's the matter with this script? Detect object dragging with iPhone Touch 1 Answer