top of page

Week 7: Advanced Programming - accessing different classes (background speed)

Hello again, last time I showed you how to create the paper plane for our game and how to apply gravity trough code. This time we will fix that using Unity’s tools and also we will manage the player speed on time and over hit. First thing we will want to do is go to Inspector > Rigidbody2D > Gravity Scale and set it to 2. This is how your rigidbody should look like

Then we must navigate to change the basic unity gravity. To do this go on Edit -> Project Settings -> Physics 2D and change Gravity – Y Axis to -1.8

And this is it. Since the last time other guys from group created the background so this is how it looks right now.

Now it’s kind of tricky to explain the connection between paper plane and background and also I will upload the background with explanation of how it works. Let’s begin with the plane. Add four variables called: moveSpeed of type float and make it public which we will use for the speed that moves the player up and down, two private float variables called yMin = -3.6f and yMax = 11.2f. These two we will be using to limit the player moving only in screen. The last variable is public of type bool. This one we will use to make an instance of script that does the background scrolling. So name the variable checkHit and give it “BGScroll.OnHit” value. This is how it should look

Then in the Start() function we must fix the screen orientation so the player will be able to hold the Nexus in front of his face, not laying down.

Next in the update function we need to create the movement for the player. We will write to types of movement – one with the accelerometer which is our task and the other with keyboard so that we will be able to test it in Unity.

To add the accelerometer it is actually pretty easy. We need to add just the line transform.Translate(0, -Input.acceleration.x,0); and comment that for now. Now you may notice if you try to run the game that the gravity will push you down harder every time you press a button. So for the key controls you create an if-else statement to check which button has been pressed, change the Y coordinates and change the gravity to zero. Then on key up return the gravity to 1 again. Then in the end use the minimum and maximus Y coordinates to limit the player inside the background. This is how your update function should look I the end.

Now it comes time check if there is hit with an enemy. We will use ready spawning script and prefabs for enemies. Still I will have to explain the background script. The logic behind the background is that we move a few sprite from right to left while we actually keep the Plane at the same spot. The background have the following variables

the static onHit variable we will use to follow what’s going on in the Player script. The slowed variable we use to check if now it is hitting a player, the scrollSpeed is the speed of the background, tileSize are the variable we are using to declare how big is this piece of sprite that we are moving. StartPosition variable we use to calculate where to move the background. Then in the update we are doing the background movement.

Now we are going back to the Player.cs and we have to add two functions: OnTriggerEnter2D and OnTriggerExit2D. In the first one we will say to the Scrolling script that a hit is encountered, slow speed of all backgrounds and then destroy the enemy.

What happens here: first check if we hit object with tag enemies, then change the BGScroll variable OnHit to true, disable the rendering of the object but leaving object there so we can check where we will stop hitting it. After there are no more colliding, destroy the meme and change the onHit variable to false again. Then in the BGScroll.cs we must add one more if statement in the update function.

And this should do the job. Next time we will have to create the game over menu and the loading screen between boss and level scene.

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square

Email:
kabadzhg@uni.coventry.ac.uk

bottom of page