Why are we using something for 3D in 2D, well in short unity still works in 3D even when using 2D it just shows us the 3D world in 2D. A Vector3 is more than just a variable, it is a position or direction in 3D space. Next we create a Vector3 called directionOfMovement which will hold both our horizontal and vertical movement in a single variable. Without using ltaTime we will move a whole seconds worth of movement in a single frame.
UNITY 2019 2D UPDATE
Since this Update method is run every frame we want to use the ltaTime to make the value 1 frame worth of movement. Next we convert this raw input value into a value effected by the moveSpeed. The same goes for the Vertical variant with W,S,Up arrow, Down Arrow. This value will be -1 if the A or left arrow key is pressed, 1 if D or right arrow is pressed and 0 if neither are pressed. Here we have used the Input.GetAxisRaw(“Horizontal”) method provided for us by Unity which allows us to get an input value off the player. These will be used to set our player speed and health.
UNITY 2019 2D CODE
In the first part of our code add these two variables. This allows us to setup the player with values once in the start method and then update the player in the Update method from then on. The Start method is called once when the object is created and Update is called once per frame after. When a script is first created you are given 2 methods Start and Update. Simply drag and drop the Player script into the Scripts folder and let’s continue.ĭouble clicking our Player script should open visual studio with the Player script already open in the code view window. This will house all our future scripts and keep the Project view organized for us. Eventually we will have a lot of scripts and it will become hard to find things in the Project window so lets make things easier for our future selves by creating a folder and calling it Scripts. We now have a script in our project called Player. Select new script and click the Create and Add button to make the script for us. Type Player in the search bar at the top to get selection of Video Player or New script.
Here we can add all sorts of prebuild components but for now lets just add a Player script. With the PlayerObject selected go to the Inspector window and click the Add Component button. Since we want the code to affect the player and all it’s components we will add the code to the PlayerObject. In order to do that we must add some code. Our player doesn’t do much at the moment because we haven’t told it to do anything yet. Let’s rename the New Sprite to something that will help identify what this object is like PlayerSprite.įull Unity 2D Game Tutorial 2019 – Coding our Player Movement
Yay! we should now be able to see out player in the Scene view.
Select the Knob(no giggling) by double clicking it or hitting the enter key with it selected. A new window pops up with all the Sprites you have by default in your project. With the New Sprite object selected view the inspector window and click the nipple (yes that’s what its called, no giggling we’re a mature game developer). We have added a Sprite to our player but we still can’t see anything? This is due to the default sprite not having any image associated with it. To do that we select the PlayerObject and right click to get the context menu again and select 2D Object > Sprite. Now, lets add a sprite to our PlayerObject so we can see our player in both the Scene view and game view. It will hold all the objects that make up our player such as sprites. This will be the root element of our player. With the new object selected in the hierarchy view press F2 or right click rename to rename the object to PlayerObject. This will create a new item in the hierarchy. We won’t though, we just need to create a single empty GameObject by clicking the Create Empty option.
This will bring up a menu where we can add all the things. The first step is to right click in your Hierarchy view.
UNITY 2019 2D FULL
Full Unity 2D Game Tutorial 2019 – Creating the player object We will then focus on Player Movement using the physics engine provided in unity. In this section of the tutorial we will create a player object that we can control with the keyboard or controller. Full Unity 2D Game Tutorial 2019 – Player Movement