Playmaker Forum

PlayMaker Help & Tips => iOS Help => Topic started by: Slater on September 10, 2012, 12:04:21 PM

Title: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on September 10, 2012, 12:04:21 PM
Hi,

This is a nut I've been unable to crack and I have read and searched and tried numerous things without any luck.

I am trying to drag an object in my scene. Its a 2d setup, so only need to drag it in xy not z. I have tried using Touch Object Event and get it to move, but whatever I try it either moves however it wants/weird or it moves to slow and "drops" and you need to grab the item again.

So how do I go about doing this? I just want to pick a specific object and be able to drag it across the screen following my finger at "full speed".

I have found a lot of similar questions about this, but not a simple understandable answer yet.
Title: Re: How to touch and drag a 3d object?
Post by: Sjones on September 10, 2012, 12:13:01 PM
the iOS youtube tutorials for playmaker should give you the best advice, utilising the different functions throughout the get touch info section.

if its too slow make sure normalise information is set, using a screen pick and a bool test to send onto the move event, with the normalised info there shouldnt be any issue.

once you got the object keep running the fsm to get (touch location) and set (object location) position until touch ended.

have every frame on too.
Title: Re: How to touch and drag a 3d object?
Post by: Slater on September 10, 2012, 06:32:57 PM
Unfortunately I have tried all of that, watched all the tutorials several times, but none of them shows dragging an object.

This should be a pretty common feature for anyone to want. The best would even be if there was an action for it. Say touch drag object where you set what object to drag on touch and in what vector.
Title: Re: How to touch and drag a 3d object?
Post by: Sjones on September 10, 2012, 07:10:56 PM
as long as you have the object specified (can get that from a screen pick) the attached screenshot will work, I use it to move a created object from the previous fsm
Title: Re: How to touch and drag a 3d object?
Post by: Slater on September 11, 2012, 10:23:21 AM
Thanks for your answer. Its working much better with your suggestion, but I still have a few problems. One is that the object is still moving a little bit to slow. It "lags" behind. If I drag a bit to fast, I drop the object and then it resets to the center of the screen.

So in my first state I have Touch object event and touch began that transitions over to a new state with what you showed in the screenshot. Any suggestions on how to have the object move faster and also not to have it center on screen if it would be dropped (which hopefully it wont when speed is good).

If I increase the collider it gets harder for the item to get dropped. Was trying to increase the collider in state 2, but couldnt find a way to do that. Also not sure if that is a smart way to go about it, increasing the collider to pretty much the screen size as long as you touch it?

Thanks for your help!
Title: Re: How to touch and drag a 3d object?
Post by: Sjones on September 11, 2012, 07:17:55 PM
this to me sounds like lag, is this a completely empty scene with just the cube and also are you on an actual device or emulating it? i dont know about the iOS emulation (if there is one) but the android is supposedly really slow, this could be your issue.

dont forget that mobile devices can be extremely slow compared to PC's especially the older gen ipads etc (I read that the original ipad starts lowing fps at aprox 25 draw calls) compared to the new devices that I am currently running 100 draw calls at 60 fps.

if u got profiler use that to see how the device is handeling, if not there was an fps action somewhere on the forums recently, add that as an action and convert it to text and display on screen, it will give a good indication to how its running.

as to your question about making it not jump, have a finish state that goes out and back to your wait for input, use a touch event ended to trigger this event, it should keep the x & y values to their last value, keeping it exactly where it was, if that still doesnt work, copy the values and have an in-between state that on untouch uses the position that was saved and make sure it is set to where it was, then send event and finish back to the start state again.


Title: Re: How to touch and drag a 3d object?
Post by: Slater on September 12, 2012, 07:05:26 PM
I actually don't know why it is a bit sluggish, I don't think its a performance issue though. However I managed to do "Set Property" on the Collider so as soon as I move the object, the collider gets bigger and now I can't slide off the collider. When I stop the touch and it goes back to "wait for touch" state, it makes the collider smaller/to original size again. The slugginess is not that much, that it is enervating, it actually works out pretty nice now.

So thank you for the help!
Title: Re: How to touch and drag a 3d object?
Post by: jeanfabre on September 13, 2012, 07:46:56 AM
Hi,

ok, I have created a working sample. It uses a custom action I am a while ago,

http://hutonggames.com/playmakerforum/index.php?topic=272.0 (http://hutonggames.com/playmakerforum/index.php?topic=272.0)

It's named after "mouse" but really it was built up from the ground to accept just a screen position, so that works with anything really.

If you have problems understanding how it's done, let me know,

bye,

 Jean
Title: Re: How to touch and drag a 3d object?
Post by: jeanfabre on October 04, 2012, 01:44:05 AM
Hi,

 ok, after a request, I modified the logic I implemented in that package above to overcome a issue with it.

basically, it doesn't check the gameObject you touch. That is, you touch and drag and no matter what it will.

