- Home /
Why is there so little support for Virtual D-pads?
I can't find anything on Dpads. More specifically, I am looking for a way to control a character with 2 Arrow Buttons that are commonly seen in Mobile's platformers. Cordy 2 is a great example as it positions Left and Right buttons diagonally to each other (which IMO increases tactile feedback).
I'm working on a 2.5D platformer. I tried unsuccessfully to implement D-Pad controls. I thought 2 simple GUIs would work.
Does anyone know how I can get this control scheme to work or is there a prefab out there?
Thank you!
Answer by barjed · Feb 09, 2014 at 08:09 PM
Then your real question is about passing parameters between objects, this isn't an issue related to the DPad specifically.
There are many ways in which you could accomplish that and I suggest reading on the subject because this is one of the fundamentals in Unity and in programming in general.
First identify the place where the Debug messages are actually sent. From there you have to make a decision. We do not know yoru code structure but let's assume you have a game object that's called TouchController and you have a script component called TouchControllerScript.cs that's connected to it that reads values from the on screen buttons.
Add a public property to your TouchControllerScript code, like so:
public GameObject PlayerPrefab;
Now save the script and go back to the inspector, you'll notice that there's a familiar field in your script that let's you drag and drop GameObject entities there. Grab the object that represents your player and drop it there.
Now, from your TouchControllerScript, you have easy access to all different variables of the player GameObject, for example:
PlayerPrefab.transform.position;
Now you can do what I said earlier - construct a movement vector from the axes values you read from the on screen buttons:
Vector3 myMovementVector = new Vector3(xValue,0,zValue);
Pass it into the SimpleMove method (it accepts Vector3 as a parameter):
PlayerPrefab.SimpleMove(myMovementVector);
Note that this requires that a CharacterController component is added to the prefab you'll be moving.
Voila!
Thank you Barjed. You are a lifesaver! I'm sorry everyone that I was not clear on my question.
I first learned Java, so I am aware of parameters and public variables. For some reason, it was difficult to understand that in Unity.
Thank you again!
Answer by Kiloblargh · Feb 09, 2014 at 06:30 PM
By "so little support," do you just mean it requires more effort than GameObject> Create Other > Virtual D-Pad?
It's not difficult to make one from scratch if you know how to use raycasts and colliders. If you can make a touchable anything, you can make a virtual d-pad. If you can't, you need to read the manual.
There are also several pre-built ones on the Asset Store, for around $30, which you would already know if you had made an effort to search for one.
Well, if you need the details.
By little support, I mean there doesn't seem to be any documentation on it or a guide by other users. Youtube is usually reliable, but not in this case.
I am able to make a touchable anything. The problem I have is trying to get that touchable anything to move the character and not just send debug confirmations. I think the key is the FPS Controller. I've changed the Joystick Script to only send out -1 on the Horizontal Axis. That works, but my character can only go in one direction. I tried making a duplicate Joystick script and rena$$anonymous$$g it Joystick2, but of course the Camera Script and Controller Scripts only recognize the regular Joystick Script.
I can keep going, but I am tired and it's been several days.
So don't tell me or any newbie for that matter to "read the manual" and label my search efforts as lazy. And don't tell me to just shell out 30 bucks like I have them lying around. Don't waste my time or anyone else's time.
I am not sure I understand.
If you are already able to get a Axis value from your Joystick then things should be rather simple. There are Controller $$anonymous$$ove and Controller Simple $$anonymous$$ove components that accept Vector as their input and will move your character around the world.
So, construct a Vector3 from your Axes (X and Z), pass it to Controller Simple $$anonymous$$ove and you're set.
The problem is I can't make the connection in scripting. I have a GuiTexture that detects touch by sending Debug messages. How do I make the connection between this GuiTexture and the Controller?
All I really want is one GuiTexture to send -1 and another GuiTexture to send 1 as an input.
The connection is made by measuring the difference between the point of touch on the texture and where it is dragged to (mapped on x,z for a 3D conversion). Taking this value for horiztonal and vertical axes gives you two separate non discrete values of control to work from.