Pokemon Safari

Group

Section A ECE 4180

Reynolds Aguilar

Zachary Johnson

Jea Du Kim

Description

Pokemon Safari is the ULTIMATE Pokemon experience. Compete with friends as you try to catch the most Pokemon in a brand new Safari Zone. This Safari Zone isn't similar to any previous safaris you've been to; many rare and legendary Pokemon call this safari home such as the Legendary bird trio, Articuno, Zapdos and Moltres! With only 30 pokeballs, rocks, and bait at your disposal, you need to be careful with how you use your items,

Gameplay: While the game is running, connect your phone to the mbed using the Adafruit Bluetooth BLE module and use Adafruit's Bluetooth app to stream your phone's Quaternion data to the mbed. Then by tilting your phone, you can move your cursor on the screen to take aim at any Pokemon on screen. By using two pushbuttons connected to your mbed, you can then cycle through your inventory, choose your items, and then throw them on screen.

Parts used: Adafruite Bluefruit LE, 2 pushbuttons.

Hookup Guide

mbedAdafruit BLEPushbutton 1Push button 2
gndgnd
VU(5v)Vin
ncRTS
GndCTS
p27 (Serial RX)TXO
p28 (Serial TX)RXI
p20digitalIn
p19digitalIn

Mbed Parts Image

/media/uploads/zjohnson31/pkmon.png

Game Demo

Code/Libraries

Import programPokemonSafari

Code to stream phone data to mbed using Adafruit Bluetooh BLE and app.

Game

The game was developed using Unity. The framework can be downloaded from unity3d.com The mbed is interfaced through the serial port of the computer (COM3 in our case). The computer receives quaternion data via the COM port from the mbed. The data is sent in a specific way in order to correctly process and move the player's cursor. Unity handles all of the games physics and intelligence as long as C# logic is provided. The following scripts are composed of the C# logic we used to implement Pokemon Safari.

C# Scripts

Player 1 controller

CursorController2.cs

using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class CursorController2 : MonoBehaviour
{
    public static SerialPort sp;
    public string message, message1;
    public string message2;

    public static float x;
    public static float y;
    public static float z;
    public static bool throwflag;
    public static int item;
    public static float throwtime;
    private static float xthresh;
    private static float ythresh;

    public CursorController2()
    {
        //sp = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
    }


    // Use this for initialization
    void Start()
    {
        Cursor.visible = false;
        x = transform.position.x;
        y = transform.position.y;
        z = transform.position.z;
        //throwflag = false;
        //throwtime = 0;
        //item = 0;
        //xthresh = 0.1F;
        //ythresh = 0.1F;
        //sp.ReadTimeout = 1;
        //OpenConnection();
    }

    // Update is called once per frame
    void Update()
    {
        //string xpos = "";
        //string ypos = "";
        //string zpos = "";
        //string w = "0";
        // make sure there is enough data in buffer to read
        //char curr = (char)sp.ReadChar();
        //while (curr == '!') curr = (char)sp.ReadChar();

        //while (curr != ',')
        //{
        //    xpos += curr;
        //    curr = (char)sp.ReadChar();
        //}

        //curr = (char)sp.ReadChar();
        //while (curr != ',')
        //{
        //    ypos += curr;
        //    curr = (char)sp.ReadChar();
        //}

        //curr = (char)sp.ReadChar();
        //string throws = "";
        //while (curr != ',')
        //{
        //    throws += curr;
        //    curr = (char)sp.ReadChar();
        //}
        //if (Time.time - throwtime > 0.5f && throws.Substring(0, 1).Equals("t"))
        //{
        //    throwflag = true;
        //    if (throws.Substring(1, 1).Equals("p"))
        //    {
        //        item = 0;
        //    }
        //    else if (throws.Substring(1, 1).Equals("r"))
        //    {
        //        item = 1;
        //    }
        //    else if (throws.Substring(1, 1).Equals("b"))
        //    {
        //        item = 2;
        //    }
        //}

        x = 2.5f * Input.mousePosition.x / Screen.width;
        y = 2 * Input.mousePosition.y / Screen.height;
        //if (xpos.Equals("")) xpos = "0";
        //if (ypos.Equals("")) ypos = "0";

        //float xdel = float.Parse(xpos);
        //float ydel = float.Parse(ypos);

        //if (xdel > xthresh || xdel < 0 - xthresh)
        //{
        //    y += xdel / 2;
        //}
        //if (ydel > ythresh || ydel < 0 - ythresh)
        //{
        //    x += ydel / 2;
        //}
        ////z += float.Parse(zpos);
        //if (x > 2.5) x = 2.5F;
        //else if (x < 0) x = 0;
        //if (y > 1.5) y = 1.5F;
        //else if (y < 0) y = 0;


        transform.position = new Vector2(x, y);
        //myport.Close();
    }

    //public void OpenConnection()
    //{
    //    if (sp != null)
    //    {
    //        if (sp.IsOpen)
    //        {
    //            sp.Close();
    //            message = "Closing port, because it was already open!";
    //        }
    //        else
    //        {
    //            sp.Open();
    //            sp.ReadTimeout = 1;
    //            message = "Port Opened!";
    //        }
    //    }
    //    else
    //    {
    //        if (sp.IsOpen)
    //        {
    //            print("Port is already open");
    //        }
    //        else
    //        {
    //            print("Port == null");
    //        }
    //    }
    //}

    //void OnApplicationQuit()
    //{
    //    sp.Close();
    //}
}