Now in many cases you actually want to have control over what you are dragging and certainly want to let the user drag what he has touched. The package attached to this post is a variation that takes into account this situation. The logic is moved into the camera to becomes generic to the scene.

 To improve raycasting efficiency, I only raycast on a layer I created called "drag". All objects that can potentially be dragged are in this layer. It doesn't no mean that they are enabled for dragging, for this, I use a tag called "drag enabled" that I set on the object I want to be currently enabled for dragging, You can always set the tag of an object using the action "set tag". This layering it not mandatory, you could simply ignore layers and raycast everything and simply check for the tag itself.

The down side of this technic is that only one drag at a time can happen. This is likely fine, but if you want multiple dragging, then this dragging would have to be hosted by each individual gameObject that can be dragged, this way they work independtly and multi touch dragging can therefore happen.

As always, questions and comments are welcome,

Bye,

 Jean
Title: Re: How to touch and drag a 3d object?
Post by: Slater on October 23, 2012, 10:39:33 PM
Ha! Jean, I just decided to try your thingy out instead of the one that I had worked with, since I had a problem that when a finger already was on the screen, the object that I touched acted weird.

Since my work is in 2d I changed the camera to orthograpic size. I also changed touch detection to "moved" instead of "began", since I want it less sensitive and the option with my underlying FSM that have the object get selected and outlined and I didnt want the object to move with to much sensitivity.

I also had to change the setting in the drag state to World and lock Z to -40 (my camera is on -50). After that it works great. Now I only need to work out the colliding with target object thingy instead of using rect.

Cheers.
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on November 07, 2012, 06:16:51 PM
So I had an idea for our website that we are launching very soon to promote the app we are making. The idea is to have the first scene playable on the website, so that the users can see and try it out. The problem I have right now is that everything I have done so far has been for touch. So on all the places that I have touch object event, I have now added mouse pick event as well and it seems to be working well.

The only thing I haven't sorted out yet is this drag functionality that I placed on the camera. It only works for touch now. How would I go about doing the exact same thing, but for mouse? Can I add something into this FSM or is there another one I can use that works the same and activate that when I build for mouse input?

 
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on November 08, 2012, 05:57:41 AM
Hi,

 It's quite simple, on the states you check for touch events, also check for mouse events as well and trigger the same events of processes.

bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on November 08, 2012, 08:27:43 AM
I'm a bit confused of the name of the action I should use and how. I can't find one called Mouse events. I only find mouse pick and mouse pick event, but they don't look the same as touch event.
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on November 09, 2012, 08:21:33 PM
Sorry for bumping but I can't figure this one out. As I understand it, I need to put in something in 3 states, but I don't understand what to put in them. "check if this gameObject is allowed to be dragged", doesn't need anything more as I understand it. "Detect drag", "check target" and "drag" are the once I need to put something in to detect mouse actions. But I can't figure it out. I hope anyone can help me out here since we are planning on releasing the website this weekend and it would be nice to have this feature in it.

Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on November 12, 2012, 08:08:10 AM
Hi,

 You simply need to use "Get Mouse Button down" and "Get Mouse Button Up" as extra actions next to the "touch event" actions.

If you need a working sample, let me know.

bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on November 12, 2012, 09:55:23 PM
Hi, Yeah I could use a sample for it. I am still confused to where to put stuff.

We launched the website yesterday. You can check it out here www.hannahenri.com and see how it looks. Would be nice to be able to update the drag thing for mouse soon.

I really appreciate your help Jean. Without your help here I would have had a much harder time as a new programmer.
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on November 13, 2012, 01:11:19 AM
Hi,

 this is odd, I can't access your site, maybe it's just some dns issues.

bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on November 13, 2012, 06:27:47 AM
Hi,

 ok, Just did a working sample for a small game startup and it features a drag system that works both with Touch and Mouse, basically I copied the drag Fsm and replaced the actions to move from touch to mouse.

http://hutonggames.com/playmakerforum/index.php?topic=2544.msg11501#msg11501 (http://hutonggames.com/playmakerforum/index.php?topic=2544.msg11501#msg11501)

If you don't own MegaFier, simply delete the offensive Action. I have also disabled the rendering of the dragged gameObject, but you can simply turn it on to really witness the dragging.

bye,

Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on November 14, 2012, 02:18:48 PM
Thank you so much. Through the example I could work it out and make it work in my scene. The website is up so you should be able to check it out.

Either www.hannahenri.com or www.hannahenri.se

Cheers!
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on November 15, 2012, 01:16:56 AM
Hi,

 good. Tho I am still unable to view your domain, maybe it's just a glitch because I am in Russia.

bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on February 09, 2013, 08:21:29 AM
Could use some help again here. In one of my scenes I have pictures in a row. I have them all grouped under one empty game object that has a collider. I have locked it so that I can drag the row up and down and all works well except that when I release the finger and then touch again to move the row further up or down, the row centers itself back to original finger center position. Any suggestions what I can do to not have it center back?
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on February 20, 2013, 08:12:42 PM
Any suggestions?
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on February 21, 2013, 12:46:50 AM
Hello,

 that's difficult to say as is, but it's likely because you don't take in account the current scrolling value.

 say scroll 0 is top, and scroll 1 is when you have scrolled all the way down.

