Mobile Joystick 2D Top-Down Game - Easy Unity Tutorial

  Рет қаралды 41,179

Hooson

Hooson

Күн бұрын

In this unity tutorial, you'll learn how to make a joystick touch control for our player movement in a 2d top-down game.
Touch control is an important component for mobile games. We will first add our joystick UI into the game and add a C# script to it. The script allows the joystick to move to the position of the user touch and return back to its original position when user are no longer touching on the screen.
Lastly, we will add another C# script for our player. The script allows our player to move when the user drags on the joystick and It will stop moving when the user is no longer touching on the joystick.
➤Kenny Onscreen Controls: www.kenney.nl/assets/onscreen...
Timestamps:
0:00 Intro
0:21 Joystick UI Setup
1:13 Rigidbody2D Setup
1:32 Joystick Scripting
4:30 Player Scripting
5:20 Conclusion
#Hooson #Unity3D
Play my first mobile game: Bot Attack - 2D Top-down shooter
➤Google Play: play.google.com/store/apps/de...
➤App Store: Coming soon
➤Follow my Instagram: / hoosontech

Пікірлер: 114
@hoosontech
@hoosontech 3 жыл бұрын
Hi guys! I hope this video help! I have a tutorial on 2D Top Down Player Movement! Do check it out if you're making a PC game! kzbin.info/www/bejne/bZfHmJ6qmq5-f6M
@kesisgiyatsu4883
@kesisgiyatsu4883 3 жыл бұрын
Such a clean tut! You are awesome!
@magbabakal708
@magbabakal708 10 ай бұрын
Literally the only guide that worked for me. Thanks man!
@ElCurios
@ElCurios 2 жыл бұрын
After more than a year still helpful, thanks!
@victormr1601
@victormr1601 3 жыл бұрын
Very well explained, clean and concise, thanks buddy
@Terra_Blade
@Terra_Blade Жыл бұрын
there was NO explanation of what tf he is doing.
@dogusipeksac
@dogusipeksac 3 жыл бұрын
dude this tutorial awesome like you :D
@keremaslan1988
@keremaslan1988 2 жыл бұрын
Hi, thank you for this great tutorial but i have a problem. My joystick doesn't scale with screen size although my canvas is set to scale with screen size. What should i do?
@XtrminatR7
@XtrminatR7 5 ай бұрын
This guy is goated 🐐🐐
@tatarin_nbg
@tatarin_nbg 3 жыл бұрын
Thank you a lot, you sawed my game!
@vanching4414
@vanching4414 2 жыл бұрын
Thank u for the tutorial!
@abdullahmuneeb4658
@abdullahmuneeb4658 2 жыл бұрын
THANK U SOOOOOO MUCHHHHH!!!! I AM INDEBTED TO U, IT WORKS ABSOLUTLEY WONDERFULLY!!!
@YapGD
@YapGD 2 жыл бұрын
So hard but... Thanks broo, you safe my life LOL
@jessestone117
@jessestone117 3 жыл бұрын
Really great video
@orkun441
@orkun441 3 жыл бұрын
nice videos and channel! subscribed :)
@95mcqueen5
@95mcqueen5 2 жыл бұрын
Awesome, thanks a lot
@alyzabautista8223
@alyzabautista8223 2 жыл бұрын
NICE NICE. I'LL TRY IT TODAY FOR MY PROJECT AND IT REALLY SOLVED MY PROBLEM. THANKYOUSOMUCH😍🤗
@Karoserigames
@Karoserigames 2 жыл бұрын
awesome tutorial
@riccardodanielli4965
@riccardodanielli4965 6 ай бұрын
Hi, nice quick and simple tutorial thanks, I have a question, do you know how I can do it so that with the movement of the player you can also rotate the player?
@minhtrong4963
@minhtrong4963 2 жыл бұрын
Thank u so much bro.
@mikeybeets3093
@mikeybeets3093 3 ай бұрын
Thank you for this tutorial- I’m having trouble when trying to add my joystick assets to the Source Image slot. It seems as though it’s not compatible. They’re png files. Does that make a difference?
@alyzabautista8223
@alyzabautista8223 2 жыл бұрын
THANK YOU ANG GALING🎉👏
@marcustan6184
@marcustan6184 3 жыл бұрын
Solid brother
@qosku
@qosku 2 жыл бұрын
Good Tutorial!!! funciona muy bien por si hay gente de habla hispana.
@DAMKEON
@DAMKEON 3 жыл бұрын
감사합니다. 11시간 약 5트만에 성공했습니다 . thank you bro your video so lovely thanks
@juan8lebron
@juan8lebron Жыл бұрын
Thank you. Like 👍
@kamikaze8625
@kamikaze8625 3 жыл бұрын
Very good video.
@nicolaeadrian8077
@nicolaeadrian8077 3 жыл бұрын
Thank you !
@0RedTree0
@0RedTree0 3 күн бұрын
I have a question - when I go to "play" after writing the script and hooking it up as seen in the video, the joystick keeps disappearing when I click and drag. I notice the joystick image and joystick BG image changes pos Z for some reason even though the movement joystick does not. What did I do wrong?
@itsdamu
@itsdamu 2 жыл бұрын
please please help me. The event system will not work for me i have tried over 50 tutorials with joysticks and none work. It all works fine no compiling errors but when i click play i cannot move the joystick around at all and it does not go to where i click and like i said it doesnt spin in a circles or anything. please help im at a complete loss
@kingclassic2231
@kingclassic2231 Жыл бұрын
can you PLEASE make a video about how to add a second joystick that will do player shoot?
@user-vn7bg7nj8l
@user-vn7bg7nj8l 7 ай бұрын
I have a question. If my character transitions to a new scene, how can I save the joystick so that it continues to work normally in the new game scene?
@abdallahmehrez4816
@abdallahmehrez4816 2 жыл бұрын
very helpful
@unluckyyooo6598
@unluckyyooo6598 Жыл бұрын
Does anyone know why does my JoystickBackground appears above my mousepoint instead of "around" the mousepoint? so the joystick will always be on the bottom of the background image.
@lolyx-pf9nh
@lolyx-pf9nh 3 жыл бұрын
Thx so much pls make More Videos
@rafcon9141
@rafcon9141 3 жыл бұрын
if anyone is using multitouch and also has the panel that parents the object covering half of the screen, in my case the right part, you will see that when you try it out and touch first the part of the screen that the panel doesnt cover and then holding you touch the one with the panel, you will see that the joystick moves to a position right in the middle of your fingers, because Input.mousePostion does the averege of all Inputs, so what you need is to get Input.touches which is the array of all touches and then figure out how to get the right finger.id by sorting the positions of each input inside the array, either by X or Y (depends in what part of the screen you want the joystick to be), then you will be able to tell which touch is more to the right, left, up or down, again depends of what you want, and you can set your joystick position to that touch position we just distinguished from the others, because that is the one we wanted. It might sound hard if you dont understand where the problem is. Anyway I'll share the code for you to try if you had the same issue I did. This is the solution I came up with. maybe I over thought and the problem was much easier to resolve, if that please let me know hahahaha using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Joystick : MonoBehaviour { // Start is called before the first frame update public GameObject joystick; public GameObject joystickBG; public Vector2 joystickVec; private Vector2 joystickTouchPos; private Vector2 joystickOriginalPos; private float joystickRadius; public float radius = 4; void Start() { joystickOriginalPos = joystickBG.transform.position; joystickRadius = joystickBG.GetComponent().sizeDelta.y / radius; } public void PointerDown() { int i = Input.touches.Length;; if (i == 1) { joystick.transform.position = Input.mousePosition; joystickBG.transform.position = Input.mousePosition; joystickTouchPos = Input.mousePosition; } else { int id = SortArray(); joystick.transform.position = Input.touches[id].position; joystickBG.transform.position = Input.touches[id].position; joystickTouchPos = Input.touches[id].position; } } private int SortArray() { Touch[] inputlist = Input.touches; float[] listpositioninputX = new float[inputlist.Length]; int id = 0; for (int i = 0; i < inputlist.Length; i++) { listpositioninputX[i] = inputlist[i].position.x; } Comparison compare = new Comparison((numero1, numero2) => numero1.CompareTo(numero2)); Array.Sort(listpositioninputX, compare); for (int i = 0; i < inputlist.Length; i++) { if (listpositioninputX[0] == inputlist[i].position.x) { return id = inputlist[i].fingerId; } } return id; } public void Drag(BaseEventData baseEventData) { PointerEventData pointerEventData = baseEventData as PointerEventData; Vector2 dragPos = pointerEventData.position; joystickVec = (dragPos - joystickTouchPos).normalized; float joystickDist = Vector2.Distance(dragPos, joystickTouchPos); if (joystickDist < joystickRadius) { joystick.transform.position = joystickTouchPos + joystickVec * joystickDist; } else { joystick.transform.position = joystickTouchPos + joystickVec * joystickRadius; } } public void PointerUp() { joystickVec = Vector2.zero; joystick.transform.position = joystickOriginalPos; joystickBG.transform.position = joystickOriginalPos; } }
@allanagibson9914
@allanagibson9914 Жыл бұрын
OMG THANK YOU
@EIvinL
@EIvinL 2 жыл бұрын
Can you make a video that show how to move a 2d character with buttons?
@monicardoso724
@monicardoso724 2 жыл бұрын
Thank you for your channel. How do you add animation to player script for touch screen?
@dendymaulana8909
@dendymaulana8909 2 жыл бұрын
have you found the solution?
@darshanchaudhari2865
@darshanchaudhari2865 3 жыл бұрын
very nice .....
@JCruzPorcel0
@JCruzPorcel0 3 жыл бұрын
Hey, nice video! how to limit the screen so that it can only be used in the lower left square?
@hoosontech
@hoosontech 3 жыл бұрын
You can scale your panel size smaller! In the tutorial, my "Movement Joystick" game object has a panel that covers the entire screen. So if you want the joystick to be used only at the bottom half of the screen, simply resize the panel smaller so that it only covers the bottom half of the screen!
@user-tt1tv4qz2o
@user-tt1tv4qz2o 2 жыл бұрын
im new to c# how to add animation for character where is the horizontal and vertical here
@tiranbaghdasaryan243
@tiranbaghdasaryan243 3 жыл бұрын
Cool👍
@mohitguher
@mohitguher 2 жыл бұрын
how can i change rotation with given output from the movementjoystick.joystickVec;
@curiositytube5924
@curiositytube5924 3 жыл бұрын
Hey Thats amazing! 60 subs and 1217 views! How'd you do it?!
@gamebzee
@gamebzee 4 ай бұрын
bro i got a error .says that the type or namespace called 'MovementJoystick' could not be found.what do i do now?
@hakimimdnoor5634
@hakimimdnoor5634 Жыл бұрын
any idea why my joystick go to bottom left and not center of joystick bg?
@faiazbinnesar2937
@faiazbinnesar2937 3 жыл бұрын
Very helpful video! I want to move my character just left and right, Can you help me with that?
@phinion3995
@phinion3995 3 жыл бұрын
I think the easiest way to create two buttons for left and right and assign the move script function of player to the on click of the button
@mnm__studios
@mnm__studios 2 жыл бұрын
please make a joystick tutorial for 3D games
@JoseCruz-qs1pw
@JoseCruz-qs1pw 2 жыл бұрын
Thanks, I would like you to make a tutorial but to make it work with the new input system. Or maybe you can guide me to adapt this to the new system.
@johncarlosmontejo1162
@johncarlosmontejo1162 2 жыл бұрын
Yes, I also want to see this with the new input system
@boombastic1324
@boombastic1324 2 жыл бұрын
Hello pls someone help i have a error saying “The name joystickTouchPos does not exist” in movement joystick script
@pistrantho
@pistrantho 3 жыл бұрын
Please upload more, i want to help small dev cuz i want to be a dev too
@christiansotelohallarsis727
@christiansotelohallarsis727 2 жыл бұрын
Thank u and godbless po
@catezachamane
@catezachamane 5 ай бұрын
tysm
@donutercz
@donutercz 11 ай бұрын
Hey... i have problem... when i try to use the joystick its just disaapears, but i can see when im moving with it that its positions updates. Dont you know how to fix that?
@z.6377
@z.6377 3 ай бұрын
did u fix it?
@PoweR-wk8yw
@PoweR-wk8yw 2 жыл бұрын
Thanks
@tejaskutal2922
@tejaskutal2922 Жыл бұрын
Will you make a video with animation
@Im-a-cool-cuber
@Im-a-cool-cuber 2 жыл бұрын
once ive downloaded the sprites i dont know how to get them in unity
@BrickShot-LEGO-builds
@BrickShot-LEGO-builds 2 жыл бұрын
This is for mobile games right? (Android/IOS)???
@snobbleDobble
@snobbleDobble Жыл бұрын
Why do no option appear (and two scripts) when i drag the movement script and player script into the character
@LitoMike
@LitoMike Жыл бұрын
same
@waflicek
@waflicek Жыл бұрын
Hi, how can I can click on buttons with this joystick if I want to return for menu for example? thanks
@BLK_Blaze
@BLK_Blaze 6 ай бұрын
looking for the same answer xD
@blueempoleon6507
@blueempoleon6507 Жыл бұрын
Help! I'm trying to put some animation on my character but the the transition is not working. The conditions are correct, "if speed is greater than 0.01" but it just stays idle.
@_dimitrisbr
@_dimitrisbr Жыл бұрын
still need help?
@thefrogsheart7670
@thefrogsheart7670 Жыл бұрын
@@_dimitrisbr i need help with the fact that the joystick images dont show up when i click but it works as intended, the weird thing is that it shows if i release my finger but disappear when i click anywhere lmao wtf
@Arteka81
@Arteka81 Жыл бұрын
How can i add the down and up walking animation
@ImGheorghe
@ImGheorghe 2 жыл бұрын
thanks
@comevodepetista345
@comevodepetista345 3 жыл бұрын
someone could explain me why my joystick don't appears on my play mode? and that's not cuz the pos Z, the z position of all objects on my scene is zero
@hoosontech
@hoosontech 3 жыл бұрын
Hi! I think you need to anchor your joystick UI correctly. Check on your joystick UI's Rect Transform, make sure the anchor point for your joystick UI is anchored to the bottom of the screen!
@targetblastsgaming202
@targetblastsgaming202 Жыл бұрын
i want to rotate my player what can i do
@bartoszzap2106
@bartoszzap2106 2 жыл бұрын
How to rotate with this joystick?
@Briezelta
@Briezelta 2 жыл бұрын
anyone know why mine doesnt return to its orginal position?
@bluestepstudios
@bluestepstudios 2 жыл бұрын
u should have gave us the code file it says im missing something but i cant find it
@twikin3577
@twikin3577 2 жыл бұрын
3:49 it says "Can't add script component ‘MovementJoystick’ because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match" Please help
@nathanael9522
@nathanael9522 2 жыл бұрын
I am having the same issue, also I used notepad to edit and make the script. I am not sure if that is why I am having the issue. Another reason for that issue in the line "public class MovementJoystick : MonoBehaviour". Make sure the public class is called "MovementJoystick" because it is case sensitive
@nathanael9522
@nathanael9522 2 жыл бұрын
Yoo i figured it out, it was because two words i accidentally capitalized when they shouldnt have been. That fixed it for me
@twikin3577
@twikin3577 2 жыл бұрын
@@nathanael9522 i dont exaxtly remember it its long time ago but i think i just had different name of file in Unity and in that script, 2 hours of smashing my head to wall and 2 seconds to solve it😂
@Centicc
@Centicc Жыл бұрын
How do you fix this??? Ive been redoing the same code for a while and never found a solution. Pls help
@twikin3577
@twikin3577 Жыл бұрын
@@Centicc make sure your script in unity has the same name as the class
@user-jm4gh3lo6r
@user-jm4gh3lo6r Жыл бұрын
Can somebody send script from joystick pls :/ When i touch it it just disapears
@khuongtunha2365
@khuongtunha2365 2 жыл бұрын
How to limit player's range of movement?
@nexoknechthd619
@nexoknechthd619 2 жыл бұрын
How can I animate the character?
@user-ft9sj6rn7b
@user-ft9sj6rn7b 5 ай бұрын
Nice, but problem is that i can't add two of them
@oncenabong2758
@oncenabong2758 3 жыл бұрын
line 20 in player script error CS1061: 'MovementJoystick' does not contain a definition for 'y' and no accessible extension method 'y' accepting a first argument of type 'MovementJoystick' could be found (are you missing a using directive or an assembly reference?)
@ralphjohncandolita6224
@ralphjohncandolita6224 2 жыл бұрын
Why won't my player move?? I've done everything that's in the video. What else am i missing?? Thank you
@ThatTartToter
@ThatTartToter 2 жыл бұрын
Are you sure you added the rigidbody2D and the Player script to the Player object in the Hierarchy?
@mnm__studios
@mnm__studios 2 жыл бұрын
make the same tutorial for 3D game pls
@the_sophile
@the_sophile 2 жыл бұрын
Can you share the code?
@dreamloggames978
@dreamloggames978 3 жыл бұрын
man, please make it using bolt
@amirmohammadyazdani6929
@amirmohammadyazdani6929 2 жыл бұрын
Dude, Thanks for video, BUT where is the source code !?
@chermainehoo214
@chermainehoo214 3 жыл бұрын
MORE VIDS PLS
@rink369
@rink369 3 жыл бұрын
hi
@devsack
@devsack 3 жыл бұрын
I have an 32-bit computer with 8GB RAM I'm behind in all of the projects and your knowledge , I just given up today 😔 Can anyone let me suggest what to do if I have an 32-bit computer with 32-bit Unity🥺
@jbefteam
@jbefteam 11 ай бұрын
not working!😡😡😡😡😡
@vipeyboii
@vipeyboii 6 ай бұрын
If you still want help, I can try to assist you
@BLK_Blaze
@BLK_Blaze 6 ай бұрын
@@vipeyboiidid you find a way for click UI buttons with this joystick system?
@vipeyboii
@vipeyboii 6 ай бұрын
@@BLK_Blaze no I ended up using Unity's new input system to make a mobile joystick much easier
@astheatic
@astheatic 2 жыл бұрын
if you want that the player looks at the direction he is moving replace your player script with this code (dont forget to change script name) using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerScript : MonoBehaviour { public JoystickScript joystickScript; public float speed; private Rigidbody2D rb; // Start is called before the first frame update void Start() { rb = GetComponent(); } private void FixedUpdate() { if(joystickScript.joystickVec.y != 0) { rb.velocity = new Vector2(joystickScript.joystickVec.x * speed, joystickScript.joystickVec.y * speed); //character looks at the direction he is moving transform.rotation = Quaternion.LookRotation(Vector3.forward, joystickScript.joystickVec); } else { rb.velocity = Vector2.zero; } } }
@mohitguher
@mohitguher 2 жыл бұрын
thank you!
@Dark-mn3db
@Dark-mn3db Жыл бұрын
Thank you!
TOP DOWN MOVEMENT in Unity!
22:30
Brackeys
Рет қаралды 1,2 МЛН
Unity3D/2D: Mobile Joystick Tutorial [NO PLUGINS]
10:17
Waldo
Рет қаралды 95 М.
когда достали одноклассники!
00:49
БРУНО
Рет қаралды 3,5 МЛН
格斗裁判暴力执法!#fighting #shorts
00:15
武林之巅
Рет қаралды 91 МЛН
Optimize UI For Notch Devices - Easy Unity Tutorial
4:32
Hooson
Рет қаралды 22 М.
I Made A Difficult Game About Climbing
15:04
Pontypants
Рет қаралды 1,9 МЛН
QUICKLY Implement Joystick For Mobile Games In Unity (2023)
6:25
Making a VIRUS for a Game Jam (and almost winning)
9:36
Deynum Studio
Рет қаралды 243 М.
i got laid off... so i made a game...
24:08
bewky
Рет қаралды 597 М.
Easy Joystick for Mobile with Input System in Unity
7:41
samyam
Рет қаралды 138 М.
Adding BGM And On/Off Sound Button - Easy Unity Tutorial
5:56
ФОТОШОП СВОИМИ РУКАМИ (@photoscapesbyclare - IG)
0:30
В ТРЕНДЕ
Рет қаралды 18 МЛН
CORRUGATED CARDBOARD KENKENPA!#shorts
0:19
HAYATAKU はやたく
Рет қаралды 11 МЛН
#лайфхак #лайфхаки #советы #viral
1:01
Юра и Вика Лайфхаки
Рет қаралды 4,9 МЛН