Unity — Setting the rules
Creating life comes with great responsibility, we have given our little square friend the power to move, run and even shoot.
Like raising a puppy it is not only about teaching them new tricks and behavior, even more important is to set boundaries! These powers can not just be used without limitation, there have to be some rules!
So what are these rules?
Restricting movement
We don't want our block to just freely run around and wander off or it might get lost. Let us give it a safe space that is right in our view, and if it goes out of these it should come back on the other side.
In order to restrict the movement we first have to find out what the boundaries of our screen are.
First we take a look at the horizontal movement. Drag your block to the far left of your screen and note that the X value of your transform.position setting changes. For me the edges on the X are between -11 and 11.
Now how do we make our block warm to the other side once it passes these points? We use an if statement! let's place it right under our movement code within void Update.
Within this statement we tell it the CONDITION of when it has to warp and in the statement where it has to move to we do this for right and left, so if it goes past 11 on the X it comes out on -11 on the X and the other way around.
Now if you save this and run it you will see that if you move your block of the screen it appears magically on the other side.
Now we want it to stop from moving up and down to much for this we limit the movement on the Y position and it's actually pretty easy! Unity has a function called Mathf.clamp we can use for that.
Clamp Returns the min value if the given float value is less than the min. Returns the max value if the given value is greater than the max value. Use Clamp to restrict a value to a range that is defined by the min and max values.
All we have to do is set our position to a new Vector3 and clamp the possible values of the Y position between -3.8f and 0 like so.
now if you run the code you will see that if you try to move outside these bounds you hit an invisible wall so we don't have to worry about our block getting away anymore.
Projectile movement
Since we are on the subject of movement you might have noticed that when we fire our projectile it just kind of stays there.
This is because we never told our projectile the rules it has to abide by, so let’s do that now.
First we want our projectile to move up the screen and when it is out of view destroys itself so we are not left with all these projectiles in the end.
To do this we need to create a script we can put on the projectile to define its behavior.
To add a new script and link it to an object you right-click within the assets folder and then go to Create > C# script and name it. to add it to an object simply drag it on there.
Within the void Update we have to tell it how to move just like we did to our player. This time let us give it a speed of +9.5 per second we are using a float here so don't forget to add the “f” behind your value.
Now the projectile moves up the screen as expected all there is left to do is have it destroy itself when it leaves the screen just like we restricted the player movement but now we want it to Destroy itself instead of jump to a new position. We can use the Destroy command for this.
The object is destroyed immediately after the current Update loop this method removes the component from the GameObject and destroys it. If it is a GameObject, it destroys the GameObject, all its components and all transform children of the GameObject. Actual object destruction is always delayed until after the current Update loop, but is always done before rendering.
Now that our projectiles work and our player knows some of the basic rules it is time to send it off to Bootcamp so we can teach it some trigger discipline(using a cooldown timer) and get our player combat ready!
However I think he deserves some rest for now so let's tackle this in our next session.
I hope to see you there, thank you for reading and as always happy coding!