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

แชร์
ฝัง

ความคิดเห็น • 117

  • @hoosontech
    @hoosontech  3 ปีที่แล้ว +5

    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! th-cam.com/video/7fdgkteIKIA/w-d-xo.html

  • @magbabakal708
    @magbabakal708 ปีที่แล้ว +1

    Literally the only guide that worked for me. Thanks man!

  • @XtrminatR7
    @XtrminatR7 10 หลายเดือนก่อน +1

    This guy is goated 🐐🐐

  • @ElCurios
    @ElCurios 2 ปีที่แล้ว +1

    After more than a year still helpful, thanks!

  • @rafcon9141
    @rafcon9141 3 ปีที่แล้ว +12

    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;
    }

    }

  • @kesisgiyatsu4883
    @kesisgiyatsu4883 3 ปีที่แล้ว +4

    Such a clean tut! You are awesome!

  • @keremaslan1988
    @keremaslan1988 3 ปีที่แล้ว +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?

  • @duck730
    @duck730 3 หลายเดือนก่อน

    You are my hero :) Thank you!

  • @0RedTree0
    @0RedTree0 4 หลายเดือนก่อน +1

    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?

    • @gameplusoneaa
      @gameplusoneaa 21 วันที่ผ่านมา +1

      In the canvas component ste it to screen space overlay instead of screen space camera

  • @JoseCruz-qs1pw
    @JoseCruz-qs1pw 3 ปีที่แล้ว +4

    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 ปีที่แล้ว +2

      Yes, I also want to see this with the new input system

  • @victormr1601
    @victormr1601 3 ปีที่แล้ว +1

    Very well explained, clean and concise, thanks buddy

    • @Terra_Blade
      @Terra_Blade ปีที่แล้ว

      there was NO explanation of what tf he is doing.

  • @alyzabautista8223
    @alyzabautista8223 3 ปีที่แล้ว

    NICE NICE. I'LL TRY IT TODAY FOR MY PROJECT AND IT REALLY SOLVED MY PROBLEM. THANKYOUSOMUCH😍🤗

  • @riccardodanielli4965
    @riccardodanielli4965 10 หลายเดือนก่อน

    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?

  • @YapGD
    @YapGD 3 ปีที่แล้ว +1

    So hard but... Thanks broo, you safe my life LOL

  • @mikeybeets3093
    @mikeybeets3093 8 หลายเดือนก่อน

    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?

  • @abdullahmuneeb4658
    @abdullahmuneeb4658 2 ปีที่แล้ว

    THANK U SOOOOOO MUCHHHHH!!!! I AM INDEBTED TO U, IT WORKS ABSOLUTLEY WONDERFULLY!!!

  • @DAMKEON
    @DAMKEON 3 ปีที่แล้ว +2

    감사합니다. 11시간 약 5트만에 성공했습니다 . thank you bro your video so lovely thanks

  • @blueempoleon6507
    @blueempoleon6507 ปีที่แล้ว +1

    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

  • @donutercz
    @donutercz ปีที่แล้ว +1

    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 8 หลายเดือนก่อน

      did u fix it?

  • @snobbleDobble
    @snobbleDobble ปีที่แล้ว +1

    Why do no option appear (and two scripts) when i drag the movement script and player script into the character

  • @twikin3577
    @twikin3577 2 ปีที่แล้ว +4

    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

  • @kingclassic2231
    @kingclassic2231 2 ปีที่แล้ว

    can you PLEASE make a video about how to add a second joystick that will do player shoot?

  • @BrickShot-LEGO-builds
    @BrickShot-LEGO-builds 2 ปีที่แล้ว +1

    This is for mobile games right? (Android/IOS)???

  • @iop313
    @iop313 4 วันที่ผ่านมา

    Thx u so much finally

  • @qosku
    @qosku 2 ปีที่แล้ว

    Good Tutorial!!! funciona muy bien por si hay gente de habla hispana.

  • @itsdamu
    @itsdamu 3 ปีที่แล้ว

    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

  • @EIvinL
    @EIvinL 3 ปีที่แล้ว +1

    Can you make a video that show how to move a 2d character with buttons?

  • @gamebzee
    @gamebzee 8 หลายเดือนก่อน

    bro i got a error .says that the type or namespace called 'MovementJoystick' could not be found.what do i do now?

  • @IlijaLuzheckij
    @IlijaLuzheckij 9 หลายเดือนก่อน

    Nice, but problem is that i can't add two of them

  • @dogusipeksac
    @dogusipeksac 3 ปีที่แล้ว

    dude this tutorial awesome like you :D

  • @tatarin_nbg
    @tatarin_nbg 3 ปีที่แล้ว

    Thank you a lot, you sawed my game!

  • @itsthemeg
    @itsthemeg 3 ปีที่แล้ว +1

    please make a joystick tutorial for 3D games

  • @tejaskutal2922
    @tejaskutal2922 ปีที่แล้ว

    Will you make a video with animation

  • @unluckyyooo6598
    @unluckyyooo6598 2 ปีที่แล้ว

    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.

  • @MinhTuấnĐỗ-c3k
    @MinhTuấnĐỗ-c3k ปีที่แล้ว

    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?

  • @alyzabautista8223
    @alyzabautista8223 3 ปีที่แล้ว

    THANK YOU ANG GALING🎉👏

  • @vanching4414
    @vanching4414 2 ปีที่แล้ว

    Thank u for the tutorial!

  • @comevodepetista345
    @comevodepetista345 3 ปีที่แล้ว +1

    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!

  • @JCruzPorcel0
    @JCruzPorcel0 3 ปีที่แล้ว +1

    Hey, nice video! how to limit the screen so that it can only be used in the lower left square?

    • @hoosontech
      @hoosontech  3 ปีที่แล้ว +4

      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!

  • @targetblastsgaming202
    @targetblastsgaming202 2 ปีที่แล้ว +1

    i want to rotate my player what can i do

  • @orkun441
    @orkun441 3 ปีที่แล้ว

    nice videos and channel! subscribed :)

  • @bartoszzap2106
    @bartoszzap2106 2 ปีที่แล้ว +1

    How to rotate with this joystick?

  • @hakimimdnoor5634
    @hakimimdnoor5634 ปีที่แล้ว

    any idea why my joystick go to bottom left and not center of joystick bg?

  • @user-tt1tv4qz2o
    @user-tt1tv4qz2o 2 ปีที่แล้ว

    im new to c# how to add animation for character where is the horizontal and vertical here

  • @mohitguher
    @mohitguher 2 ปีที่แล้ว

    how can i change rotation with given output from the movementjoystick.joystickVec;

  • @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 11 หลายเดือนก่อน

      looking for the same answer xD

  • @Karoserigames
    @Karoserigames 2 ปีที่แล้ว

    awesome tutorial

  • @paxionlight
    @paxionlight 2 ปีที่แล้ว +5

    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 ปีที่แล้ว +2

      thank you!

  • @jessestone117
    @jessestone117 3 ปีที่แล้ว

    Really great video

  • @curiositytube5924
    @curiositytube5924 3 ปีที่แล้ว +1

    Hey Thats amazing! 60 subs and 1217 views! How'd you do it?!

  • @boombastic1324
    @boombastic1324 3 ปีที่แล้ว

    Hello pls someone help i have a error saying “The name joystickTouchPos does not exist” in movement joystick script

  • @pistrantho
    @pistrantho 3 ปีที่แล้ว +1

    Please upload more, i want to help small dev cuz i want to be a dev too

  • @khaelus_dev
    @khaelus_dev ปีที่แล้ว

    Thank you. Like 👍

  • @95mcqueen5
    @95mcqueen5 2 ปีที่แล้ว

    Awesome, thanks a lot

  • @minhtrong4963
    @minhtrong4963 3 ปีที่แล้ว

    Thank u so much bro.

  • @Darkziss0
    @Darkziss0 ปีที่แล้ว

    Thank you!

  • @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

  • @monicardoso724
    @monicardoso724 3 ปีที่แล้ว

    Thank you for your channel. How do you add animation to player script for touch screen?

  • @Briezelta
    @Briezelta 2 ปีที่แล้ว

    anyone know why mine doesnt return to its orginal position?

  • @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 3 ปีที่แล้ว

      Are you sure you added the rigidbody2D and the Player script to the Player object in the Hierarchy?

  • @PavloZhdanov-u8s
    @PavloZhdanov-u8s 2 ปีที่แล้ว

    Can somebody send script from joystick pls :/ When i touch it it just disapears

  • @Im-a-cool-cuber
    @Im-a-cool-cuber 2 ปีที่แล้ว

    once ive downloaded the sprites i dont know how to get them in unity

  • @Arteka81
    @Arteka81 ปีที่แล้ว

    How can i add the down and up walking animation

  • @the_sophile
    @the_sophile 2 ปีที่แล้ว +1

    Can you share the code?

  • @lolyx-pf9nh
    @lolyx-pf9nh 4 ปีที่แล้ว +1

    Thx so much pls make More Videos

  • @dreamloggames978
    @dreamloggames978 3 ปีที่แล้ว +1

    man, please make it using bolt

  • @khuongtunha2365
    @khuongtunha2365 2 ปีที่แล้ว

    How to limit player's range of movement?

  • @abdallahmehrez4816
    @abdallahmehrez4816 3 ปีที่แล้ว

    very helpful

  • @bluestepstudios
    @bluestepstudios 3 ปีที่แล้ว

    u should have gave us the code file it says im missing something but i cant find it

  • @marcustan6184
    @marcustan6184 4 ปีที่แล้ว

    Solid brother

  • @ArravTwitch
    @ArravTwitch 3 ปีที่แล้ว

    Very good video.

  • @christiansotelohallarsis727
    @christiansotelohallarsis727 2 ปีที่แล้ว

    Thank u and godbless po

  • @nexoknechthd619
    @nexoknechthd619 3 ปีที่แล้ว

    How can I animate the character?

  • @amirmohammadyazdani6929
    @amirmohammadyazdani6929 2 ปีที่แล้ว

    Dude, Thanks for video, BUT where is the source code !?

  • @itsthemeg
    @itsthemeg 3 ปีที่แล้ว

    make the same tutorial for 3D game pls

  • @darshanchaudhari2865
    @darshanchaudhari2865 4 ปีที่แล้ว

    very nice .....

  • @catezachamane
    @catezachamane 10 หลายเดือนก่อน

    tysm

  • @PoweR-wk8yw
    @PoweR-wk8yw 2 ปีที่แล้ว

    Thanks

  • @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?)

  • @cryptonewsen1
    @cryptonewsen1 3 ปีที่แล้ว

    Cool👍

  • @ImGheorghe
    @ImGheorghe 3 ปีที่แล้ว

    thanks

  • @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🥺

  • @chermainehoo214
    @chermainehoo214 4 ปีที่แล้ว

    MORE VIDS PLS

  • @rink369
    @rink369 3 ปีที่แล้ว

    hi

  • @nicolaeadrian8077
    @nicolaeadrian8077 3 ปีที่แล้ว

    Thank you !