Pokemon Behaviour

PokeBehavior.cs

using UnityEngine;
using System.Collections;

public class pokeBehavior : MonoBehaviour
{
    //private PokeManager;
    public Transform[] cursors;
    //public PokeManager pm;
    public bool stun = false;
    public bool bait = false;
    public float moveSpeed = 0.2f;
    public float maxX = 6.1f;
    public float minX = -6.1f;
    public float maxY = 4.2f;
    public float minY = -4.2f;
    public float catchRate = 0.0f;
    public float baitRate = 0.0f;
    public int loc;
    private float tChange = 0f; // force new direction in the first Update
    private float randomX;
    private float randomY;
    private float tstart = 0.0f;
    private float tstun = 0.0f;
    private float life = 0.0f;
    private float tbait = 0.0f;
    private bool firstStun = false;
    private bool firstBait = false;
    private bool isBaited = false;
    private bool isStunned = false;
    public float lifeTime = 10.0f;
    private bool leave = false;
    private float timeDeath = 0.0f;
    // Use this for initialization
    void Start()
    {
        tstart = Time.time;
    }

    void Update()
    {
        Stun();
        Bait();
        if (!leave)
        {
            life = Time.time - tstart;
            // change to random direction at random intervals
            if (Time.time >= tChange)
            {
                randomX = Random.Range(-2.0f, 2.0f); // with float parameters, a random float
                randomY = Random.Range(-2.0f, 2.0f); //  between -2.0 and 2.0 is returned
                                                     // set a random interval between 0.5 and 1.5
                tChange = Time.time + Random.Range(0.5f, 1.5f);
            }
            if (isStunned)
            {
                transform.Translate(0, 0, 0);
            }//if
            else if (bait)
            {

            }
            else
            {
                transform.Translate(new Vector2(randomX, randomY) * moveSpeed * Time.deltaTime);
            }
            // if object reached any border, revert the appropriate direction
            if (transform.position.x >= maxX || transform.position.x <= minX)
            {
                randomX = -randomX;
            }
            if (transform.position.y >= maxY || transform.position.y <= minY)
            {
                randomY = -randomY;
            }

            if (transform.position.x > maxX || transform.position.x < minX || transform.position.y > maxY || transform.position.y < minY)
            {

                int id = this.gameObject.GetInstanceID();
                int shift = 0;
                for (int i = 0; i < 5; i++)
                {
                    if (PokeManager.spawnedPokemon[i] != null)
                    {
                        if (PokeManager.spawnedPokemon[i].GetInstanceID() == id)
                        {
                            shift = i;
                            break;
                        }
                    }
                }
                for (int i = shift; i < 4; i++)
                {
                    //Debug.Log("Removing position " + shift);
                    PokeManager.spawnedPokemon[i] = PokeManager.spawnedPokemon[i + 1];
                    PokeManager.stun[i] = PokeManager.stun[i + 1];
                    PokeManager.bait[i] = PokeManager.bait[i + 1];
                }
                PokeManager.pokeCount--;
                DestroyImmediate(this.gameObject);
            }//if
            if (life >= lifeTime)
            {
                leave = true;
                timeDeath = Time.time;
            }
            // make sure the position is inside the borders
        }//if
        else
        {
            int x2 = 2;
            transform.Translate(new Vector2(x2, 0) * moveSpeed * Time.deltaTime);
            
            if (transform.position.x > maxX || transform.position.x < minX || transform.position.y > maxY || transform.position.y < minY)
            {
                int shift = 0;
                int id = this.gameObject.GetInstanceID();
                for (int i = 0; i < 5; i++)
                {
                    if (PokeManager.spawnedPokemon[i] != null)
                    {
                        if (PokeManager.spawnedPokemon[i].GetInstanceID() == id)
                        {
                            shift = i;
                            break;
                        }//if
                    }//if
                }//for
                for (int i = shift; i < 4; i++)
                {
                    //Debug.Log("Removing position " + shift);
                    PokeManager.spawnedPokemon[i] = PokeManager.spawnedPokemon[i + 1];
                    PokeManager.stun[i] = PokeManager.stun[i + 1];
                    PokeManager.bait[i] = PokeManager.bait[i + 1];
                }//for
                PokeManager.pokeCount--;
                DestroyImmediate(this.gameObject);
            }//if
            else if (Time.time - timeDeath > 5.0f)
            {
                int shift = 0;
                int id = this.gameObject.GetInstanceID();
                for (int i = 0; i < 5; i++)
                {
                    if (PokeManager.spawnedPokemon[i] != null)
                    {
                        if (PokeManager.spawnedPokemon[i].GetInstanceID() == id)
                        {
                            shift = i;
                            break;
                        }//if
                    }//if
                }//for
                for (int i = shift; i < 4; i++)
                {
                    //Debug.Log("Removing position " + shift);
                    PokeManager.spawnedPokemon[i] = PokeManager.spawnedPokemon[i + 1];
                    PokeManager.stun[i] = PokeManager.stun[i + 1];
                    PokeManager.bait[i] = PokeManager.bait[i + 1];
                }//for
                PokeManager.pokeCount--;
                DestroyImmediate(this.gameObject);
            }//elseif
        }//else
    }//update

