Monday, June 8, 2015

P2D6

Used another bunch of rather crude int triggers (I really need to figure out how bool works) to make enemy movement direction random.

Randomized all the timers just a little.

Made graphic for background and spawner.

Defined walls and made enemies turn back when they hit the wall.  Currently player can still walk past the walls, I think I can make the player's speed 0 if he walks into the wall.  Might need to tag top/bot and side walls differently so it only affect the proper axis.

I want to make sure minions face the other way when they walk left, like the player does.

Still need to make the minions shoot at player, and hurt the player if they overlap.

I want to make the minions not overlap each other as much.  I wonder if I can define a secondary collision box tagged differently just for this?

Need UI and scoring.

I'd like to make animations and fx for everything too.

Eventually need sounds.


Here's the round-about way I did the randomized turning/pausing etc:


void Start () {
        timeStamp5 = Time.time;
        timeStamp6 = Time.time;
        ispaused = 0;
        randomPause = 0f;
        randomWalk = 0f;
        isFaced = 2;
        //randomFacing = 1;
    }
   
    void Update () {

        if (isFaced == 2)
        {randomFacingDice = Random.Range (-1, 2);
         randomFacingDice2 = Random.Range (-1,2);

        }

        if (randomPause == 0)
        {
            randomPause = Random.Range (0.1f, 1f);
        }
        if (randomWalk == 0)
        {
            randomWalk = Random.Range (0.1f, 1f);
        }

        if ((ispaused == 0) && Time.time - timeStamp6 >= pauseTime + randomPause)
        {
            timeStamp5 = Time.time;
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (randomFacingDice*enemySpeed, randomFacingDice2*enemySpeed);
            ispaused = 1;
            randomPause = 0;
            isFaced = 2;
        }
        if ((ispaused == 1) && Time.time - timeStamp5 >= pauseRate + randomWalk) {
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
            timeStamp6 = Time.time;
            ispaused = 0;
            randomWalk = 0;
        }
   
    }

Friday, June 5, 2015

P2D5

Made an invisible spawner that spawns enemies at a regular interval.  Enemies now walk to the right side of the screen and pause periodically (also regular interval.)

Next steps: make enemies walk in a circle around the outer edge of the screen; have enemies fire towards player when they pause; make enemy movement directions/pause intervals/spawn intervals more random; display player score

Note: I couldn't figure out the proper grammar when checking for a false bool with another condition (&&), so I ended up making the pause-checker an int instead.  I'm sure this is bad in some way.  I will look into how to properly do this with bool instead.

Codes:
Added enemyControl for movement.  Using 2 timestamps to calculate pause interval and pause time.  Seems kind of clunky?
public class enemyControl : MonoBehaviour {
    private float timeStamp5;
    public float enemySpeed = 1f;
    public float pauseRate = 2f;
    private float timeStamp6;
    private int ispaused;
    public float pauseTime = 0.8f;

    // Use this for initialization
    void Start () {
        timeStamp5 = Time.time;
        timeStamp6 = Time.time;
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (enemySpeed, 0);
        ispaused = 0;
    }
   
    // Update is called once per frame
    void Update () {

        if ((ispaused == 0) && Time.time - timeStamp6 >= pauseTime)
        {
            timeStamp5 = Time.time;
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (enemySpeed, 0);
            ispaused = 1;
        }
        if ((ispaused == 1) && Time.time - timeStamp5 >= pauseRate) {
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
            timeStamp6 = Time.time;
            ispaused = 0;
        }
   
    }

    void OnBecameInvisible()
    {
        DestroyObject(this.gameObject);
    }
}


Made spawner script that has an initial pause at game start, then spawn things at a fixed rate:

public class enemyspawn : MonoBehaviour {
    private float timeStamp3;
    public float spawnDelay;
    private bool firstSpawn;
    public GameObject enemy1;
    public float firstDelay = 0.8f;

    // Use this for initialization
    void Start () {
        timeStamp3 = Time.time;
        firstSpawn = true;
        //Mathf.Clamp (1, 10);  //how to define min/max value
   
    }
   
    // Update is called once per frame
    void Update () {
        if (firstSpawn && Time.time - timeStamp3 >= firstDelay)
        {
            Instantiate (enemy1, transform.position, Quaternion.identity);
            firstSpawn = false;
            Debug.Log (firstSpawn);
            timeStamp3 = Time.time;
        }

        else if (Time.time - timeStamp3 >= spawnDelay)
        {
            Instantiate (enemy1, transform.position, Quaternion.identity);
            timeStamp3 = Time.time;
        }

    }
}


Bonus learning: how to clamp a value between 1-10 (untested)

Mathf.Clamp (1, 10);

Thursday, June 4, 2015

P2D4

Made player and enemies into a trigger; gave them "enemy" tag;  the projectile now destroys up to 2 enemies.

