Alright so I’m not really sure about how I would script this idea I have on my mind, so I decided to ask my peepz from the forum to help me with the idea like they always do.
Got this thing set up:
http://s32.postimg.org/uh2spap1d/trns.png
And what I want with that is for the circle to be draggable, but only by the transmission pattern’s boundaries, and I have no clue where to start with this or anything without making it really buggy. So, any ideas ?
Hopefully i can bump now…
Please ?
The first step is to make the circle to be draggable - without any limitations. Have you tried that?
Have you tried anything?
Start with some tutorials about drag and drop (you’ll probably use some on mouse down and mouse up events to start and stop the drag functionality).
If you have a problem with that come ask us questions based on whatever tut you use.
Then with that you’ll then need to set up boundaries… but as @Baste pointed out, you need the dragging first.
We’re not going to write this for you, we’ll help you write it for yourself (give a fish vs teach to fish and all)
… but… but… I liked this one…
using canvas with arbitrary recttransforms defining the “rails”, different colours are different recttransforms
edit:
might continue on with this later, should really be snapping the handle to the middle of the rail, which would help with getting stuck on the sides… although it might have issues with the rail switching though.
@RickyX
have a look at the IDragHandler, IBeginDragHandler, IEndDragHandler EventSystem interfaces for the basic drag handling.
For the “within rails” have a look at the RectTransformUtility.RectangleContainsScreenPoint(…) function
Yeah guys, i don’t know if you’re gonna believe this or not but i’m not a beginner in unity. (note: i’m not saying i’m not a beginner just because i can make the circle draggable, i’m saying it because it might give you a better idea of how to explain stuff to me) Yes of course i can make the circle draggable. The boundaries are my problem. I think IBeginDragHandler, IDragHandler and IEndDragHandler might be the best thing to use here, but it’s not like it really matters that much when it comes to what i’m gonna use to drag the thing. So here, i just set up this:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class TransmissionDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
}
But what i don’t have an idea about is how to make the boundaries that’s not gonna be a simple box, limitations by max x and max y, that’s not what i want and that’s pretty easy to make. I need the boundaries to be the actual transmission pattern, and the circle is draggable only over the pattern, the lines of the pattern, so it doesn’t leave the lines ever.
Thanks in advance.
Hmm okay i’ll see what that RectTransformUtility.RectangleContainsScreenPoint(…) thing is.
I don’t like the way it works on that gif. I want the circle to still follow the mouse when the mouse leaves the boundaries, i just don’t want the circle to leave the rails, the mouse should be able to, do you get me ? Hard to explain.
EDIT: Also i have no idea how to implement the function you mentioned, i don’t even see how I can achieve anything using it, perhaps more explaination ?
EDIT 2: Yeah actually you said it yourself, i don’t want the circle to be stuck when the mouse leaves the rails, it should still try to follow it but not literally like set it’s position to the mouse position, because then it’s gonna leave the rails.
EDIT 3: Oh so what you did there is just checked when the mouse is on the rails, the circle will follow it, but when it’s not - the circle will stop following, yeah that’s not gonna be good enough for me.
To make the object attempt to follow get the direction that the touch point is from the object, if its the same as the way the current part of your path is facing then move the object along the path altering speed for accuracy. You can check both ways to allow dragging backwards, ar a junction where two paths meet if its within a certain threshhold use that path for the calculation aswell.
I can’t really understand how that would work in my head but i’ll at least try to script it.
OK, so then why didn’t you start out with “this is what I have, I’m having a problem ‘here’”.
It’s not like we have intimate knowledge of who you are, what you’re capable of, or any of the implementation that exists in your project.
As your OP goes, if I thought to start out explaining it… I probably would have ended up explaining drag code that you’ve already implemented completely wasting both of our times… not cool.
As for your problem. So you have drag and drop.
So when dragging one usually moves the draggable object towards the mouse position, rather than directly to it, to give it that nice drag sensation.
Well all you to follow that with is find boundaries.
But what sort of boundaries should they be? Rigid straightlines, or can they wiggle within the straight lines? If they’re rigid lines, well just define those lines and find the nearest point on the line that the mouse position is and move towards that.
If it can wiggle around inside a spacial region… well then you’re going to need to find the closest point relative to that region. It’s a bit more work than rigid lines.
In the end, you should maybe decide how you want the look and feel of the motion to be… describe that to us, as that would again help us figure out what you want specifically. Instead of you know LeftyRighty implementing something you don’t approve of because it’s not like you want…
again, we don’t have intimate knowledge of who you are, and what you want!
Sorry was in abit of a rush when I wrote that, ill break it down abit.
- setup some empty objects to act as nodes, these will be used for the start and end of lines.
-write a script that lets you store what nodes the current node can go to. Setup the nodes accordingly
-write another script that has a list of all the nodes. This script will work out where the current touch input is relative to the object its attached to, to get the touch direction. You will want to store the nodes we are moving between, you can get the direction of each node relative to the marker compare with the touch direction and based on accuracy move towards the node thats in the touch direction. Once close enough to a new node, check the nodes its connected to and compare touch direction to there direction then update the new node path your own.
-the script above should probably be placed on the marker
Hope that clears things up
Oh, oh i get your general idea here. I like how it’s taught out but I’m not gonna be able to script it completely, I will try though and when i’m stuck i guess i can come back and ask you where to go from there.
Yeah sorry about that stuff… Um, yeah both of those would work for me, it can move in straight lines or wiggle around, doesn’t matter. But your explaination on how to do it does not help me at all, like i have no idea what you just said.
Because I didn’t go into full depth, because I’m not going to explain in depth that which you have not decided.
Dude, you really sound like you’re code begging here…
I’m outs. Peace.
I archived the effect that @LeftyRighty did by doing the following:
I made this simple script for the circle itself:
public bool CanFollow;
void FollowMouse()
{
if (CanFollow == true)
{
transform.position = Input.mousePosition;
}
}
void EnableFollow()
{
CanFollow = true;
}
void DisableFollow()
{
CanFollow = false;
}
And then I added 3 diffrent event triggers on all of the rails which are: Drag, pointer enter, pointer exit.
When the drag is triggered I call the function FollowMouse from my circle script, when the pointer exit is triggered I call EnableFollow and for pointer exit i call DisableFollow.
It works exactly like @LeftyRighty 's gif.
But of course, it’s not gonna be good enough for me so i wrote it for no reason.
What is there to explain pal ? I explained everything there was to explain, and clearly @lordconstant understood me.
When I said ‘explain’ I was referring to ME explaining to YOU what I was talking about in further depth.
I asked YOU to ‘decide’ what sort of implementation you wanted. Why should I waste my time explaining one way to do it when the user experience might not be what you want… case in point, LeftyRighty’s example was something you did not want.
Yeah… not doing that for you.
So I gave simple basic generalized explanations, and you merely respond with “But your explaination on how to do it does not help me at all, like i have no idea what you just said.”
lordconstant happening to trip over the user experience you want is called coincidence.
The fact you don’t make choices and rely on people willy nilly dropping knowledge nuggets that may or may not work for you shows a lack of effort on your end, which smells like you expect everyone else to do your work for you.
I call that code begging.
Think whatever you want, it’s not true what you’re saying. I rarely ever post on this forum because i write all of my code myself, only when i really don’t have an idea of how to create something i imagined i come here and ask for people to send me into the right direction. I am not asking for code, i am asking for an idea, and when i say i don’t know if i’ll be able to script it, it doesn’t mean i’m asking you to script it for me. I’m going to try.