    //function to set stun
    void Stun()
    {
        for (int i = 0; i < 5; i++)
        {
            if (PokeManager.spawnedPokemon[i] != null && PokeManager.spawnedPokemon[i].GetInstanceID() == this.gameObject.GetInstanceID())
            {
               // Debug.Log("this guys stunned");
                if (PokeManager.stun[i]&&!isStunned)
                    stun = true;
            }
        }

        if (stun)
        {
            firstStun = true;
            stun = false;
        }
        if (firstStun)
        {
            tstun = Time.time;
            firstStun = false;
            isStunned = true;
        }
        if (Time.time - tstun >= 2.0f)
        {
            for(int i = 0; i<5; i++)
            {
                if (PokeManager.spawnedPokemon[i] != null && PokeManager.spawnedPokemon[i].GetInstanceID() == this.gameObject.GetInstanceID())
                {
                    PokeManager.stun[i] = false;
                }
            }
            isStunned = false;
        }
    }//stun

    //function to set bait
    void Bait()
    {
        if (PokeManager.baitCount > 0)
        {
            float xpos = this.transform.position.x;
            float ypos = this.transform.position.y;
            float bx = 0;
            float by = 0;
            float nearestx = 10;
            float nearesty = 10;
            int closest = 0;
            for(int i =0; i<PokeManager.baitCount; i++)
            {
                if (PokeManager.baits[i] != null)
                {
                    bx = PokeManager.baits[i].transform.position.x;
                    by = PokeManager.baits[i].transform.position.y;
                    bx = System.Math.Abs(xpos - bx);
                    by = System.Math.Abs(ypos - by);
                    if(bx < nearestx && by < nearesty)
                    {
                        closest = i;
                        nearestx = PokeManager.baits[i].transform.position.x;
                        nearesty = PokeManager.baits[i].transform.position.y;
                    }//if
                }//if
            }//for
            bait = true;
            //this.transform.Translate(new Vector2(nearestx, nearesty) * moveSpeed * Time.deltaTime);
            if (PokeManager.baits[closest] != null)
            {
                Vector3 target = PokeManager.baits[closest].transform.position;
                this.transform.position = Vector3.MoveTowards(this.transform.position, target, moveSpeed * Time.deltaTime);
            }
        }//if
        else
        {
            bait = false;
        }
    }//bait
}//class

