How to make Asteroids in Unity (Complete Tutorial) 🚀🌑
Vložit
- čas přidán 23. 07. 2024
- Learn to make the classic 2D arcade game Asteroids in Unity. Asteroids is a space-themed multidirectional shooter arcade game designed by Lyle Rains, Ed Logg, and Dominic Walsh and released in November 1979 by Atari, Inc. The player controls a single spaceship in an asteroid field which is periodically traversed by flying saucers. The object of the game is to shoot and destroy the asteroids and saucers, while not colliding with either, or being hit by the saucers' counter-fire. The game becomes harder as the number of asteroids increases.
0:00 - Introduction
1:10 - Project Creation
1:57 - Scene Setup
7:42 - Player Movement
19:22 - Shooting
32:25 - Asteroid Prefab
42:06 - Spawning Asteroids
1:04:22 - Destroying Asteroids
1:16:09 - Dying & Respawning
1:34:19 - Explosion Particle Effect
1:44:31 - Scoring
1:49:17 - Game Over
1:51:33 - Outro
🖥️ Source code and assets: github.com/zigurous/unity-ast...
👍 Subscribe for more Unity tutorials: czcams.com/users/zigurous?sub...
💬 Join our Discord community: / discord
👑 Become a Patreon supporter: / zigurous
❤️ Support my work: www.paypal.com/donate?hosted_...
⭐ Download my Unity assets: zigurous.com/assets
····················································································
► Learn more about my work: zigurous.com
► Follow for news and updates: / zigurous
#unity #unitytutorial #gamedev #unity3d #unity2d #gamedevelopment #indiegame #indiedev #madewithunity
Visit the GitHub page for the most up to date source code: github.com/zigurous/unity-asteroids-tutorial
View the FAQ for answers to common questions: github.com/zigurous/unity-asteroids-tutorial/wiki
Only about halfway through, but awesome tut! You nailed dropping in just enough explanation here and there to help connect the dots...and the wiki, super nice touch! First time I've seen that for this type of channel and it makes so much sense!
Slowly making my way through these tutorials, really love them. You're gonna be big someday
Hey brother, I'm an oldschool guy that have played the original Astroids game, sadly I did not have the money or drive to get that thing for myself. I did know that this was special tho. Now, at 55, I'm learning to program one of the originals, that's very cool. Thanks for this tut, and the others of cause. Cheers from Holland, Jake. ;-)
One of the best tutorials I have seen on CZcams. Perfect speed and excellent mix of supplying assets and showing development method.
Just got to the end of this! First game I've ever made :) Super clear to follow, learned a ton. Thank you!
discover your channel was one of the best things can happen to my career, I'm learning a lot, thank you
I like that he does and says things slowly, I've watched other tutorials and sometimes the people go way to fast and it's most likely because there passionate about what they do, but it can be hard to follow for people who don't know where everything is quite yet. I think the background music is good too, it's calming and nicer than just listening to someone talking.
I really enjoy your teaching style, speed, and clarity. This is the second tutorial I've done of yours to get back in the flow after not coding for a while and both have been great.
This is one of the best tutorials I've seen for Unity and C#... seriously absolutely amazing. I'm learning so much! Thanks a million and keep it up!
I started playing around with Game Maker, then UE5, then Godot, and now Unity. Programming has been the biggest hurdle for me, so I am also watching other C# tutorials. This has been a very well-paced tutorial and explained well. Thank you!
AWESOME! Best game development tutorial series i've ever seen.
Possibly one of the best tutorials i've found on CZcams.
`score += -(int)Mathf.Round((asteroid.maxSize - asteroid.size) * 100);` score based on size idea.
Your videos are really great! You don't say perplexing things like "This seems really complicated, but it isn't" (and then proceed to not explain it). You are covering not only the how to put the pieces together, but the why each part works the way it does and that is good teaching. Thanks! If you plan to stick to arcade style games, May I suggest Defender, Joust, and Galaga. If you plan to try something outside of the arcade genre then I'd love to see something turn-based. Maybe a simple tb rpg or strategy game. I can't wait to see more.
Thanks for the feedback!
Thank you ! i'm starting Unity and coding, i've done Snake and now Asteroids with your tutorial and i'm starting to understand what i'm doing! Keep it up!
Thanks for the tutorial. I really enjoyed working through it and I appreciate the time you took to explain things. Looking forward to seeing more!
Thanks a lot for the detailed tutorial. I finally learnt many concepts with this session and parallelly finished making the game (with little tweaks). Once again. Thanks.!
Great tutorials. Thank you for all the time you spend making these with us.
This is an outstanding tutorial! That for posting it. Going to try and work on the UI. I will look at your other tutorials as well.
Great tutorial love your explanations I also like how efficient your programming is and how you tailor it to your viewers knowledge perfect for beginners!
Really nice tutorial! Thanks for making these!
Wow Adam, this is the best unity tutorial i have ever watched, because i just love that its a full game, step by step, looks solid and you explained everything really clearly. Im a newbie, i understood most of the concepts and this really helped. Thanks a lot and keep it up!
Thanks for making this tutorial. It has been a great help in my school assignment.
Thank you so much. I really happy for learn these tutorials.
Thanks Adam for sharing your knowledge!
u r best. easy, brief, to the point, well explained. Thanx bro.
very great video, love your content
great video i love this channel, it helps me a lot
thanks :D
Thank you for this tutorial, helped me out a lot! Liked and subscribed ;)
I'm late to the party, but I really enjoyed this tutorial.
Excellent. Thank you.
Thank you very much for the lessons!
Thank you very much ! Its very cool tutorials!
Really enjoying your tutorials! Thank you for making them. Would you consider doing a tutorial for a classic retro FPS dungeon crawler like Wizardry or Ultima? Frogger would be neat, too. Thanks again! I look forward to more!
I'll definitely consider those suggestions, thanks for watching!
These tutorials are absolutely the best! thank you for making them! if you're taking suggestions then maybe can you please do an idle clicker game (like cookie clicker). And if you're thinking of something more advanced then a tower defense game, endless runner, or a gacha style turn-based rpg?
Really great tutorial us always! one thing i would've liked is for you to have included the UI in the video since it can be a lot harder for people to understand things just by looking at code, although i get it would've added a lot more steps to the tutorial
Hope you make more soon!!
Just came across your channel, hope you do a lot more!
excellent tutorial!
Thank You So Much Sir. You Helped me alot. Thank you for such a wonderful teaching and explanation 🙏.Best Tutorial for Sure.
Thank you Adam. Sincerely
bro you are pretty good at this
👍🏻👍🏻👍🏻u r the best! Just keep going! 👌
thanks for helping me to understand the unity engine
Completed this tutorial :)
NICEEEEE
Good job! Thanks!
Thanks man!
Cool!
Thank you
thanks my man
Amazing tutorial, learned loads, thankyou. Any chance of a follow up video showing how to do the UI adding the score and game over screen etc?
and done
just completed!
THANKYOU FOR YOUR TUTORIAL.. REALLY HELO MYSELF, :D
if your player invulnerability wont work check to make sure the layer names are the same, also check for a space at the end.
Its a very good tutorial! Can you do a tutorial how to do a 3d game? I know 3d is much harder than 2d but its intresting, you dont need to do a very hard game thats took many of hours to prepare
next brackeys
Hi, Thanks for the tutorial. I am stuck at a point though, my particle system is producing particles with random initial alpha values. some are opaque initially but most of the particles have low initial alpha value, and it seems to be random. how can I fix that can anyone help please?
In the particle system component, there is a "Start Color" property that you can set. One of the options is to make it random. Perhaps yours got set to this.
There is also a "Color Over Lifetime" property which we use in the video. It's possible to change this to be random as well. Double check those.
Hmmm in the video your using a different version then what you have listed for the assets used.
Unity 2019.4 LTS
So will this still work to follow along without having some sort of errors?
hey, really great tutorial. i know i probably wont get an answer, but what if i wanted to add a max speed to the ship so that it cant go zooming off into infinity? i implemented it pretty easily but then the problem is the ship wont thrust when its pointed in the opposite direction of travel but it definitely should. ive tried all kinds of math and equations and cant figure it out.
AddForce has no top end. You need an entirely different move method.
how would i make it so that the S key or down arrow key makes the player move backward?
Another Easy to Follow Tutorial, learning a lot about coding. The particle system icon was on game screen all the time when playing, right after animation that icon appeared where the animation was played, that was annoying. I'm using version 2020.3.22f1, maybe it's a bug there or maybe i've clicked somewhere accidentally, where i was not supposed to click. changed Explosions position out of camera (x -11) and ring buffer mode from disabled to pause until replayed. Now the annoying icon stays away.
I think those icons you are referring to are called Gizmos. There's a toggle in the editor to turn them on/off.
@@Zigurous yes, that's it, I had gizmos on.
i have Lunix and i code with VS Code, what extensions could I use for Unity, because is too hard for me to write in a good way
👍
55:43 For setTrajectory function we need to normalize -spawnDirection
I seem to have a problem where I get an IndexOutOfRangeException error for when selecting random sprites. It tells me that the index was outside the bounds of the array but I'm not sure why as I have triple checked that the code is the same as yours. I have also checked that the sprites have been selected in the inspector. The error also only appears when the player shoots. Do you have any ideas what might be wrong?
Cheers for the tutorials they are very helpful and easy to follow along
Problem solved -my bad. Turned out i accidentally added the wrong script to the wrong object
For Layer Collision in Physics 2D, You will find 2 tabs on top, General Settings and Layer Collsion Matix. Click on the layer Collison Matrix there you will find the Layer Collision for Physcis 2D.
Soooo...where do I go to find out the *right* way to create a global reference to a GameManager method?
hey bro im stuck with Layer Collision Matrix... im using vers 2022.03.0f1. I found the Layer Collision only in "Physics" not "Physics2D". i tried to change Layer Col on Physics since idont found it on 2D and its still not fix. can anyone help me please reply this comments ASAP
When will next tutorial be uploaded love arcade games 😂❤️ cant wait for pac-man
I'll be make a new video this week and it should be ready by upcoming Monday.
Any advantage to using Vector3 over a Vector2 on a 2d game?
No not necessarily. I tend to use Vector3 out of habit because I work in 3D spaces more, and because the underlying types are still Vector3. That said, if you can use a Vector2 then I would use that as it can simplify your code and improve readability in 2D games.
How to increase spawn rate and player speed overtime?
bummer there isnt a part with screen wrapping the ship and asteroids if they hit the boundaries, not sure why you added those assets in, im not entirely sure how to go about directing the asteroids to be allowed in but still make their way around the zones.
Cant open this project in unity hub. It shows a blue screen only
I followed the code but it is giving me compiler error messages. I tried to copy and paste some of the code and I am still getting messages. Does anyone know why that might be?
can we do it for android phones ?
at 26:13..I get the error msg: 'Bullet' does not contain a definition for 'Project' and no accessible extension method 'Project' accepting a first argument of type 'Bullet' could be found (are you missing a using directive or an assembly reference?) can you help?
I believe your Project function inside of the Bullet script is not set to be public therefore you cannot access it from outside its class. Try changing it from private to public.
At 25:15 you have to make sure the word on line 14 in Bullet.cs is " public void Project(Vector2 direction)" and not anything else.
I had this problem where it changed to Projector instead of Project.
Just an error on our part to not type the code EXACTLY as the engine wants. These things are really tricky and will point the direction of the problem elsewhere instead of where it should have been.
TL;DR. Line 14 in Bullet.cs needs to be exact.
Really liking this tutorial, However, Early on I get NullReferenceException: Object reference not set to an instance of an object. I have added the bullet prefab references to the player like instructed, so I dont really know what I am doing wrong
same boat
use awake function instead of start.
i have done all things the same but at 1:00:30 the asteroids still don't pass through the boundries and my spaceship automatically goes out of the camera view. Please help.
If the asteroids are not passing through the boundaries then that means they are colliding with them. We want to turn off collision between the asteroids and boundaries. This is done by setting these game objects / prefabs on different layers, then changing the 2d collision layer matrix in your project settings. Keep in mind when setting the collision matrix, there is separate settings for 3D vs 2D. Be sure to select the "Physics 2D" tab.
If the spaceship is able to fly out of the camera view, then it's a similar problem. One or more of your game objects / prefabs must not have the correct layer set or the collision matrix hasn't been changed.
How did you get the not equal sign?
using fira code font
When I destroy one astroid I get the message - The object of type 'Asteroid' has been destroyed but you are still trying to access it. pls help
can you make it for pc please? because I really like it and want to play it when I'm on the computer
A build of the game can be downloaded from here: github.com/zigurous/unity-asteroids-tutorial/releases/tag/v1.0.0. Look for the Asteroids.zip link. It's built for Windows.
How do ytou write the does not equal to symbol?
Just use !=
Can you please make a 3d tutorial?
ADD SCORE UI, OK so i figured a way to add a score counter that you can see in game, first you need to add a text (in the UI bar in add) and then you need to add a "public Text ScoreText" in the method area and add a void update at the bottom of the GameManager Script, ad this "ScoreText.text = score.ToString();" also at the very top underneath "Using.UnityEngine" add "using.UnityEngine.UI; if this isn't clear then ask in reply and ill try and clarify it.
how to add settings to this game
love the tutorial but PLEASE... Take a drink next time homey, take... a... drink... Thank you lol...
Can you plzz help me . I did everything same but my asteroids are spawning out of the box. Please kindly tell me the solution
16:38 Can u pls explain what keys u used in keyboard to run and get it in unity?
I think you are referring to the not equal sign, which is typed as !=
I have turned off font ligatures for my future videos to prevent confusion on stuff like this.
@@Zigurous No I mean, u typed the code in visual studio and got it running in unity, how did u did that?
For me it's coming compiler error
@@blck21 You can use any editor you want to write the code. You can select your editor via the menu Edit > Preferences > External Tools. If you have a compiler error then you have mistyped something. If you post the error, I can try to provide more insight.
@@Zigurous what did you type to get not equal sign?
! Means not.
!= Mean not equal
!isThrusting means the opposite of isThrusting. If isThrusting is true, !isThrusting is false.
Whenever I type in >= , unity sends out an error message "Invalid expression term '>=' " Can someone please help? Thank you!
What does the rest of the line of code look like? You might be missing a parenthesis or something else that is causing the problem.
@@Zigurous Thank you so much for the reply ! But I figured it out after sometime ! I was missing a parenthesis and i changed the capitalization in setTrajectory ! Thanks for the tutorial mate! It really helps a lot!
Is this for Android?
If i wanted to increase the speed of the asteroids every time it reaches a score of 1000 how would i go about it?
If(score % 1000 == 0)
Speed++;
How can i add a joystick?:(
Change
isThrusting = Input.GetKey(KeyCode.W);
To
isThrusting = Input.GetAxis("Vertical");
idk why but the particle system doesn't want to work for me.
21:07 "Doodoo"
First
my asteroid do not dissapear afther shooting
No new tutorials yet ☹️😊
New tutorial czcams.com/video/qWDQgmdUzWI/video.html, sorry for the delay!
The original asteroids had no drag lol
I got kinda upset looking at your source code because its very different then what you showed in the video, and I just wonder why you didn't do it like you have it in the first place because I pretty much have to rewrite everything to get player movement to work. Why the tutorial isn't consistent with the actual code bewilders me.
Here is my player controller
Note: for those of you looking for a "reverse" this works
Rigidbody2D rb;
Vector2 InputAxis;
float rotateSpeed = 2.0f;
void Awake() {
rb = GetComponent();
}
void Update() => PlayerInputs();
void FixedUpdate() => PlayerMovement();
void PlayerInputs() {
float xAX = Input.GetAxis("Horizontal");
float yAX = Input.GetAxis("Vertical");
InputAxis = new Vector2(xAX, yAX);
}
void PlayerMovement() {
Vector2 moveDirection = rb.transform.up;
moveDirection.Normalize();
if(InputAxis.y > 0)
rb.AddForce(moveDirection);
if(InputAxis.y < 0)
rb.AddForce(-moveDirection);
rb.transform.Rotate(0, 0, -InputAxis.x * rotateSpeed);
}
Also, if youre looking to "warp" your ship to the opposite side of the screen
Make two more scripts. "SideBoundary" and "VerticalBoundary"
Make sure the boundary game objects have their BoxCollider2D "Is Trigger" box is checked!!!
Add this to the SideBoundary script, and attach the SideBoundary script to the East and West side Boundaries.
void OnTriggerEnter2D(Collider2D target)
{
target.transform.position = new Vector2(target.transform.position.x * -1, target.transform.position.y);
}
Same for the North and South boundaries. Put the vertical script on them, and add this in the script
void OnTriggerEnter2D(Collider2D target)
{
target.transform.position = new Vector2(target.transform.position.x, target.transform.position.y * -1);
}
And there ya go! Now anything that hits that box collider will warp!
If you want to specify which object warps, make a new script called "Warpable"
You dont need to actually edit this script. Just make it, and attach it to whatever object you want to be able to warp.
Add this line to the two Boundary scripts
void OnTriggerEnter2D(Collider2D target)
{
if(target.GetComponent() != null)