Тёмный
No video :(

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

Hooson
Подписаться 7 тыс.
Просмотров 42 тыс.
50% 1

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/...
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.co...
➤App Store: Coming soon
➤Follow my Instagram: / hoosontech

Опубликовано:

 

17 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 115   
@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! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-7fdgkteIKIA.html
@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
@magbabakal708
@magbabakal708 Год назад
Literally the only guide that worked for me. Thanks man!
@ElCurios
@ElCurios 2 года назад
After more than a year still helpful, thanks!
@XtrminatR7
@XtrminatR7 7 месяцев назад
This guy is goated 🐐🐐
@kesisgiyatsu4883
@kesisgiyatsu4883 3 года назад
Such a clean tut! You are awesome!
@0RedTree0
@0RedTree0 2 месяца назад
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?
@duck730
@duck730 Месяц назад
You are my hero :) Thank you!
@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
@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.
@keremaslan1988
@keremaslan1988 3 года назад
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?
@donutercz
@donutercz Год назад
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 5 месяцев назад
did u fix it?
@gamebzee
@gamebzee 6 месяцев назад
bro i got a error .says that the type or namespace called 'MovementJoystick' could not be found.what do i do now?
@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
@DAMKEON
@DAMKEON 3 года назад
감사합니다. 11시간 약 5트만에 성공했습니다 . thank you bro your video so lovely thanks
@JoseCruz-qs1pw
@JoseCruz-qs1pw 3 года назад
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 3 года назад
Yes, I also want to see this with the new input system
@alyzabautista8223
@alyzabautista8223 2 года назад
NICE NICE. I'LL TRY IT TODAY FOR MY PROJECT AND IT REALLY SOLVED MY PROBLEM. THANKYOUSOMUCH😍🤗
@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!
@YapGD
@YapGD 3 года назад
So hard but... Thanks broo, you safe my life LOL
@abdullahmuneeb4658
@abdullahmuneeb4658 2 года назад
THANK U SOOOOOO MUCHHHHH!!!! I AM INDEBTED TO U, IT WORKS ABSOLUTLEY WONDERFULLY!!!
@mikeybeets3093
@mikeybeets3093 5 месяцев назад
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?
@user-vn7bg7nj8l
@user-vn7bg7nj8l 10 месяцев назад
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?
@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!
@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.
@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
@qosku
@qosku 2 года назад
Good Tutorial!!! funciona muy bien por si hay gente de habla hispana.
@tatarin_nbg
@tatarin_nbg 3 года назад
Thank you a lot, you sawed my game!
@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
@dogusipeksac
@dogusipeksac 3 года назад
dude this tutorial awesome like you :D
@targetblastsgaming202
@targetblastsgaming202 2 года назад
i want to rotate my player what can i do
@hakimimdnoor5634
@hakimimdnoor5634 Год назад
any idea why my joystick go to bottom left and not center of joystick bg?
@riccardodanielli4965
@riccardodanielli4965 8 месяцев назад
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?
@mnm__studios
@mnm__studios 2 года назад
please make a joystick tutorial for 3D games
@mohitguher
@mohitguher 2 года назад
how can i change rotation with given output from the movementjoystick.joystickVec;
@user-tt1tv4qz2o
@user-tt1tv4qz2o 2 года назад
im new to c# how to add animation for character where is the horizontal and vertical here
@EIvinL
@EIvinL 2 года назад
Can you make a video that show how to move a 2d character with buttons?
@bartoszzap2106
@bartoszzap2106 2 года назад
How to rotate with this joystick?
@boombastic1324
@boombastic1324 2 года назад
Hello pls someone help i have a error saying “The name joystickTouchPos does not exist” in movement joystick script
@alyzabautista8223
@alyzabautista8223 2 года назад
THANK YOU ANG GALING🎉👏
@vanching4414
@vanching4414 2 года назад
Thank u for the tutorial!
@tejaskutal2922
@tejaskutal2922 Год назад
Will you make a video with animation
@user-ft9sj6rn7b
@user-ft9sj6rn7b 7 месяцев назад
Nice, but problem is that i can't add two of them
@kingclassic2231
@kingclassic2231 Год назад
can you PLEASE make a video about how to add a second joystick that will do player shoot?
@user-jm4gh3lo6r
@user-jm4gh3lo6r Год назад
Can somebody send script from joystick pls :/ When i touch it it just disapears
@Karoserigames
@Karoserigames 2 года назад
awesome tutorial
@orkun441
@orkun441 3 года назад
nice videos and channel! subscribed :)
@BrickShot-LEGO-builds
@BrickShot-LEGO-builds 2 года назад
This is for mobile games right? (Android/IOS)???
@lolyx-pf9nh
@lolyx-pf9nh 3 года назад
Thx so much pls make More Videos
@Im-a-cool-cuber
@Im-a-cool-cuber 2 года назад
once ive downloaded the sprites i dont know how to get them in unity
@pistrantho
@pistrantho 3 года назад
Please upload more, i want to help small dev cuz i want to be a dev too
@Arteka81
@Arteka81 Год назад
How can i add the down and up walking animation
@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 8 месяцев назад
looking for the same answer xD
@95mcqueen5
@95mcqueen5 2 года назад
Awesome, thanks a lot
@jessestone117
@jessestone117 3 года назад
Really great video
@Darkziss0
@Darkziss0 Год назад
Thank you!
@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!
@marcustan6184
@marcustan6184 3 года назад
Solid brother
@juanan_zzz
@juanan_zzz Год назад
Thank you. Like 👍
@minhtrong4963
@minhtrong4963 3 года назад
Thank u so much bro.
@ArravTwitch
@ArravTwitch 3 года назад
Very good video.
@Briezelta
@Briezelta 2 года назад
anyone know why mine doesnt return to its orginal position?
@monicardoso724
@monicardoso724 3 года назад
Thank you for your channel. How do you add animation to player script for touch screen?
@dendymaulana8909
@dendymaulana8909 2 года назад
have you found the solution?
@ralphjohncandolita6224
@ralphjohncandolita6224 3 года назад
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?
@bluestepstudios
@bluestepstudios 3 года назад
u should have gave us the code file it says im missing something but i cant find it
@the_sophile
@the_sophile 2 года назад
Can you share the code?
@nexoknechthd619
@nexoknechthd619 3 года назад
How can I animate the character?
@abdallahmehrez4816
@abdallahmehrez4816 2 года назад
very helpful
@catezachamane
@catezachamane 7 месяцев назад
tysm
@dreamloggames978
@dreamloggames978 3 года назад
man, please make it using bolt
@khuongtunha2365
@khuongtunha2365 2 года назад
How to limit player's range of movement?
@curiositytube5924
@curiositytube5924 3 года назад
Hey Thats amazing! 60 subs and 1217 views! How'd you do it?!
@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
@christiansotelohallarsis727
@christiansotelohallarsis727 2 года назад
Thank u and godbless po
@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?)
@darshanchaudhari2865
@darshanchaudhari2865 3 года назад
very nice .....
@cryptonewsen1
@cryptonewsen1 3 года назад
Cool👍
@PoweR-wk8yw
@PoweR-wk8yw 2 года назад
Thanks
@ImGheorghe
@ImGheorghe 3 года назад
thanks
@mnm__studios
@mnm__studios 2 года назад
make the same tutorial for 3D game pls
@amirmohammadyazdani6929
@amirmohammadyazdani6929 2 года назад
Dude, Thanks for video, BUT where is the source code !?
@chermainehoo214
@chermainehoo214 3 года назад
MORE VIDS PLS
@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🥺
@rink369
@rink369 3 года назад
hi
@g.o.b.z
@g.o.b.z Год назад
not working!😡😡😡😡😡
@vipeyboii
@vipeyboii 9 месяцев назад
If you still want help, I can try to assist you
@BLK_Blaze
@BLK_Blaze 8 месяцев назад
@@vipeyboiidid you find a way for click UI buttons with this joystick system?
@vipeyboii
@vipeyboii 8 месяцев назад
@@BLK_Blaze no I ended up using Unity's new input system to make a mobile joystick much easier
@nicolaeadrian8077
@nicolaeadrian8077 3 года назад
Thank you !
Далее
I Made the Same Game in 8 Engines
12:34
Просмотров 4 млн
7 Days Stranded In A Cave
17:59
Просмотров 37 млн
1 Year of Learning Game Development In 6 Minutes
6:01
Unity3D/2D: Mobile Joystick Tutorial [NO PLUGINS]
10:17
How to Make a Good 2D Camera
11:38
Просмотров 398 тыс.
Optimize UI For Notch Devices - Easy Unity Tutorial
4:32
The Making of Karlson 2D (Game)
11:41
Просмотров 6 млн
10 Minutes vs. 10 Years of Animation
19:29
Просмотров 834 тыс.
I Made A Difficult Game About Climbing
15:04
Просмотров 2 млн
How I Made a 3D Platformer in 2D Game Engine
21:23
Просмотров 467 тыс.