Pokemon Manager

PokeManager.cs

using UnityEngine;


public class PokeManager : MonoBehaviour
{
    //public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    public static int pokeCount;
    public static int p1Score = 0;
    public static int p2Score = 0;
    private bool done = false;
    public static bool[] stun = new bool[5]; // for baits and stuns 
    public static bool[] bait = new bool[5];
    public static GameObject[] baits = new GameObject[5];
    public static int baitCount = 0;
    public GameObject[] regularPokemon;                // The enemy prefab to be spawned.
    public GameObject[] rarePokemon;        // rarer pokemon
    public GameObject[] ultraRarePokemon;   // rarest
    public float spawnTime = 3f;            // How long between each spawn.
    public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    public Transform[] cursors;
    public static GameObject[] spawnedPokemon = new GameObject[5];
    private int shiftIndex = 0;

    void Start()
    {
        // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
        InvokeRepeating("Spawn", spawnTime, spawnTime);
    }//Start

    void removePokemon()
    {
        for(int i = shiftIndex; i <4; i++)
        {
            spawnedPokemon[i] = spawnedPokemon[i + 1];
            stun[i] = stun[i + 1];
            bait[i] = bait[i + 1];
        }
    }

    void Spawn()
    {
        if(pokeCount > 4)
        {
            return;
        }
        float rand = Random.Range(0, 101);
        // Find a random index between zero and one less than the number of spawn points.
        int spawnPointIndex = Random.Range(0, spawnPoints.Length);


        // Create an instance of the enemy prefab at the randomly selected spawn point's position depending on chances
        if(rand >= 98)
        {
            spawnedPokemon[pokeCount] = (GameObject)Instantiate(ultraRarePokemon[UnityEngine.Random.Range(0, ultraRarePokemon.Length)], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
            pokeCount++;
        }
        else if (rand >= 85)
        {
            spawnedPokemon[pokeCount] = (GameObject)Instantiate(rarePokemon[UnityEngine.Random.Range(0, rarePokemon.Length)], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
            pokeCount++;
        }//if
        else
        {
            spawnedPokemon[pokeCount] = (GameObject)Instantiate(regularPokemon[UnityEngine.Random.Range(0, regularPokemon.Length)], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
            pokeCount++;
        }//else
    }

    void Update()
    {
        shiftIndex = 0;
        //check collisions of pokemon and cursor
        if (pokeCount > 0)
        {
            for (int i = 0; i < pokeCount; i++)
            {
                //cursor is 0.2 wide and 0.1 tall
               if (spawnedPokemon[i] != null)
               {
                    for (int j = 0; j < PlayerV2.numpb; j++)
                    {
                        if (PlayerV2.projectile[j] != null&&PlayerV2.projectile[j].tag != "bait")
                        {
                            float xdiff = System.Math.Abs(spawnedPokemon[i].transform.position.x - PlayerV2.projectile[j].transform.position.x);
                            float ydiff = System.Math.Abs(spawnedPokemon[i].transform.position.y - PlayerV2.projectile[j].transform.position.y);
                            if (xdiff <= 0.201f && ydiff <= 0.101f)
                            {
                                if (PlayerV2.projectile[j].tag == "rock")
                                {
                                    stun[i] = true;
                                    DestroyImmediate(PlayerV2.projectile[j].gameObject);
                                    for (int k = i; k < PlayerV2.numpb - 1; k++)
                                    {
                                        PlayerV2.projectile[k] = PlayerV2.projectile[k + 1];
                                        PlayerV2.time[k] = PlayerV2.time[k + 1];
                                    }
                                    PlayerV2.numpb--;

                                }//if
                                else
                                {
                                    if (spawnedPokemon[i].tag == "ultra")
                                    {
                                        p1Score += 100;
                                    }
                                    else if (spawnedPokemon[i].tag == "rare")
                                    {
                                        p1Score += 10;
                                    }
                                    else
                                    {
                                        p1Score += 1;
                                    }
                                    DestroyImmediate(spawnedPokemon[i].gameObject);
                                    DestroyImmediate(PlayerV2.projectile[j].gameObject);
                                    for (int k = i; k < PlayerV2.numpb - 1; k++)
                                    {
                                        PlayerV2.projectile[k] = PlayerV2.projectile[k + 1];
                                        PlayerV2.time[k] = PlayerV2.time[k + 1];
                                    }
                                    PlayerV2.numpb--;
                                    shiftIndex = i;
                                    //p1Score++;
                                    removePokemon();
                                    pokeCount--;
                                    done = true;
                                    break;
                                }//else
                            }//if
                        }//if

                        //bait
                        else
                        {

                        }
                    }//for

                    for (int j = 0; j < PlayerV2_2.numpb2; j++)
                    {
                        if (PlayerV2_2.projectile2[j] != null && PlayerV2_2.projectile2[j].tag != "bait")
                        {
                            float xdiff = System.Math.Abs(spawnedPokemon[i].transform.position.x - PlayerV2_2.projectile2[j].transform.position.x);
                            float ydiff = System.Math.Abs(spawnedPokemon[i].transform.position.y - PlayerV2_2.projectile2[j].transform.position.y);
                            if (xdiff <= 0.201f && ydiff <= 0.101f)
                            {
                                if (PlayerV2_2.projectile2[j].tag == "rock")
                                {
                                    stun[i] = true;
                                    DestroyImmediate(PlayerV2_2.projectile2[j].gameObject);
                                    for (int k = i; k < PlayerV2_2.numpb2 - 1; k++)
                                    {
                                        PlayerV2_2.projectile2[k] = PlayerV2_2.projectile2[k + 1];
                                        PlayerV2_2.time2[k] = PlayerV2_2.time2[k + 1];
                                    }
                                    PlayerV2_2.numpb2--;

                                }//if
                                else
                                {
                                    if (spawnedPokemon[i].tag == "ultra")
                                    {
                                        p2Score += 100;
                                    }
                                    else if (spawnedPokemon[i].tag == "rare")
                                    {
                                        p2Score += 10;
                                    }
                                    else
                                    {
                                        p2Score += 1;
                                    }
                                    DestroyImmediate(spawnedPokemon[i].gameObject);
                                    DestroyImmediate(PlayerV2_2.projectile2[j].gameObject);
                                    for (int k = i; k < PlayerV2_2.numpb2 - 1; k++)
                                    {
                                        PlayerV2_2.projectile2[k] = PlayerV2_2.projectile2[k + 1];
                                        PlayerV2_2.time2[k] = PlayerV2_2.time2[k + 1];
                                    }
                                    PlayerV2_2.numpb2--;
                                    shiftIndex = i;
                                    //p2Score++;
                                    removePokemon();
                                    pokeCount--;
                                    done = true;
                                    break;
                                }//else
                            }//if
                        }//if

                        //bait
                        else
                        {

                        }
                    }//for
                    if (done)
                    {
                        done = false;
                        break;
                    }
                }//if
            }//for
        }//if
    }//update
}

Player 2 Gui

ScoreManager1.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager1 : MonoBehaviour
{
    //public static int score;        // The player's score.


    Text text;                      // Reference to the Text component.


    void Awake()
    {
        // Set up the reference.
        text = GetComponent<Text>();

        // Reset the score.
        PokeManager.p2Score = 0;
    }


    void Update()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Player 2 Score: " + PokeManager.p2Score + "\n\nPokeballs Left: " + PlayerV2_2.totalBall2;
    }
}

Player 1 Gui

ScoreManager.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    //public static int score;        // The player's score.


    Text text;                      // Reference to the Text component.


    void Awake()
    {
        // Set up the reference.
        text = GetComponent<Text>();

        // Reset the score.
        PokeManager.p1Score = 0;
    }


    void Update()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Player 1 Score: " + PokeManager.p1Score + "\n\nPokeballs Left: " + PlayerV2.totalBall;
    }
}

