Hello Guest

Author Topic: Draggable sprite/button?  (Read 8069 times)

lenix

  • Newbie
  • *
  • Posts: 3
    • View Profile
Draggable sprite/button?
« on: August 14, 2012, 01:22:59 pm »
Hello,
I was wondering if a 2d toolkit sprite or a 2d toolkit button could be modified to be dragged by finger.
There is a feature I want to develop where a button ( or a sprite ) is displayed on the screen and the user needs to touch it via finger ( or mouse on windows ) and drag it to a specific position on the screen.

Is this possible?

Many thanks in advance unikron,

MooNiZZ

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: Draggable sprite/button?
« Reply #1 on: August 14, 2012, 03:09:01 pm »
I don't think this is achievable with the tk2dButton, I would just make a sprite with a collider, give it a "button"-tag or something, and using a raycast against it and check if the  left mouse button is pressed, and if it is just move the buttons transform to mouse pointer.


Something like (this is by mouse, but you know how to change it I hope :) )
Code: [Select]

void Update(){

CheckButtonInput();

        if (buttonObject != null)
buttonObject.transform.position = Input.mousePosition;
}


void CheckButtonInput(){

if (Input.GetMouseButtonDown(0)) {
    Ray ray = camera.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast(ray, out hit, 1000)){
    if (hit.collider.tag == "button"){
buttonObject = hit.collider.gameobject;
} else {
buttonObject = null;
}
    }
}

if (Input.GetMouseButtonUp(0)){
buttonObject = null;
}
}

Something like that would prolly work, haven't tried it :D I hope you get the idea.
« Last Edit: August 14, 2012, 03:54:45 pm by MooNiZZ »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Draggable sprite/button?
« Reply #2 on: August 14, 2012, 06:26:05 pm »
What MooNiZZ says is probably the best thing to do. It covers the basics, but you'll need to do a bit more to make it proper and polished - eg. remembering where the user tapped/clicked and then keep the correct offset, otherwise the button will jump to the cursor at the start.

bahaahamza

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Draggable sprite/button?
« Reply #3 on: October 09, 2012, 06:18:26 pm »
Hi,
can this be added to the roadmap of 2dtoolkit.
i used orthello before and it supports user input on sprites by default (drag, click, hover,etc.).
I think it will be a very good feature to add.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Draggable sprite/button?
« Reply #4 on: October 10, 2012, 12:30:25 am »
I had plans to do something like this earlier, but that has been postponed. I'll add it to the requests list, but I don't know when it'll get done as there's a huge pile in the todo list already...

eaymon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Draggable sprite/button?
« Reply #5 on: November 03, 2012, 03:10:25 pm »
I am currently using EasyTouch 2.0 from the asset storre to do alot of these touchable events and it works really well over all... its and additional $15 but well worth it.

eaymon

VeTaLkO

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Draggable sprite/button?
« Reply #6 on: August 11, 2014, 10:33:38 pm »
As this thread is quite high in google (and it has 1.7K views), i decided to add my answer here.

So, just in case someone will found it useful, final code is

Code: [Select]
void Update ()
{
CheckButtonInput();

if (selectedObject != null)
{
newPosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
newPosition.z = 0;
selectedObject.transform.position = newPosition - selectedDelta;
}
}


void CheckButtonInput()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
if (hit.collider.tag == "Card")
{
Debug.Log("Found a Card at " + hit.point);
selectedObject = hit.collider.gameObject;
selectedDelta = hit.point - hit.collider.transform.position;
}
else
{
Debug.Log("Found something " + hit.collider.tag);
selectedObject = null;
}
}
else
{
Debug.Log("Found nothing");
}
}

if (Input.GetMouseButtonUp(0))
{
Debug.Log("Releasing");
selectedObject = null;
}
}