Skip to content

Commit d720ef0

Browse files
yaustarsteveny-sc
andauthored
Added touchscreen joystick tutorial (#454)
* Outline * WIP * Added buttons section * Update content/en/tutorials/touch-joypad.md * Update content/en/tutorials/touch-joypad.md * Fixed mouse and touch links Co-authored-by: Steven Yau <[email protected]>
1 parent 84365c4 commit d720ef0

13 files changed

+154
-0
lines changed

content/en/tutorials/touch-joypad.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: Touchscreen Joypad Controls
3+
layout: tutorial-page.hbs
4+
tags: input,ui
5+
thumb: https://s3-eu-west-1.amazonaws.com/images.playcanvas.com/projects/12/1007506/2F5E56-image-75.jpg
6+
---
7+
8+
<iframe loading="lazy" src="https://playcanv.as/p/kvE0iJWc/" title="Touchscreen Joypad Controls"></iframe>
9+
10+
[Click here to see the project][project-link].
11+
12+
## Overview
13+
14+
This tutorial shares a Touchscreen Joypad library that can be installed and customized for any project that needs touch screen controls.
15+
16+
We recommend reading about the [PlayCanvas UI system][playcanvas-ui] if you have not used it before as it is used to render and position the controls for this library.
17+
18+
![][preview]
19+
20+
## How to install
21+
22+
Open the [example project][project-link], right click on the folder 'touch-joypad' and click on 'Copy'.
23+
![][copy-folder]
24+
25+
Open your project, right click in the assets panel and click on 'Paste'
26+
![][paste-folder]
27+
28+
## Adding your joystick
29+
30+
In the folder we've just pasted, open the 'templates' folder and there will be 4 preconfigured templates for the joysticks to cover the most common use cases.
31+
32+
* **Left/Right Fixed Touch Joystick** - A fixed joystick that is anchored to the bottom left/right of the screen corners. Recommended for quick reaction games where an instant response is expected such as a retro platforming game.
33+
* **Left/Right Half Touch Joystick** - A joystick where the input area the left/right half of the screen and the joystick will move to where you touch in that area and them requires dragging for movement. Recommended for when 'pushing' or 'pulling' an object in the application.
34+
35+
For this example, let's use the 'Left Half Touch Joystick' template.
36+
37+
Create a 2D screen Entity and [add the template][add-template-docs] as a child of the screen Entity.
38+
39+
![][adding-left-half-joystick]
40+
41+
The joystick is made of three Entities:
42+
43+
- Input area (outlined in red)
44+
- Base (outlined in blue)
45+
- Nub (outlined in blue)
46+
47+
![][joystick-layout]
48+
49+
The input area will listen for touch and mouse (for debugging purposes) events and also stops the propagation of the event.
50+
51+
This means that any UI Elements under this and any [pc.Mouse][pc-app-mouse-api] or [pc.Touch][pc-app-touch-api] events will not be fired if this area is interacted with first.
52+
53+
As the input area is an UI Element, it can be positioned, sized and anchored specifically for your needs via the [UI system layout][elements-manual].
54+
55+
It also has the 'touchJoystick' which has all the logic and attributes for the joystick. Each attribute has tooltips describing what they are for with some more details below.
56+
57+
![][joystick-script-attributes]
58+
59+
The base and nub Entities are controlled by the script and are UI Elements. If you would like the joystick to be in a different place than where it is in the template, please position and anchor the base Entity to where you need them to be and not the input area.
60+
61+
### Behavior Types
62+
63+
There are 3 behavior types for the joystick that are commonly found in touch screen games and applications.
64+
65+
![][joystick-type]
66+
67+
'Fixed in place' where the base of the joystick does not move from it's position:
68+
![][joystick-fixed]
69+
70+
'Move to first touch and fixed' where the base of the joystick moves to where the user first touches in the input area and then stays fixed:
71+
![][joystick-relative-fixed]
72+
73+
'Move to first touch and drags' where the base of the joystick moves to where the user first touches in the input area and then is dragged when the user drags pass the joystick range:
74+
![][joystick-relative-drag]
75+
76+
This can be changed at runtime so it's possible to add this as a user option in the application as part of the in-application settings.
77+
78+
### Reading joystick values
79+
80+
![][joystick-deadzone-range]
81+
82+
The red circle is dead zone and if the nub position is within that circle, the value from the joystick will return 0 for both axes. The blue circle is the range and the nub cannot go outside that circle.
83+
84+
The values are always normalized between -1 and 1 on both axes based on the where the nub is between the dead zone (red circle) and the range (blue circle).
85+
86+
The size of the circles can be set in the Editor via the script attributes.
87+
88+
Joystick values can accessed in code from the global Javascript object `window.touchJoypad.sticks` with the identifier. By default, the identifier is 'joystick0' but can be changed in the Editor on the script to be more specific.
89+
90+
Example code:
91+
92+
```javascript
93+
// Get the joystick by the identifier from the global object
94+
var joystick = window.touchJoypad.sticks['joystick0'];
95+
96+
// Get the normalized values of both joystick axes and print to console
97+
console.log('X: ' + joystick.x ', Y: ' + joystick.y);
98+
```
99+
100+
[In the demo][project-link], the camera is controlled by the right joystick and you can see how it gets and uses the values in the [script here][orbit-camera-joypad-input-script].
101+
102+
103+
## Adding your buttons
104+
105+
Buttons are fixed position UI Elements on the screen. There is a template for button in the 'templates' folder and [should be added][add-template-docs] as a child of the screen Entity.
106+
107+
![][adding-button]
108+
109+
As they are UI Elements, they can be positioned, sized and anchored specifically for your needs via the [UI system layout][elements-manual].
110+
111+
Like the joysticks, they have an identifier so they can be accessed in code from the global Javascript object `window.touchJoypad.buttons` with the following API.
112+
113+
| Function name | Description |
114+
|---------------|-------------|
115+
| isPressed | Takes the button identifier and returns true if the button is currently being pressed. |
116+
| wasPressed | Takes the button identifier and returns true if the button was pressed since the last frame.
117+
| wasReleased | Takes the button identifier and returns true if the button was released since the last frame. |
118+
| wasTapped | Takes the button identifier and returns true if the button was pressed and released within 200ms. i.e. A quick tap. |
119+
120+
The joysticks are also buttons which gives extra flexibility in how they can be used. For example, using the `wasTapped` API with a joystick identifier can act like a L3/R3 input on a PlayStation controller.
121+
122+
Example code:
123+
124+
```javascript
125+
// Get the button global object
126+
var buttons = window.touchJoypad.buttons
127+
128+
// Check if the button was pressed since the last frame
129+
console.log('Was pressed: ' + buttons.wasPressed('button0'));
130+
```
131+
132+
[In the demo][project-link], the character is controlled by the left joystick and buttons. You can see how it gets and uses the buttons in the [script here][player-controller-script] to play attack animations.
133+
134+
135+
[project-link]: https://playcanvas.com/project/1007506/overview/touchscreen-joypad-controls
136+
[playcanvas-ui]: /user-manual/user-interface/
137+
[copy-folder]: /images/tutorials/touchscreen-joypad-controls/copy-folder.gif
138+
[paste-folder]: /images/tutorials/touchscreen-joypad-controls/paste-folder.gif
139+
[preview]: /images/tutorials/touchscreen-joypad-controls/preview.gif
140+
[add-template-docs]: /user-manual/templates/#adding-templates-in-your-scene
141+
[adding-left-half-joystick]: /images/tutorials/touchscreen-joypad-controls/adding-left-half-joystick.gif
142+
[adding-button]: /images/tutorials/touchscreen-joypad-controls/adding-button.gif
143+
[joystick-layout]: /images/tutorials/touchscreen-joypad-controls/joystick-layout.png
144+
[pc-app-mouse-api]: /api/pc.Mouse.html
145+
[pc-app-touch-api]: /api/pc.Touch.html
146+
[elements-manual]: /user-manual/user-interface/elements/
147+
[joystick-script-attributes]: /images/tutorials/touchscreen-joypad-controls/joystick-script-attributes.gif
148+
[joystick-fixed]: /images/tutorials/touchscreen-joypad-controls/joystick-fixed.gif
149+
[joystick-relative-fixed]: /images/tutorials/touchscreen-joypad-controls/joystick-relative-fixed.gif
150+
[joystick-relative-drag]: /images/tutorials/touchscreen-joypad-controls/joystick-relative-drag.gif
151+
[joystick-type]: /images/tutorials/touchscreen-joypad-controls/joystick-type.png
152+
[joystick-deadzone-range]: /images/tutorials/touchscreen-joypad-controls/joystick-deadzone-range.png
153+
[orbit-camera-joypad-input-script]: https://playcanvas.com/editor/code/1007506?tabs=111433673
154+
[player-controller-script]: https://playcanvas.com/editor/code/1007506?tabs=111432679
321 KB
Loading
756 KB
Loading
138 KB
Loading
10.9 KB
Loading
288 KB
Loading
55.8 KB
Loading
131 KB
Loading
182 KB
Loading
568 KB
Loading

0 commit comments

Comments
 (0)