How To Move with UI Buttons in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys it is me judge again with another video today i'll be making movement buttons in unity if you'd like to see how to make them just click on the end of the video so first we'll have to go and add a ground which is going to be square and name it crown and add a rigid not a ridge body box collided 2d to it and let's scale it out a bit to match it's not really matching but that's okay and let's add a player my player for now is going to be a square for some reason but let's name it player there we go and let's add a box rtb as well and add a rigid body rigid body to it there we go and let's we just i can't do anything at the moment there go and add it right there and if we press play right now the object just falls which is pretty nice i guess but you can't move the object which is very boring if you want if you want to make a game and the object kind of has to move to make it fun okay now we're gonna add a now we're gonna change the color of this not add anything because we can see so we don't mix up the player with the ground because with them are white the color frame we want okay next in the player we're gonna have to add a script which is called move new script and create an ad okay then we're gonna press oh okay visual city code is opened and the first line of code we should have is private rigidbody rigid body 2d and rb there we go next line should be private bool move let me call move right now move left and the other one is going to move right let's copy the same thing from here if you would like private bool move left and change move left to right okay next line should be private float private float horizontal move that's a floss there you go horizontal move so i can spell and public float there we go speed equals five okay we're gonna start coding and the void start now first gonna have rb equals get component to arrows like this and have rigidbody from here oh add to and we're going to have another semicolon next two lines is going to be move left equals false move right equals false in the semicolon okay after we've finished with it that line we're going to have another line of code which says public void pointer down left and we're gonna have move left equals true and we're going to switch another live code with same private no public void pointer up left this time and we're gonna have move left equals false underline which says public move public void pointer down right have move right equals true and another line of code which says public void pointer upright this time there we go i'm gonna have move right equals false in a semicolon okay once you have all that then we're gonna move on to another line which is avoid updates we're gonna have move player equals not equals just brackets like that and a semicolon [Music] another line of code which is gonna cheat see if the player is moving i'm gonna have private void move player if i can actually get there there we go and brackets that have a if statement if move left okay horizontal move equals minus speed and another line which set which is an else if statement else if there we go else if move right i'm going to have horizontal move equals speed okay there we go and we're gonna have another code line or a line of code which is gonna take action when i'm not pressing any buttons it's going to be an l script l statement which is horizontal move equals 0 and semicolon okay we're going to move on to our last line of code let me just do that and it's going to be a public void public void we're not public private private void fixed update and we're gonna have brackets and rb dot velocity equals new at reference vector 2 vector 2 brackets horizontal move rb rb dot velocity again equals not equals dot dot y it's going to be moving on the y axis i have a so that should be the movement code done now we will hop into unity and test it okay we are in unity again and if i go into the player and i scroll down to move script i'm going to see the speed of my player as satisfied because it's normally it's going to be satisfied because that's what we said in the public or private float what we did in the code it was a public flow yes now we're going to add ui buttons we're going to head to ui and canvas and if i can go to canvas right now there you go and we're going to add buttons where is the uigen bun and if you can scale out a bit like this and there we go and duplicate again and just change those in the names of them it's going to be right button there we go and the other is going to be called left button that there you go okay we're going to change the text of them because we kind of need do change the techniques change text because you can't just have button the player in the game won't know what to do with it and it's going to be left and we're going to set it to best fit there we go and right for another text there we go okay once you have these buttons done we're going to change this to best fit again and yeah as we have these two buttons done we're going to go into the left button and add a not let's not us at event trigger if it get event trigger and add new event type it's going to be pointer down there go and drag the player then we're going to have move script pointer down left another event type called pointer up i have player players already in that and we're gonna have change to move to pointer up left here we go i'm gonna head into the right button when i have the same thing but change the left to right okay pointer down and plus drag the player and change the move to pointer down right new event type pointer up and change this to pointer upright there we go and this should be it if i press playground and play will fall and i can move right move left or right so this is the video i hope you enjoyed it make sure to like and subscribe and goodbye [Music]
Info
Channel: Jadtru
Views: 19,814
Rating: undefined out of 5
Keywords: Unity, tutorials, 2021, help, pastebin, move, captions, move with ui buttons in unity
Id: I4EzjvVR6Xw
Channel Id: undefined
Length: 10min 41sec (641 seconds)
Published: Thu Sep 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.