void OnTriggerEnter2D(Collider2D coll)
    {
        Debug.Log ("collided with" + coll.gameObject.name);
        if (coll.gameObject.tag == "enemy")
        {
            Destroy (coll.gameObject);
            gameControl addScore = GetComponent<gameControl>();
            addScore.scoring ();
            hitNum += 1;
        }
        if (hitNum >= 2)
        {
            DestroyObject(this.gameObject);
        }
    }

problem? addScore.scoring isn't working :(

gameControl.cs function below:

    public void scoring ()
    {
        Debug.Log ("scoring is being called");
        playerScore += 1;
    }


Error, on the addScore.scoring(); line:

NullReferenceException: Object reference not set to an instance of an object
projectile_sc.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/projectile_sc.cs:36)


Note:
Have the ranged minions walk around the edge of arena, shooting at the player.  Maybe put in obstacles for player to hide behind.

Update:
Made score a static variable and scoring a static function.
A static function can be called without an object reference.
 Scoring, in gameControl script:
     public static void scoring ()
    {
        playerScore += 1;
        Debug.Log (playerScore);
    }

 Calling it from another script:

            gameControl.scoring ();


Had a Singleton tutorial from herbie.
It's essentially a static public class that creates an instance of itself when called, so other scripts can access the non-static functions attached to it.  Should only be used when there is only 1 of it, like in scoring.

public class scores : MonoBehaviour {
    protected static scores m_instance = null;
    protected int m_currentScore = 0;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    public static scores instance ()
    {
        if ((m_instance) == null)
        {
            m_instance = new GameObject("scoresSingleton").AddComponent<scores>();
        }
        return m_instance;
    }

    public void increaseScore()
    {
        m_currentScore++;
        Debug.Log (m_currentScore);
    }
}


GetComponent only works on things attached to current object.
FindComponentbyType/Name are slower functions and I should avoid using these in updates.
When using either, store the results in an variable and just use those.

Use the "xxx = null" "if xxx = null, xxx = return value" to make sure xxx only gets defined once.

Wednesday, June 3, 2015

P2D3

Managed to have the projectile firing at mouse location.  Feels a bit floaty at the moment though.

Next goal: add a static target killable by this projectile.

Note: considering the slower firing rate by design, it might be good to have a "charge up" animation attached to him to indicate cooldown.



Did so by defining instantiate angle from Player_Control, and adding a force to the "right" direction in projectile script.

rb.AddForce (transform.right * projAcce);

Learned that if I toggled on "is Kenetic" on the projectile, AddForce would not work to make it move.  Presumeably, the Kenetic checkbox turns on and off its physics in some way.

Quaternion.identity means rotation: default.

There is no "forward" for rigidbody 2D, rather "transform.right" for x and "transform.up" for y.

Screen position is different from world position, so in calculating mouse-aiming angle, mouse position needs to be translated into world position which transforms use.

Time to brush up on basic geometry :P

playerPos = transform.position;
            mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            Vector2 dir = mousePos - playerPos;
            float angle = Mathf.Atan2(dir.y, dir.x)*Mathf.Rad2Deg;
            Instantiate(projectile1, transform.position, Quaternion.AngleAxis(angle,Vector3.forward));

Tuesday, June 2, 2015

P2D2

Got projectile to spawn on left mouse click, flipped sprite if walking opposite direction. Created prefab: projectile1.

    if(Input.GetKey(shootKey))
        {
            Instantiate(projectile1, transform.position, Quaternion.identity);
   
        }
I have no idea what "Quaternion.identity" means or does.

Need to figure out how to change the spawn position, so it shoots out of the wand instead of his face.

Currently projectile only goes 1 direction.  Need to figure out how to make it go towards mouse.

Need to figure out how to kill projectile after a certian distance.

Need to figure out how to define firing rate.

 Still have to add collider.


Mini update:

Figured out how to add delays between shots, and how to kill bullet after a certain time using a timeStamp variable, defined with Time.time .




Monday, June 1, 2015

P2D1

Reviewed how to create simple player control.  This should be a lot less complicated due to lack of jumping/ground detection.

Made a sprite.

Set up new project.

Achieved player movement in 8 directions.


Phrases used:

if(Input.GetKey (KeyCode.A)&&Input.GetKey (KeyCode.W) )
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2 (-speedForce, speedForce);
        }

 

Reboot: 2D arena 01

Practice project, theme is veigar attacking minions

MVP:
1 player character with an aimeable "skillshot" attack that vanishes after colliding with 2 enemies.
No gravity
1 kind of stationary enemy that spawns randomly
Keeping score

Bonus:
Collider/blockers
Max range on player attack
enemies move towards player
scoring/ scoring streaks
player health bar
2ndary attack mode