Player 1 Controller

CursorController.cs

using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class CursorController : MonoBehaviour
{
    public static SerialPort sp;
    public string message, message1;
    public string message2;

    public static float x;
    public static float y;
    public static float z;
    public static bool throwflag;
    public static int item;
    public static float throwtime;
    public static float switchtime;
    private static float xthresh;
    private static float ythresh;

    public CursorController()
    {
        sp = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
    }


    // Use this for initialization
    void Start()
    {
        //Cursor.visible = false;
        x = transform.position.x;
        y = transform.position.y;
        z = transform.position.z;
        throwflag = false;
        throwtime = 0;
        switchtime = 0;
        item = 0;
        xthresh = 0.1F;
        ythresh = 0.1F;
        //myport = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        //myport.Open();
        //Debug.Log("here");
        sp.ReadTimeout = 1;
        OpenConnection();
    }

    // Update is called once per frame
    void Update()
    {
        string xpos = "";
        string ypos = "";
        string zpos = "";
        string w = "0";
        char curr = (char)sp.ReadChar();
        while (curr == '!') curr = (char)sp.ReadChar();
        while (curr != ',')
        {
            xpos += curr;
            curr = (char)sp.ReadChar();
        }

        curr = (char)sp.ReadChar();
        while (curr != ',')
        {
            ypos += curr;
            curr = (char)sp.ReadChar();
        }

        curr = (char)sp.ReadChar();
        string throws = "";
        while (curr != ',')
        {
            throws += curr;
            curr = (char)sp.ReadChar();
        }
        if (Time.time - throwtime > 0.5f && throws.Substring(0, 1).Equals("t"))
        {
            throwflag = true;
            if (throws.Substring(1, 1).Equals("p") && Time.time - switchtime > 0.5f)
            {
                item = 0;
                switchtime = Time.time;
            }
            else if (throws.Substring(1, 1).Equals("r") && Time.time - switchtime > 0.5f)
            {
                item = 1;
                switchtime = Time.time;
            }
            else if (throws.Substring(1, 1).Equals("b") && Time.time - switchtime > 0.5f)
            {
                item = 2;
                switchtime = Time.time;
            }
        }

        if (xpos.Equals("")) xpos = "0";
        if (ypos.Equals("")) ypos = "0";

        float xdel = float.Parse(xpos);
        float ydel = float.Parse(ypos);

        if (xdel > xthresh || xdel < 0 - xthresh)
        {
            y += xdel / 2;
        }
        if (ydel > ythresh || ydel < 0 - ythresh)
        {
            x += ydel / 2;
        }
        if (x > 2.5) x = 2.5F;
        else if (x < 0) x = 0;
        if (y > 1.5) y = 1.5F;
        else if (y < 0) y = 0;


        transform.position = new Vector2(x, y);
    }

    public void OpenConnection()
    {
        if (sp != null)
        {
            if (sp.IsOpen)
            {
                sp.Close();
                message = "Closing port, because it was already open!";
            }
            else
            {
                sp.Open();
                sp.ReadTimeout = 1;
                message = "Port Opened!";
            }
        }
        else
        {
            if (sp.IsOpen)
            {
                print("Port is already open");
            }
            else
            {
                print("Port == null");
            }
        }
    }

    void OnApplicationQuit()
    {
        sp.Close();
    }
}

