Making 2d game unity tutorial
This allows importing optimized Sprite Atlases and retrieving the individual elements as needed without the need to have a handful of separate Sprite assets.
In this tutorial, you'll learn to create and edit Sprites and Sheets. Making a spinner in Unity is a great entry-level project to get students to think about programming and development. Learn more about teaching ideas in this video and by downloading the curriculum guide here. Probability and Design Thinking: Make a Spinner! Load More. Our Pac-Man will only have 4 movement directions up, down, left, right.
Let's take a look at the left area of the Animator and select the Parameters tab:. SetFloat "DirY" , 0 ;. We want Unity to automatically switch to different animation states based on those parameters. Transitions are used to achieve just that.
To save us from doing all this work, we can use Unity's Any State. The Any State stands for literally any state. So if we create a Transition from Any State to right then it's the same as creating a Transition from left , up and down to right. Let's right click the Any State and select Make Transition. Afterwards we can drag the white arrow onto the right state:.
Now we can click on the white arrow and take a look at the Inspector where we can modify the Transition's Condition Let's also disable Can Transition To Self in the Settings : Note: this avoids weird situations where an animation would be restarted all the time while holding down a movement key. Here is how it looks in the Animator now:. We are almost done with our animation state machine. There is one last adjustment to be made here, so let's select all states and then modify their Speed in the Inspector so that the animation doesn't look too fast:.
If we press Play then we can already see the right animation:. We will also change its position to 14, 14 so it's not outside the maze anymore:.
Right now Pac-Man is only an image, nothing more. He is not part of the physics world, things won't collide with him and he can't move or anything else. We will need to add a Collider to make him part of the physics world, which means that things will collide with Pac-Man instead of walking right through him.
Pac-Man is also supposed to move around. A Rigidbody takes care of stuff like gravity, velocity and other forces that make things move. As a rule of thumb, everything in the physics world that is supposed to move around needs a Rigidbody. It's important that we use exactly the same settings as shown in the following image:.
Alright, Pac-Man is now part of the physics world. He will collide with other things and other things will collide with him. With the preparation out of the way, there are several ways to make Pac-Man move.
While this would be trivial to implement, in reality the controls wouldn't feel very good. Instead we will try to be more faithful to the arcade classic, meaning whenever the player presses one of the Arrow-Keys, Pac-Man should move exactly 1 unit into the desired direction. The Pac-Dots the food will also be positioned with a 1 unit distance between them, so it only makes sense that Pac-Man always moves exactly one unit.
We will also move the Script into a new Scripts folder in the Project Area just to keep things clean :. Let's double click the Script file, so it loads up in a script editor usually Visual Studio, but may vary between platforms.
The Start function is automatically called by Unity when starting the game. The Update function is automatically called over and over again, roughly 60 times per second this depends on the current frame rate, it can be lower if there are a lot of things on the screen.
There is yet another type of Update function, which is FixedUpdate. It's also called over and over again, but in a fixed time interval. Unity's Physics are calculated in the exact same time interval, so it's always a good idea to use FixedUpdate when doing Physics stuff:. We will need a public variable so that we can modify the movement speed in the Inspector later on:. We will also need a way to find out if Pac-Man can move into a certain direction or if there is a wall.
So for example if we would want to find out if there is a wall at the top of Pac-Man, we could simply cast a Line from one unit above of Pac-Man to Pac-Man and see if it hit anything. If it hit Pac-Man himself then there was nothing in-between, otherwise there must have been a wall.
We will also need a function that makes Pac-Man move 1 unit into the desired direction. Of course we could just do:. But this would look too abrupt because it's such a huge step. Instead we want him to go there smoothly, one small step at the time. Let's store the movement destination in a variable and then use FixedUpdate to go towards that destination step by step:.
MoveTowards transform. We then use it to do the movement we should never use transform. Let's also watch out for arrow key presses whenever we are not moving.
Note: we are not moving if the current position equals the destination. GetKey KeyCode. Note: transform. Also -Vector2. If we save the Script and press Play then we can now move Pac-Man with the arrow keys:.
Right now we can perfectly move Pac-Man around the maze, but the animator doesn't play all the animations yet. No problem, let's just modify our code to calculate the current movement direction and then set the animator parameters:.
SetFloat "DirX" , dir. SetFloat "DirY" , dir. Others, however, will cite the fact that Unity is more well-rounded and, for indie developers, is often a better entry into the industry. Regarding 2D animation, the level of difficulty also depends on your skill level. If you can draw quickly and efficiently getting the form right immediately , it will probably be easier for you.
A standalone mode, Fortnite Battle Royale, based on the battle royale game genre, was released for the same platforms in September Make a Game — Unity Course by Brackeys. Unity Tutorials from Unity3d. Unity Tutorials by noobtuts. Its visual scripting system and intuitive Editor enables beginners to build an entire game without writing a line of code, and helps programmers plug-and-play their own functionality.
No, coding is not hard to learn. The OnGUI function takes care of displaying the score and the reset button functionality. Then, it checks every time something happens if someone has won yet, and triggers the function ResetBall if someone has. So when we say theBall. Ok cool. Play around with size until it looks good. I set Font Sizes for my Label and Button to 42 and 20, respectively. After we import our packages, we just need to write one function.
This function detects when something is colliding with our left or right walls. Choose the Script we just wrote. Test your game to make sure both players can score a point. If so, know what that means? Now, we only have to make our game playable outside of Unity. To do this, go to File at the top of Unity. This will make an executable playable file appear on our desktop. You may need to Add Open Scenes , to ensure that Main is included in the build.
Now, click on Player Settings. It asks for a default screen width and height. The important thing is in the Supported Aspect Ratios list. Click the little arrow and uncheck everything except and Your settings in the end should look like this:.
Now, hit Build and choose where you want to save the file I chose the desktop , and name it something I named it Pong v1. Look at your desktop. Is it there? Congratulations, and enjoy your game. Thanks for going through this tutorial, and if you want to learn more about Unity or coding in general, make sure to check out the rest of Awesome Inc U online. Note, July Updated for Unity You have a Background to play on You have a set of Paddles that go up and down You have a ball that bounces off walls and paddles You have a set of side walls that you hit to score You have a score at which you win You have a reset button so you can play again Seems simple enough.
Now we can start setting up our project! Open Unity and follow these steps: From the welcome screen, click Projects. Make sure you choose the 2D Template. Click Create Project. Once the project has been created, you should see a 2D grid appear in the Scene view. It should now look something like this: Click and drag the Background image from the Project pane into the Hierarchy pane, just below Main Camera. Select your Background, now in the Hierarchy pane, and you should see it show up in the Inspector pane like this: First, up at the top of the Inspector under Transform , change its scale to 0.
Your Inspector should look like this: Next, we need to make some changes to the Main Camera so that our game will look nicer. Then the Camera component in the Inspector should look like this: All right! Step Two: The Paddles The next step is to make our paddles. Your Inspector should now look like this: Now we want to do the hard part: add a Script for movement for our paddles. Code Breakdown In short, the first three lines are packages of pre-written code we want to tell our program it can use.
Collections ; using System. Generic ; using UnityEngine ;. Label new Rect Screen. Button new Rect Screen. SendMessage "RestartGame" , 0. Score wallName ; hitInfo.
0コメント