I never really know how to word these questions, because there’s always more questions than one.
Essentially, I have an object I move up and down using this code:
private Vector3 offset;
private bool stopDrag = false;
private bool twoClicks;
//private int fingerCount;
void Start()
{
Input.multiTouchEnabled = false;
}
void Update()
{
if (Input.touchCount > 1)
{
stopDrag = true;
}
//if (fingerCount > 0)
//print("User has " + fingerCount + " finger(s) touching the screen");
}
/***Slider***/
void OnMouseDown(){
stopDrag = false;
//screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(1, Input.mousePosition.y));
}
void OnMouseDrag(){
/*int fingerCount = 0;
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}*/
if (Input.touchCount == 1 && stopDrag == false)
{
Vector3 cursorPoint = new Vector3(1, Input.mousePosition.y);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
/***Lock down slider***/
Vector3 pos = transform.position;
pos.y = Mathf.Clamp(pos.y, -4, 4);
transform.position = pos;
}
if (stopDrag == true)
{
return;
}
}
So I’m able to move the object up and down where the cursor position is, but if I do a second touch (say to press a button) and then take my finger off the object with the second touch held down, the object will jump to the cursor position (within the ‘clamped’ Y axis).
Does anyone know of a way to stop that from happening? I’ve tried adding a bool that turns true when more than one touch is detected, but I also want to be able to move the object while I’m making another touch on something else. I guess I want to have my cake and eat it too. Meaning, it’s either one or the other as far as I’m aware.
All I can think of is to make it so the OnMouseDrag can only work if the cursor is hovering over the object, but I’m not sure how I’d achieve that.