Player 1 behavior

PlayerV2.cs

using UnityEngine;
using System.Collections;

public class PlayerV2 : MonoBehaviour {

    public GameObject pokeBall;
    public GameObject rock;
    public GameObject bait;
    public static int totalBall = 30;
    public static GameObject[] projectile = new GameObject[30];
    public static int numpb = 0;
    public float speed = 0.5f;
    public static float[] time = new float[30];
    private int index = 0;
    public static float[] cx = new float[30];
    public static float[] cy = new float[30];
    private Vector2 target, myPos, direction;
    public Transform cursor;

    // Use this for initialization
    void Start()
    {

    }

    void Update()
    {
        if (CursorController.throwflag)
        //if (Input.GetMouseButtonDown(0))
        {
            CursorController.throwflag = false;
            CursorController.throwtime = Time.time;
            if (numpb < 5&& totalBall>0)
            {

                //target = new Vector2(2.5f * Input.mousePosition.x / Screen.width, 2 * Input.mousePosition.y / Screen.height);
                target = new Vector2(CursorController.x, CursorController.y);
                myPos = new Vector2(transform.position.x, transform.position.y);
                direction = target - myPos;
                direction.Normalize();
                time[numpb] = Time.time;
                //projectile[numpb] = (GameObject)Instantiate(pokeBall, myPos, Quaternion.identity);
                if (CursorController.item == 0)
                {
                    projectile[numpb] = (GameObject)Instantiate(pokeBall, myPos, Quaternion.identity);
                }
                else if (CursorController.item == 1)
                {
                    projectile[numpb] = (GameObject)Instantiate(rock, myPos, Quaternion.identity);
                }
                else if (CursorController.item == 2)
                {
                    projectile[numpb] = (GameObject)Instantiate(bait, myPos, Quaternion.identity);
                }
                if (projectile[numpb].tag == "bait")
                {
                    PokeManager.baits[PokeManager.baitCount] = projectile[numpb];
                    PokeManager.baitCount++;
                }
                cx[numpb] = cursor.transform.position.x;
                cy[numpb] = cursor.transform.position.y;
                projectile[numpb].GetComponent<Rigidbody2D>().velocity = direction * speed;
                numpb++;
                totalBall--;
            }
        }
        for (int i = 0; i < numpb; i++)
        {
            if (projectile[i] != null && Time.time - time[i] > 0.4f)
            {

                float px = projectile[i].transform.position.x;
                float py = projectile[i].transform.position.y;
                if (projectile[i].tag == "bait")
                {
                   float xdiff = System.Math.Abs(px - cx[i]);
                   float ydiff = System.Math.Abs(py - cy[i]);
                   if (xdiff <= 0.201f && ydiff <= 0.101f)
                   {
                        projectile[i].GetComponent<Rigidbody2D>().isKinematic = true;
                   }//if
                   if(Time.time - time[i] > 6.0f)
                    {
                        PokeManager.baitCount--;
                        for (int k = 0; k < 4; k++)
                        {
                            PokeManager.baits[i] = PokeManager.baits[i + 1];
                        }
                        DestroyImmediate(projectile[i].gameObject);
                        removePb(i);
                        numpb--;
                    }
                }//if
                if(py >= 1.26 && projectile[i].tag == "bait")
                {
                    projectile[i].GetComponent<Rigidbody2D>().isKinematic = true;
                }
                if (px <= -0.35f || px >= 2.79f || py <= 0.10f || py >= 1.6f)
                {
                    DestroyImmediate(projectile[i].gameObject);
                    removePb(i);
                    numpb--;
                }//if
            }//if
        }//for
    }//update