if you drag to scroll 0.3, release and start dragging again, you need to take in account that the scroll is 0.3 and thus offset the touch drag delta position to ADD to the scroll and not SET the scroll.

 does that make sense?

bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on February 27, 2013, 04:51:52 AM
Thank you. Yes it makes sence, just see if I can figure out how to do that :)
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on May 06, 2014, 07:43:16 AM
Hi, I am back :)  I am now back to this problem over a year later, crazy. I have a new project where I need to drag the scene around(alternatively move the camera). It is 2d. I have the background and all the objects in the scene grouped under an empty object I call "wrapper". The camera is "zoomed in"(lower orthograpic size). I have set the "wrapper" to be draggable and can move the whole scene around with my finger on the iPad. The problem is that when I release the finger from the screen and then put it back on to continue moving it the "wrapper" resets the center point to where the finger is.

What is it in the fsm that makes the object, in this case the "wrapper", move and center to the position? What can I do to make it start from the position it is currently in without resetting to the finger.

I also need to set a limit on how much you can drag it in x/y position, so that you don't drag outside the edges of the texture image. Any suggestions on this?

Thanks/
Slater
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on May 14, 2014, 07:17:06 AM
Hi,

 you need to use a relative translation as opposed to an aboslute, so when you drag and object around, you don't move it to the position of the finger, you translate it everyframe by the amount of drag for that frame.

 does that make sense?

 as for your limitation, one easy way, without getting into too much math is to use triggers, simply suround your area with virtual walls ( triggers) and when your object touches them then you don't allow movement further in "that" direction.

 Tell me how you get on with this, it's quite a hot topic, so if you keep struggling, I'll see if I can make a micro sample showing this.

 Bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on May 14, 2014, 05:45:35 PM
I kind of understand the theory of how to do it. I just don't manage to figure out how to do it with the tools I have in Playmaker. I will probably go "ahhh" how stupid of me, when I see the solution heh, but I have a hard time on how to actually make it work.
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on May 15, 2014, 01:06:12 AM
Hi,

 Do you have Input.touches? I just did a similar demo yesterday for it.

Bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on May 19, 2014, 06:03:22 PM
No I don't. Guess it wouldn't be to bad to get it though, just haven't done it since I haven't needed it so far. I am in no hurry anymore to get this solved since I didn't get the contract unfortunately, but it would still be great to have it sorted to be able to use it in the future in other projects or to implement it in current apps where it could be useful.
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Redhawk on June 03, 2014, 01:37:02 PM
I tried out the sample you created for moving the object.  The moveable/dragable cube goes straight through the other cube.  No collision whatsoever.  Objects shouldn't be able to move through the other.  How can I fix this??? :)
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on July 13, 2015, 08:11:29 PM
So I just updated to the latest version of Unity, 5.1.1f1, and all of a sudden it doesn't work to pick an object and drag i in my apps anymore. The item I pick out of 6 disappear when I try to drag it and sometimes other objects as well just disappear. When I release the finger they items show again.

Any idea on why this happens with latest Unity version?
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on July 15, 2015, 11:45:46 AM
Jean, does it maybe have to do with the mouse 3d plane drag action and that it maybe is out of date with new changes? I haven't found anything in "my added code" that seem to create this problem. I am a bit lost here and really need this to work :P
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on July 15, 2015, 12:30:55 PM
Hi,

 uhm, indeed the Physics engine is all new, but raycasting should remain working.  Can you confirm the regular sample on the matter works or is it just in your implementation?

 I'll test tomorrow anyway.

 Bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on July 26, 2015, 03:38:06 PM
Hi,

I have tried the regular sample now and it works. The problem with my implementation is in the "Drag" - state. This problem occurred with Unity 5.1.1f1 and haven't been a problem before. It has been working for 2 years.

The difference in my implementation is that I have the Camera set to Orthographic size with -50 in Z. In the Drag State I have "Set position" set to: Game object "Picked Object", Vector "Result" and then I have also Z "-40" (to lock the object to "2d") and Space "World".

It works when I run it on the computer with Unity Remote 4. But when I have it built and run it on the iPad(IL2CPP), it doesn't work.

When I grab one of the objects and drag it, it disappears. What is even more weird is that sometimes also some of the other objects disappear when I drag one of them.

Any idea on what is going on here?
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on July 29, 2015, 10:33:27 AM
Do you have any idea Jean? I think you get the same problem in your sample if you put in these values. Is there another smarter way to "lock" it to 2d?
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: jeanfabre on July 31, 2015, 04:47:46 AM
Hi,

 uhm, that's very odd,I am not sure what happens. Is the example I provided as is works fine?

Bye,

 Jean
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: Slater on July 31, 2015, 05:55:59 AM
Yes. But not when adding the z -40 and orthographic.
Title: Re: How to touch and drag a 3d object?[SOLVED]
Post by: gregacuna on December 21, 2020, 12:19:14 PM
Old thread, but since the post by SJones worked for me to be able to touch/drag a game object I thought I'd reply to ask the simple question...

How can I do this same thing on a GUI element?