Unity3D/2D: Mobile Joystick Tutorial [NO PLUGINS]

preview_player
Показать описание
In this tutorial I explain how to code a virtual joystick in Unity to be used for mobile or touch screen devices. In this video you will learn:

+ 0:52 - Create a new C# script
+ 1:08 - Create a moveCharacter function
+ 2:50 - Move with arrow keys or physical joystick
+ 3:38 - How to calculate direction
+ 5:08 - Mouse Position - Screen View vs. World View
+ 7:50 - Setup a Visual Joystick
+ 9:45 - Final Product

Download Assets:

Copy & Paste Code Here:

For more Unity tutorials:
Рекомендации по теме
Комментарии
Автор

This is the waldo everyone is searching for. Clear tutorial and simple codes to follow along. Pls make more mobile tutorials, it's what we need right now

ramoncababaan
Автор

Dude this tutorial awesome. I know not much English but I understand many information on this video. Thank you.

menesekinci
Автор

Note: this is for inverse mouse input or inverse touch control;
Vector2 offset = pointB - pointA;
Change it get proper direction movement;
Vector2 offset = pointA - pointB;

aimesolomon
Автор

for those who are having inverted Controls remove the -1 at the circles and add +1 at the movement
circle.transform.position = pointA;
(remove negative)
= pointA; (remove negative)


moveCharacter(direction * +1); (add positive)

janjancababaan
Автор

I dont usually comment, but this is probably the best mobile touch control tutorial on youtube. Hands down!

codebrown
Автор

I've never seen such a great tutorial about touch control. Keep up the good work!

kick
Автор

oh my god, after a so long time i found a great tutorial.
Thx dude

dibbelgames
Автор

I rarely ever like and comment videos, but this one is so well made, I just had to.

andrewferguson
Автор

Thank you so much, I was tired of other tutorials, None that i tried was working.

IFennecYouCODM
Автор

hello, I read all the comments below, poked around and found a great solution. (removed "* -1") and everything began to work as it should.
Also thanks for such a good video.
P.S not tested on phone yet.

Edited Code:
using UnityEngine;

public class Joystick : MonoBehaviour {
public Transform player;
public float speed = 5.0f;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;

public Transform circle;
public Transform outerCircle;

void Update() {
if (Input.GetMouseButtonDown(0)) {
pointA = Vector3(Input.mousePosition.x, Input.mousePosition.y,

circle.transform.position = pointA;
= pointA;
= true;
= true;
}
if (Input.GetMouseButton(0)) {
touchStart = true;
pointB = Vector3(Input.mousePosition.x, Input.mousePosition.y,
}
else {
touchStart = false;
}
}
private void FixedUpdate() {
if (touchStart) {
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction);

circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y);
}
else {
= false;
= false;
}
}
void moveCharacter(Vector2 direction) {
player.Translate(direction * speed * Time.deltaTime);
}
}

_moonbeam
Автор

THANKYOU SO MUCH!! Btw I got rid of all the * -1 bits because it just put the joystick in the wrong place and made you go the opposite direction but anyway this was the best video I have ever watched for explaining this!! btw I subbed

StolenGalaxy
Автор

OIE! I changed some of the code for it to work right. Line 29 = pointA * 1; Line 52 circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * 1; I took out the Negative (-) before the 1s and the joystick now follows my screen instead of being on opposite side of where i click/push. Also would love a Fixed joystick in one spot and the joystick snapping back to starting position when letting go.

LavaStormPlays
Автор

Great video, very simple and clean. Liked the approach

BBdaCosta
Автор

Your tutorials are great! Plz keep continue making them.:)

ElectronGOD
Автор

Nice video, Waldo please create a video about VR-games

JungleGAS_
Автор

That was very helpful for me
Thanks for your tutorial video
It's very simple to know how it working

SamuelTsai_
Автор

Is it possible to change this code for a 3D setup?

AaronFormyduval
Автор

I will try this code once I come back from marketing one of my apps to some retail stores

lajocymbalski
Автор

you are a good person, thank you for helping me to learn unity

lankahelth
Автор

Hi, Very informative tutorial. Straight to point. And clean as well. I'd like to implement the same "virtual joystick" for rotating a sprite on its z axis. Also so that when you've rotated the object, the next time i rotate it, it rotates from the current rotation to the desired direction! I've tried to change the code as per my knowledge and failed. would you be kind enough to help me out? thanks!

tgtgtgtgtgtg