- Home /
Touchscript drag constraints
I'm using a preset found here: https://www.assetstore.unity3d.com/#/content/7394 called Touchscript. Mainly the part that allows me to click and drag on Android devices. With this preset, the drag has a limit which is defined by the starting position of the GameObject (i.e. When you go into play mode it wont scroll down any further than the starting point) Looking into the (very complicated) set of code, I believe I've found the code that implements this:
using System;
using TouchScript.Events;
using TouchScript.Gestures;
using UnityEngine;
using System.Collections;
public class Side : MonoBehaviour
{
public float Speed = 10f;
private Vector3 startPosition;
private Vector3 targetPosition;
private void Start()
{
startPosition = targetPosition = transform.localPosition;
GetComponent<PanGesture>().StateChanged += OnStateChanged;
}
private void Update()
{
//Debug.Log(targetY);
var fraction = Speed * Time.deltaTime;
//transform.localPosition = targetPosition;
transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, fraction);
}
private void OnStateChanged(object sender, GestureStateChangeEventArgs e)
{
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var target = sender as PanGesture;
Debug.DrawRay(transform.position, target.WorldTransformPlane.normal);
Debug.DrawRay(transform.position, target.WorldDeltaPosition.normalized);
var local = new Vector3(0, transform.InverseTransformDirection(target.WorldDeltaPosition).y, 0);
targetPosition += transform.parent.InverseTransformDirection(transform.TransformDirection(local));
if (transform.InverseTransformDirection(transform.parent.TransformDirection(targetPosition - startPosition)).y < 0) targetPosition = startPosition;
break;
}
}
}
Specifically this part:
private Vector3 startPosition;
private Vector3 targetPosition;
private void Start()
{
startPosition = targetPosition = transform.localPosition;
GetComponent<PanGesture>().StateChanged += OnStateChanged;
}
My question is, would anyone be able to adapt this code to apply to the bottom of the GameObject to prevent it scrolling up any further?
Answer by hollym16 · Dec 11, 2013 at 11:28 AM
I found out how to do it! After a week of confusion I worked out that in order to make it scroll to the bottom and not ping back up to the top, you must get the local position as you call the endPosition. This is the code I ended up with:
using System;
using TouchScript.Events;
using TouchScript.Gestures;
using UnityEngine;
using System.Collections;
public class Side : MonoBehaviour
{
public float Speed = 8f;
private Vector3 startPosition;
private Vector3 endPosition;
private Vector3 targetPosition;
private void Start()
{
startPosition = targetPosition = transform.localPosition;
endPosition = targetPosition = transform.localPosition;
GetComponent<PanGesture>().StateChanged += OnStateChanged;
}
private void Update()
{
var fraction = Speed * Time.deltaTime;
transform.localPosition = targetPosition;
transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, fraction);
}
private void OnStateChanged(object sender, GestureStateChangeEventArgs e)
{
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var target = sender as PanGesture;
Debug.DrawRay(transform.position, target.WorldTransformPlane.normal);
Debug.DrawRay(transform.position, target.WorldDeltaPosition.normalized);
var local = new Vector3(0, transform.InverseTransformDirection(target.WorldDeltaPosition).y, 0);
targetPosition += transform.parent.InverseTransformDirection(transform.TransformDirection(local));
var end = new Vector3(0, transform.InverseTransformDirection(target.WorldDeltaPosition).y, 0);
targetPosition += transform.parent.InverseTransformDirection(transform.TransformDirection(end));
if (transform.InverseTransformDirection(transform.parent.TransformDirection(targetPosition - startPosition)).y < 0) targetPosition = startPosition;
if (transform.InverseTransformDirection(transform.parent.TransformDirection(targetPosition - endPosition)).y > 0.9) targetPosition = transform.localPosition; //Specifically specify here to find the local position, rather than referencing the endPosition
break;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613123408im_/https://answers.unity.com/themes/thub/images/avi.jpg)