Send me a gwei! (ether address, all donations accepted!) 0xfFB33D2Af25909eB92dF5Da6236b69c4AFBaC692 send me a satoshi! (bitcoin address, all donations accepted!) 0xfFB33D2Af25909eB92dF5Da6236b69c4AFBaC692
Current Channel Goal: 25,000 Subs!
Welcome back everybody as always I am TechNiverse. We are right where we left off
We're gonna create our first script for this game in just a moment, but before we can create a script
We need something to control so to make things easy for now
I'm gonna bring in a primitive object and basically the way that you do this is you go to
2d object and
Sprite we're gonna go ahead and grab a square now later on
We'll fill this sprite, and we'll be able to do some things with it for right now
Move it to about right here
It's gonna be our dolphin
The dolphin will go up and down
The camera we're gonna go ahead and attach to it
So that the dolphin is always in this part of the screen because remember we're gonna have obstacles to avoid
Coming in from the right side of the screen, and we want to make sure we have plenty of time to avoid them so
Now if I were to start this game nothing is gonna happen
This is just a square that we can put a picture on later
And we need to do some other things to it so the first thing we're gonna. Do here is we're gonna go to
Add some properties to it and to do that
We need to select it here
And we need to drag this window back out. This is our properties window, so we're gonna need this inspector here so square
okay, we're gonna do is add a component and
Physics 2d
Start with a box collider
Now if I zoom in here you can see a green line around this and I can actually edit the collider right here
I'm making it larger smaller. I think I'm gonna make it a little bit bigger that way eventually this is gonna be a dolphin
So he's gonna be kind of twice as long as he is tall
But I don't want to skew the scale until I get some of my graphics items figured out so now that we have that
We're not quite ready for our first script yet. We have a collider which means you can run into stuff
We need one more component to make the physics work, and that is gonna be a rigid body 2d so
Now that I have these two things I can go ahead and start the game, and you'll watch this this guy will
well actually
Since the camera is attached
Let's detach it sure
Gravity is gonna affect this square, and it should just fall right out of view there there we go
Now if we put the camera back on the object
It is still falling, but the camera is locked to its position
So you can still see it you saw fall out of view here the reason you can tell
That it's still falling
Because if you select square here the y value is decreasing exponentially, so let's go ahead. We'll stop that
That's kind of cool, but the way this game is gonna work is with kind of a buoyancy effect
So I want it to fall upwards
And I want to be able to tap the button to make it swim down below stuff
So what we're gonna do in that case is we're gonna grab the rigid body here
We're gonna change the gravity scale to negative 1 and if we unattach the camera one more time
It should go up instead of down
increasing on the Y value so
There it is you can see
As I said we do however need that
That camera attached for now. We are gonna add a script to this camera that will allow it to
Like stay right here, so this goes up and down
But it basically tracks it on the X coordinate so
For those of you that are maybe throwing some things at you a little bit fast
Let me explain a little bit more about the coordinate system, and why that's expanding on the Y
So if you look here, we have this grid and right now
It's in two-dimensional space this this 2d is clicked if I go to three-dimensional
You can see these and these are the three axes of the game engine basically this green one is your Y
That's your Y value okay, and if I go to the front view here
You can see there is an up and down and a left and right and you can't see that third that depth
Because it's hidden from the angle we're looking at it, so it does still take effect
But with the 2d engine selected you don't really need to worry about it. We'll be putting things into layers
But basically what you need to know is this is the x-axis right here, okay?
And this is the y-axis so
That's basically the most important part. I think we need to take this
Get down here. I don't think you even saw the position changing. Let's run that one more time here
Cameras attached to the square, so it's going to follow the square, but if we select the square
You can see the y increasing okay, so that's perfect. That's exactly what we want so
Now we need to go ahead and make a script. We're not going to do a movement script yet
Let's do
We're going to go to resources
scripts and
create
C-Sharp script
Call this one dolphin
All right, so now
We have our square. Which is actually our dolphin. We're going to put some controls on it, so
order to do that
We go ahead and select the script drag and drop it onto the square here
and you can see it shows up in its components, and then we need to open our
C-Sharp editing programs we created the C-Sharp script, so let's click on this should open and Microsoft visual studio
It does take a minute so bear with me
All right, it has opened up unity code assist is acting if asking if we want to use the plug-in we're going to hit yes
and
Basically, we're going to go ahead and jump right into this so in the update method. We are going to look for some input, so
Let's go ahead and type
input
Get button, and we don't want to be able to hold the button, so we're going to push get button get button down and
Key code is how you access the keyboard we're going to use the space bar here, so key code
Space all right, so if we push the space button
input
Get key, that's what we need okay, so that's I did wrong. I'm trying to pass a key into a button, so let's
Key down there we go got rid of that error there, so
We're actually we're going to make this an if statement, and these are pretty common for
dealing with Boolean logic
If there we go, so if key down space we're going to say
get
Component would you grab a component?
Rigid body 2d. That's the object we added to it
Velocity equals
vector to
0 5 now that should
Should alter the y
See if this works. There's several different ways to skin a cat, and I'm not sure it's been a little bit since I did
That's safe, let's go to unity here
Let it compile and then we'll see what's happening
Might need to change that velocity to something times
Speed or times a direction honestly
We'll see what we get here
All right, it's going here, so let's see
If I hit the spacebar I get nothing it should be x and then y let's see
Let's try this let's try
rigid body dot add
Force make sure to dot down times
to
It's probably gonna end up being less than one value. We'll use a float there eventually
See if this works of course we got to let it reload all the assemblies again
All right, so we're gonna go ahead and increase this because it's obviously not having an effect
Now we're gonna add a second thing here, that's the force mode and we're gonna go force mode 2d
impulse
This should do the trick
Jump back over to unity here. Let it recompile
We're gonna go ahead and just unattach this for now leave it unattached since we don't have any motion to the right
We're trying to deal with the vertical right now, but we can get a little bit our better idea of the movement here
So let's go ahead and hit play
And you'll see you're trying to rise off the screen
then when I tap
Down key when I tap spacebar. I get this nice Bob. I feel like it's a little bit too strong I
want like a three jump gap between
The top and the bottom so let's go back into the code here. We'll reduce the strength probably about seven or eight I think
Let's try eight
Eventually, we'll change this to be a force button. That'll be alterable as well as the speed that we move at
Oops, that's not what I wanted to click on there. We go and now we'll try this again
I should have to press space a couple times to get it to go down further
And it's a change in momentum. So the faster I'm moving upwards
The further down I'll move I feel like that's still a little strong for what I'm trying to accomplish. We're gonna go back
To a half of that. I do like the buoyancy how it's like popping up and down. That's pretty cool
We're gonna scale that back to four. In fact, let's go ahead and add that variable and now variables are amazing
We're gonna call this one a float in case we end up doing a partial percentage and we're gonna call it
Strength how about that equals 4.0 F and then we'll replace that here
Now that should work. And in fact, I think we want to get this dialed in. So if we go public here
Then we can hit save
We'll actually have the option to change that on the fly while we're playing the game so we can go over here
And now if I click on square you can see it's added that item here
So I can change this before I start or even after I start the simulation once I start it if I change it
It'll revert back to what it was
Before so
Don't think four is gonna cut it
Actually, let's try this. Let's go ahead and reduce
The gravity because it's not really negative one. It's closer to negative
What's a 30 your body weight?
Let's try that. This is
Three-quarters, it's not there, but we have to reduce that some more. It's too much too much
We should just put the strength back up at eight people actually will change both of these the gravity scale and
Okay, so that works but here's the thing is I intend this to be a mobile game in the long run
There's no space key on a regular screen. What there is however is touches
but we can simulate that on the computer using a
different
tactic we're also going to use if
input dot get
We're gonna do mouse button down
And we're gonna do zero because that's the left click
grab our code there, which we already like place it here and
Let's go ahead and get rid of the space bar
So we'll save that I should be able to click the mouse. That's gonna give me a little bit better simulation of like
tapping and
Hitting the spacebar, so let's go back to unity here
Open it up
Yeah, it's a little bit better a little bit more fluid. I think I
Think the gravity is a little weak
Let's go ahead and shut that off. Let's get in here
We'll try that
In fact, let's go back to eight
this
2.55 I
Know this seems like a lot of simple minutiae, but these are the things that are really gonna affect the game later
and they'll be easy enough to adjust for sure, but
So once I go off-screen, I've already lost
How easy is it to keep?
Keep it
in the top
Bottom all right, so I think what we're gonna. Do is we're gonna put the water to about here, right?
Don't put a seafloor down there
Okay, all right, so that's basically the gist of it. I kind of went off into my own little world there for a minute
I hope you guys liked it if you did hit the subscribe button and hit the notification bell as well that we can get notified
When we continue the series I do do I'm working on two games right now
This is actually the second one, but I'm working on another one called zombie which you may have seen the videos
I've done on that you may not
but
I'm getting further in it, which I like so that one won't be too long before it's out
That's gonna be it for this one guys tech never sell