    void removePb(int i)
    {
        for(int j = i; j < numpb-1; j++)
        {
            projectile[j] = projectile[j + 1];
            time[j] = time[j + 1];
            cx[j] = cx[j + 1];
            cy[j] = cy[j + 1];
        }
    }
    
}

Player 2 behavior

PlayerV2_2.cs

using UnityEngine;
using System.Collections;

public class PlayerV2_2 : MonoBehaviour {

    public GameObject pokeBall;
    public GameObject rock;
    public GameObject bait;
    public static int totalBall2 = 30;
    public static GameObject[] projectile2 = new GameObject[30];
    public static int numpb2 = 0;
    public float speed = 0.5f;
    public static float[] time2 = new float[30];
    private int index = 0;
    public static float[] cx2 = new float[30];
    public static float[] cy2 = new float[30];
    private Vector2 target, myPos, direction;
    public Transform cursor;
    public static int item;

    // Use this for initialization
    void Start()
    {
        item = 0;
    }

    void Update()
    {
        //if (CursorController2.throwflag)
        if (Input.GetMouseButtonDown(1))
        {
            item++;
            if (item > 2)
            {
                item = 0;
            }
        }
        if (Input.GetMouseButtonDown(0))
        {
            //CursorController2.throwflag = false;
            //CursorController2.throwtime = Time.time;
            if (numpb2 < 5&& totalBall2>0)
            {

                target = new Vector2(2.5f * Input.mousePosition.x / Screen.width, 2 * Input.mousePosition.y / Screen.height);
                //target = new Vector2(CursorController2.x, CursorController2.y);
                myPos = new Vector2(transform.position.x+1.1f, transform.position.y);
                direction = target - myPos;
                direction.Normalize();
                time2[numpb2] = Time.time;
                //projectile2[numpb2] = (GameObject)Instantiate(pokeBall, myPos, Quaternion.identity);
                if (item == 0)
                {
                    projectile2[numpb2] = (GameObject)Instantiate(pokeBall, myPos, Quaternion.identity);
                }
                else if (item == 1)
                {
                    projectile2[numpb2] = (GameObject)Instantiate(rock, myPos, Quaternion.identity);
                }
                else if (item == 2)
                {
                    projectile2[numpb2] = (GameObject)Instantiate(bait, myPos, Quaternion.identity);
                }
                if (projectile2[numpb2].tag == "bait")
                {
                    PokeManager.baits[PokeManager.baitCount] = projectile2[numpb2];
                    PokeManager.baitCount++;
                }
                cx2[numpb2] = cursor.transform.position.x;
                cy2[numpb2] = cursor.transform.position.y;
                projectile2[numpb2].GetComponent<Rigidbody2D>().velocity = direction * 2.5f*speed;
                numpb2++;
                totalBall2--;
            }
        }
        for (int i = 0; i < numpb2; i++)
        {
            if (projectile2[i] != null && Time.time - time2[i] > 0.9f)
            {

                float px = projectile2[i].transform.position.x;
                float py = projectile2[i].transform.position.y;
                if (projectile2[i].tag == "bait")
                {
                   float xdiff = System.Math.Abs(px - cx2[i]);
                   float ydiff = System.Math.Abs(py - cy2[i]);
                   if (xdiff <= 0.201f && ydiff <= 0.101f)
                   {
                        projectile2[i].GetComponent<Rigidbody2D>().isKinematic = true;
                   }//if
                   if(Time.time - time2[i] > 6.0f)
                    {
                        PokeManager.baitCount--;
                        for (int k = 0; k < 4; k++)
                        {
                            PokeManager.baits[i] = PokeManager.baits[i + 1];
                        }
                        DestroyImmediate(projectile2[i].gameObject);
                        removePb(i);
                        numpb2--;
                    }
                }//if
                if(py >= 1.26 && projectile2[i].tag == "bait")
                {
                    projectile2[i].GetComponent<Rigidbody2D>().isKinematic = true;
                }
                if (px <= -0.35f || px >= 2.79f || py <= 0.10f || py >= 1.6f)
                {
                    DestroyImmediate(projectile2[i].gameObject);
                    removePb(i);
                    numpb2--;
                }//if
            }//if
        }//for
    }//update

    void removePb(int i)
    {
        for(int j = i; j < numpb2-1; j++)
        {
            projectile2[j] = projectile2[j + 1];
            time2[j] = time2[j + 1];
            cx2[j] = cx2[j + 1];
            cy2[j] = cy2[j + 1];
        }
    }
    
}


Please log in to post comments.