- Home /
 
Touch and drag object on path with iTween
Hello,
I'm trying to move an object along path by touch, using iTween. I'm not very good at coding. I searched the web for days to find a solution but i couldn't. I found iTween example "Path-constrained Characters" and thought I could modify it for my needs.
I'm posting the code and a video of what I managed to do so far. You can find the video here: https://youtu.be/ijddFumeBH4
This is close to what I want but as you can see in the video, it staggers a lot and most of the time touch looses the object. Also, since I get the distance between touch point and object on path and add it to path position, object moves forward when I touch and drag the opposite direction. I don't want that to happen.
I want the object to move only in one direction on the path and only if I drag it forward. I'm pretty sure this is not the way to do it. However I have no idea how to do it in any other way.
So I would appreciate if anyone can point me in the right direction.
Thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
 
     public Transform[] controlPath;
     float pathPosition = 0;
     Vector3 coordinateOnPath;
     Vector2 inputPosition;
     GameObject draggedObject;
 
     void OnDrawGizmos(){
         iTween.DrawPath(controlPath,Color.blue);    
     }    
 
     void Update() {
         
         coordinateOnPath = iTween.PointOnPath (controlPath, pathPosition % 1);
 
         if (Input.touchCount > 0)
         {
             inputPosition = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
 
             RaycastHit2D hit = Physics2D.Raycast(inputPosition, inputPosition, 1f, LayerMask.GetMask("Circle"));
 
             if (hit.transform != null) {
                 draggedObject = hit.transform.gameObject;
 
                 float dist = Vector2.Distance (inputPosition, draggedObject.transform.position);
                 pathPosition += dist * .02f;
                 draggedObject.transform.position = new Vector3 (coordinateOnPath.x, coordinateOnPath.y, coordinateOnPath.z);
             }
         }
     }
 }
 
              I am exactly looking for this for past few days. Did you get it working. @Dekman
Your answer
 
             Follow this Question
Related Questions
Unity - Problem when dragging game object 1 Answer
iTween, Put on path & arrow keys 1 Answer
iTween path following on mouse click 0 Answers
Touch controls for Pong game for Android devices 0 Answers
Rotate Bar Based on Touch Drag 0 Answers