Hello,
I am currently developing a game but I need help with something. Is there any script that allows you (the user) to click/tap on an object and drag it around? I am developing this for the IPhone so any help would be greatly apprenticed. Please PM me.
i have maked it for puzzle. Add this script on gameobject, that should be moved. Add collider2D too. Set it Tag to something, and insert this tag in script on object.
int touchID;
GameObject item;
public string movingTag;
public bool pressed;
void Update ()
{
for (int i = 0; i < Input.touchCount; i ++) {
Touch touch = Input.GetTouch (i);
if (touch.phase == TouchPhase.Began ) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (touch.position), Vector3.forward, 100f);
if (hit.collider != null && hit.collider.tag == movingTag) {
touchID = touch.fingerId;
item = hit.collider.gameObject;
pressed = true;
}
}
if (touch.phase == TouchPhase.Ended) {
if (touch.fingerId == touchID && item != null && item.tag == movingTag) {
pressed = false;
item = null;
}
}
if (touch.phase == TouchPhase.Moved) {
if (touch.fingerId == touchID && item != null && item.tag == movingTag) {
Vector3 itemPos = Camera.main.ScreenToWorldPoint (touch.position);
Vector3 pos = new Vector3 (itemPos.x, itemPos.y, 0);
item.transform.position = pos;
}
}
}
this, only for mouse
void Update ()
{
if (Input.GetMouseButton (0)) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward, 100f);
if (hit.collider != null) {
Vector2 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
hit.collider.gameObject.transform.position = pos;
}
}