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
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;
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?
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?
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?
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?
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 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
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?
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
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 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😂
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
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.
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?
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
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!
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!
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; } } }
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?)
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🥺
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
Literally the only guide that worked for me. Thanks man!
This guy is goated 🐐🐐
After more than a year still helpful, thanks!
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;
}
}
OMG THANK YOU
Such a clean tut! You are awesome!
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?
You are my hero :) Thank you!
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?
In the canvas component ste it to screen space overlay instead of screen space camera
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.
Yes, I also want to see this with the new input system
Very well explained, clean and concise, thanks buddy
there was NO explanation of what tf he is doing.
NICE NICE. I'LL TRY IT TODAY FOR MY PROJECT AND IT REALLY SOLVED MY PROBLEM. THANKYOUSOMUCH😍🤗
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?
So hard but... Thanks broo, you safe my life LOL
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?
THANK U SOOOOOO MUCHHHHH!!!! I AM INDEBTED TO U, IT WORKS ABSOLUTLEY WONDERFULLY!!!
감사합니다. 11시간 약 5트만에 성공했습니다 . thank you bro your video so lovely thanks
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.
still need help?
@@_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
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?
did u fix it?
Why do no option appear (and two scripts) when i drag the movement script and player script into the character
same
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
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
Yoo i figured it out, it was because two words i accidentally capitalized when they shouldnt have been. That fixed it for me
@@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😂
How do you fix this???
Ive been redoing the same code for a while and never found a solution.
Pls help
@@Centicc make sure your script in unity has the same name as the class
can you PLEASE make a video about how to add a second joystick that will do player shoot?
This is for mobile games right? (Android/IOS)???
Thx u so much finally
Good Tutorial!!! funciona muy bien por si hay gente de habla hispana.
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
Can you make a video that show how to move a 2d character with buttons?
bro i got a error .says that the type or namespace called 'MovementJoystick' could not be found.what do i do now?
Nice, but problem is that i can't add two of them
dude this tutorial awesome like you :D
Thank you a lot, you sawed my game!
please make a joystick tutorial for 3D games
Will you make a video with animation
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.
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?
THANK YOU ANG GALING🎉👏
Thank u for the tutorial!
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
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!
Hey, nice video! how to limit the screen so that it can only be used in the lower left square?
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!
i want to rotate my player what can i do
nice videos and channel! subscribed :)
How to rotate with this joystick?
any idea why my joystick go to bottom left and not center of joystick bg?
im new to c# how to add animation for character where is the horizontal and vertical here
how can i change rotation with given output from the movementjoystick.joystickVec;
Hi, how can I can click on buttons with this joystick if I want to return for menu for example? thanks
looking for the same answer xD
awesome tutorial
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;
}
}
}
thank you!
Really great video
Hey Thats amazing! 60 subs and 1217 views! How'd you do it?!
Hello pls someone help i have a error saying “The name joystickTouchPos does not exist” in movement joystick script
Please upload more, i want to help small dev cuz i want to be a dev too
Thank you. Like 👍
Awesome, thanks a lot
Thank u so much bro.
Thank you!
Very helpful video! I want to move my character just left and right, Can you help me with that?
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
Thank you for your channel. How do you add animation to player script for touch screen?
have you found the solution?
anyone know why mine doesnt return to its orginal position?
Why won't my player move?? I've done everything that's in the video. What else am i missing?? Thank you
Are you sure you added the rigidbody2D and the Player script to the Player object in the Hierarchy?
Can somebody send script from joystick pls :/ When i touch it it just disapears
once ive downloaded the sprites i dont know how to get them in unity
How can i add the down and up walking animation
Can you share the code?
Thx so much pls make More Videos
man, please make it using bolt
How to limit player's range of movement?
very helpful
u should have gave us the code file it says im missing something but i cant find it
Solid brother
Very good video.
Thank u and godbless po
How can I animate the character?
Dude, Thanks for video, BUT where is the source code !?
make the same tutorial for 3D game pls
very nice .....
tysm
Thanks
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?)
Cool👍
thanks
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🥺
MORE VIDS PLS
hi
Thank you !