Drag an object for mobile and multiple touch input

Hi all,

I’m currently making a 2D game for mobile similar to the line zen where you are constantly moving and dragging an object.

I’m currently using on mousedown to move the character but there is lag between where you move your finger to and the object moving to that position. It also doesn’t allow for multiple touches on screen.

I’m hoping someone can point me in the right direction for how to enable multiple touches and an effective way for mobiles to have a draggable object. I’ve been looking through various other posts and I can’t make sense of raycasting and struggling to find a decent tutorial explaining it.

How are you moving the moving objects? I would suggest using Time.deltaTime * a speed variable for AI movement. Mousedown is fine for the player but I switched my drag and drop to raycasting it seems more efficient.

Here is my beta drag and drop. It is in .JS or UnityScript. This script is assuming you are placing it directly onto the player and the player has a rigidbody. This is also for a 2d game.

#pragma strict

private var dragging : boolean = false;
private var distance : float;
private var start : Vector3;
private var rb : Rigidbody2D;

function Start()
{
   start = transform.position;
   rb = gameObject.GetComponent.<Rigidbody2D>();
}

function OnMouseDown()
{
     distance = Vector3.Distance(transform.position, Camera.main.transform.position);
     dragging = true;
}
function OnMouseUp()
{
    dragging = false;
}

function Update ()
{
   if (dragging)
        {
          var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          var rayPoint : Vector3 = ray.GetPoint(distance);
          rb.MovePosition(rayPoint);
        }
       
   
}

Hope this helps!

Thanks for the reply unitynoob. I only know C# and my C# knowledge is beginner/intermediate so I need it in C#

I’m also wanting help in understanding how to apply raycasting and find a good tutorial that explains how to solve the solution rather than pasting code. The code Im currently using is.

   private Vector3 screenPoint;
    private Vector3 offset;

    void OnMouseDown()
    {

        offset = gameObject.transform.position -
                 Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }

    void OnMouseDrag()
    {
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
    }

Well converting my code to C# wouldn’t be difficult to do at all. The main differences in this instance would be variable declaration and class organization. If you want I can convert it to C#!

Thank you for the offer, I’ve been able to find code but I want to be able to understand raycasting and input controls for my problem so I can also replicate it in the future. I’ve just started using:

  void Update()
    {
        tCounText.text = Input.touchCount.ToString();
    }

So I can see on my mobile how many touches it is detecting but it never detects more than 2 do you know why this is?