playMaker

Author Topic: Touch and Drag the Object on Device  (Read 7072 times)

NN2

  • Playmaker Newbie
  • *
  • Posts: 20
Touch and Drag the Object on Device
« on: October 12, 2016, 12:53:56 PM »
Hi,
I search about this issue for a long time but haven't found the clear answer yet.
I looked all official and user tutorials but still cannot find the clue.

What I did was following very last bit of the tutorial below (21:05 ~ 22:44) and made my object to move exactly like what it is on the video.
But from this point, what I want to do is to make the object moves depends on the amount of dragging, not tracking my finger.
How can I move the object with touching anywhere on screen and drag? I do not want the object to be hidden by my finger and also not jumping to the point where I touch. Is it possible to make the object stay where it was and move by dragging when my finger is on the screen?

thanks for reading and I hope can find good solution.

official tutorial video link is here (by Hutong games):


+ oh, and I also found the code which is exactly what I want to do (but this is for mouse?) add this on here might help you to understand what I am looking for:
Code: [Select]
using UnityEngine;
using System.Collections;

public class MouseDrag : MonoBehaviour {

private Vector3 startPos = Vector3.zero;
private Vector3 endPos = Vector3.zero;
private Vector3 targetPos = Vector3.zero;

private bool isClicked = false;

void Update()
{
DragObject();
}

void DragObject()
{
if (Input.GetMouseButtonDown(0))
{
startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
isClicked = true;
}

if (isClicked)

{

if (Input.GetMouseButton(0))

endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);



targetPos = endPos - startPos;
startPos = endPos;


targetPos = new Vector3(targetPos.x, targetPos.y, 0);
transform.position += targetPos;
}
}
}
« Last Edit: October 12, 2016, 01:03:41 PM by NN2 »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Touch and Drag the Object on Device
« Reply #1 on: October 12, 2016, 04:39:36 PM »
Hi,
I believe that mouse drag also works on mobile, but i am not sure.

But i would suggest to get "Easy Touch 5" or "Input.Touches" for more advanced touch behavior.

The 1st one has PM Actions included and the 2nd on has actions on the wiki.

NN2

  • Playmaker Newbie
  • *
  • Posts: 20
Re: Touch and Drag the Object on Device
« Reply #2 on: October 13, 2016, 11:34:53 AM »
Hi,
thank you for the reply.
1. As you said, the mouse drag script worked. However, whenever I click other object while my finger is on, nothing can be active because the script I use does not contain fingerID. Unfortunately, I do not have much knowledge about script so I don't know how to put the fingerID into the script (this is the reason why I chose to use playmaker as well).
Not sure if I can ask this question here but can you make it work? How can I put TouchID into the script?

2. As following your advice I will try to use the assets that you suggested.
but because they are similar to each other, I am not sure which one is better me to use. If you are using both of them could you tell me the difference between them?

Sorry for too much questions. Hope I don't bother you much.
thanx

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Touch and Drag the Object on Device
« Reply #3 on: October 13, 2016, 12:05:01 PM »
Hi,
Well, i have input.touches, but i am planning to buy easy touch 5

input.touches is getting outdated

NN2

  • Playmaker Newbie
  • *
  • Posts: 20
Re: Touch and Drag the Object on Device
« Reply #4 on: October 14, 2016, 04:40:22 PM »
Ok, I will try these :) think it will work.
Thank you!

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: Touch and Drag the Object on Device
« Reply #5 on: November 15, 2016, 12:42:25 PM »
Why not use the built-in unity event system? Playmaker supports events, like ondrag.