Tuesday, 14 November 2017

Advancing game further

Previous: Marketing tips

As final step in our basic game development, we will add code to move the box right and left , add lose or win for better game play and generate box from random points. this clearly marks our levels but you can also do level based on another criteria say for every 100 boxes collected increase speed.

Add  variable to store how fast we move the box  
public float MovementSpeed = 1; //speed in meter per second

Add new vector variable  
Vector3 moveDir = Vector3.zero;

Add this to update
moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
This get the horizontal x distance to move based on key pressed.
then move our box
transform.position += moveDir * MovementSpeed * Time.deltaTime;

You can test py pressing play button and use arrow keys
To support touch screen on android ,add the following variables
private Vector3 fp;

private Vector3 lp;

 private float dragDistance=0.2f;

 add this above 
transform.position += moveDir * MovementSpeed * Time.deltaTime; 
See in the code below section for complete code
         #if UNITY_ANDROID || UNITY_IPHONE  // Cap letters
To check lose and win,  add a box below our catchup, enlarge to 
fill screen width and reduce height and call it loss and give it a 
tag loss

Add the new variable
int boxeslost =0; 
add the following in the update below

if (collid) {
if (boxdropped <= totalboxes) {
Instantiate(dropbox, new Vector2(0,2), Quaternion.identity);
collid = false;

if (GameObject.FindGameObjectWithTag ("box").transform.position.y < transform.position.y + 0.1) {
Destroy (GameObject.FindGameObjectWithTag ("box"));
collid = true;
boxeslost += 1;

You will be getting position of box and compare with catch up. 
If below, destroy and add boxes lost
Add this in GUI function

GUI.Label (new Rect (10,30, 300, 50), "Boxes lost: " + boxeslost);

 We now generate from random places
add new variable at start of update
int rand = Random.Range (-5,5);
 change our x value in
 Instantiate(dropbox, new Vector2(rand,2), Quaternion.identity); 

This is just a basic game. You can advance it further like adding
1. random movement downwards by changing x value
2. introduce obstacles and more

The complete code

using UnityEngine;
using System.Collections;

public class CatchBox : MonoBehaviour {
    int boxescollected =0;
    int boxeslost =0;
    int totalboxes =40,boxdropped=0;
    public GameObject dropbox;
    bool collid = false;

    private Vector3 fp;
    private Vector3 lp;
    private float dragDistance=0.2f;
    public float MovementSpeed = 1;
    Vector3 moveDir = Vector3.zero;

    // Use this for initialization
    void Start () {
        collid = true;


    // Update is called once per frame
    void Update () {

        int rand = Random.Range (-5,5);
            if (collid) {
            if (boxdropped <= totalboxes) {
                Instantiate(dropbox, new Vector2(rand,2), Quaternion.identity);
            collid = false;

        if (GameObject.FindGameObjectWithTag ("box").transform.position.y < transform.position.y + 0.1) {
            Destroy (GameObject.FindGameObjectWithTag ("box"));
            collid = true;
            boxeslost += 1;

        moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X

        #if UNITY_ANDROID || UNITY_IPHONE  // Cap letters

        if (Input.touchCount>0) // user is touching the screen with a single touch
            Touch touch = Input.GetTouch(0); // get the touch
            if (touch.phase == TouchPhase.Began) //check for the first touch
                fp = touch.position;
                lp = touch.position;
            else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
                lp = touch.position;
            else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
                lp = touch.position;  //last touch position. Ommitted if you use list

                //Check if drag distance is greater than 20% of the screen height
                if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
                {//It's a drag
                    //check if the drag is vertical or horizontal

                    if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                    {   //If the horizontal movement is greater than the vertical movement...
                        if ((lp.x > fp.x))  //If the movement was to the right)
                            //Right swipe
                            // move box right    
                            MovementSpeed = MovementSpeed;
                        {   //Left swipe
                            // move box left
                            MovementSpeed = MovementSpeed * -1;

                    {   //the vertical movement is greater than the horizontal movement
                        if (lp.y > fp.y)  //If the movement was up
                        {   //Up swipe
                            // you can add code to move box up on advance levels to evade obstacle
                            // you can increase speed to advance levels

            {   //It's a tap as the drag distance is less than 20% of the screen height

                //you can tap code to attack obstacle and clear way on advance levels


        transform.position += moveDir * MovementSpeed * Time.deltaTime;

    void OnCollisionEnter(Collision other)
        if (other.gameObject.tag == "box") {
            boxescollected += 1;
            Destroy (other.gameObject);
            collid = true;

    void OnGUI()
        GUI.Label (new Rect (10,10, 300, 50), "Boxes collected: " + boxescollected);
        GUI.Label (new Rect (10,30, 300, 50), "Boxes lost: " + boxeslost);


Next: adding graphics

No comments:

Post a Comment