I got this code that drags a 2d sphere when i touch the screen:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dragstick : MonoBehaviour {
private Vector3 offset;
void Update()
{
if (Input.touchCount > 0)
{
Vector3 touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
if (hitInformation.collider != null)
{
GameObject touchedObject = hitInformation.transform.gameObject;
switch (touchedObject.tag)
{
case "Player":
Debug.Log("Touched Sphere " + touchedObject.transform.name);
touchPosWorld.z = 0f;
transform.position = touchPosWorld;
break;
}
}
}
}
}
Thing is, if I move my finger too fast, the drag stops and the sphere drops... also it runs kind of slow when i execute it...
Any help about what am I doing wrong? Maybe I'm making things too complex, drag and drop of a sphere should be really easy but functions in unity have changed a lot.
Thanks in advance!