Wednesday, July 20, 2016

P3D2



Made rough draft 2 of the design doc and fleshed out the setting.  Drew a bunch of stuff too but too lazy to scan them in for now.

Swapped out cube for Rynn's block-in model.  Decided to use velocity to determine her facing, but switching her movement to velocity made her movements really floaty.  Ended up forcing her to stop when no movement keys are pressed.  There is probably a better way and I spent a funny amount of time to get to the clunky solution :\

I eventually want her upper body to rotate towards player mouse.  Using a hacky indicator for now.  Still pretty sure it's not working correctly, but at least I learned how to use raycast to determine the world position of the mouse.  I understand zero amount of maths on this and will have to learn math again for sure.

Code Notes:

Seems like the new API convention or something (??) made a lot of short-hands like "Rigidbody" or "camera" not work.  Rather, "GetComponent<Rigidbody>()"seems to be the way to go.

transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (new Vector3 (transform.GetComponent<Rigidbody> ().velocity.x, 0, transform.GetComponent<Rigidbody> ().velocity.z)), Time.deltaTime * rotateSpeed);

Must look into Quaternion-related stuff.  Need to relearn geometry as well.

RaycastHit hit;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
Vector3 lookTarget = hit.point;
lookTarget.y = hit.point.y + gameObject.transform.position.y;
gameObject.transform.LookAt (lookTarget);


I'm still thrown off by the grammar of this raycasting endeavor.  Also, the LookAt seems pretty gimpy and I don't know how to control or change the "forward axis".  Might not be very usable if I want to use this to define shooting direction.  Found some complicated math stuff that again has to do with angles.  Geometry.

Next Steps:
  • Get the pointer to rotate towards mouse direction at a fixed speed
  • Spawn projectiles flying along the "front" direction of the pointer on mouse click (not necessarily towards mouse)
  • Make some environment assets that can be placed
  • Separate out Rynn's upper body so it can rotate properly instead of the pointer


Tuesday, July 19, 2016

P3D1

Venturing into 3D this time.  Picking up the game idea I had a while back, let's see how far this one gets :)

Day 1:


  • Started a new project.
  • Stole a sample scene.
  • Figured out how to attach camera to player, then swapped default player out for a cube.  Animation is for noobs super advanced people.
  • Made a movement script that includes jump.  The directional movements are currently analogue, might make that physics-based too since I'm already using rigidbody for jump for convenient gravity.
  • blocked in some assets.
  • Refresher on the tiny bit of C# I knew.


Code notes:

public KeyCode myKey = KeyCode.M;
if (Input.GetKeyDown(myKey)){
}
void OnCollisionEnter (Collision col) {
if (col.gameObject.tag == "Ground") {
isGrounded = true;
}
}

 Vector3 playerInfo = player.transform.transform.position;
gameObject.transform.position = new Vector3 (playerInfo.x + cameraDistOffsetX, playerInfo.y + cameraDistOffsetY, playerInfo.z - cameraDistOffsetZ);

Next Steps:


  • Make a less cubish player character
  • Get it to face whichever direction it moves in
  • Make it possible for the player to have a dialogue with Rynn
  • Probably figure out a rough art style??

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 .