Class Notes & Exercises


Week 1

Module Information Booklet


Class note

In today's class, lecturer brief us about the module information booklet and talked about what will we be learning in the following classes. We will have 3 assignments and several class exercises. In the final product, we will be showing a well-developed digital 2D game with nice visuals, audio, storyline and more. We are to develop our game from assignment 1 to final assignment. 


Exercise
Game Analysis
1. The Cry from The Classroom by Yong Jeen Jeen


Story

This is the most important component in this game. The storyline is brilliant, complexed, well-written, very addictive and it makes me keeps on wanting to play it to know the whole story. The way of presenting the story is very interactive, it is the elements that encourages players to keep on playing it. But unfortunately, I can't get the whole story as the game file is way too big and I might have not enough storage for it or something else. I would really like to know the whole story, who's the one haunting the school, who is the real assaulter, what happened to the victims.  

Challenge

The obvious challenge in this game are avoiding getting killed by the ghost and attacking the ghost. There are ghost inside the school that haunting the students there. Another hidden challenge is to solve the puzzle of the story. Even though this is not required, but as a mystery lover and just a merely thinking human, we will always try to figure out the situation, the truth of the mystery before we get to know the ending. We collect the papers in the game, read through it and understand the story, our brain are triggered to solve the mystery right away. As we progress into deeper level of the game, we get more and more invest in the mystery and trying to solve it, we see it as a challenge. This is what makes mystery game/story interesting. And I like this element added in this game. 



Strategy

There are strategies to avoiding getting killed and also attacking the ghost. We need to be near to the ghost because the attack range in quite short, but at the same time, we need to act fast to avoid the ghost touch us as this will kill us. So, in order to win, player needs to be brave and take risks, go near to the ghost, attack fast, and retreat in time when the ghost come too near. 

Chance

This game doesn't really give chances, once got attacked, we lost the game instantly, but we get to play again at the same level. It saves player's progress, so player only need to play again at the same level, no need to start over. In another perspective, this can be count as a chance too, as player gets to play again. 


Choice

In this game, player doesn't have choices. We only need to go ahead, attack the ghost, collect the story pieces and finish the game. We do not need much decision making except when making strategies to attack ghost. I think the thing we can improve here is give players a certain number of chances to get story pieces, this means that the players cannot get all the story pieces, they need to choose which story pieces they can read and which one they abandon. Then, at the final part, we can give player to guess the who's the real assaulter, what ending is it and etc. This allows players to think critically and make choices, the choices they make will affect their final thinking outcome or guess at the story ending. It will make the game more interesting and let the player fully invest in this story game. 

Luck

This game also doesn't have luck elements, but if improve it with the suggestion above, it will have the luck element too. The player's luck at choosing important story pieces will lead them to correct guessing at the story ending. 

Visual & Sound

The ambience sound is scary and it suits the game so well, but I feel like the sound effect can be better especially the attack sound. I like the art style very much, it is so well done, all the little details from the elements, backgrounds are so well drawn. Overall, This might be my favourite game from seniors, as I can't really play all of it. I like it a lot and I hope that I can produce this kind of interesting game too.

Bug

There is a little bug in this game. I fell from the existing floor as I move way too left. And once I am going down, I can't get back up again. I need to reload the game and play again from starting point. 

Reflection

I gained a lot of insight with the exercise given, I understand what we are going to do and learn in this class. I find the exercise interesting as I can actually play some games. With analyzing, I found out so much more about game that I have never noticed before. I also get to understand the importance of the gameplay element like challenge, strategy, chance, luck and more. These all affect the player experience, playability and usability of a game.



Week 2
Class Activity
As me and some of my classmates went to a conference on this class, we weren't able to attend the class and need to do the class activity afterwards. After getting some clarifications on this class activity from our fellow classmates and lecture, we start on the activity as soon as possible. From our understanding, we need to find existing game and use 3(i) or scamper methods to modify the game. We can choose more than one game to modify. After some discussion, we have an idea to modify Fruit Ninja and Cooking Madness. We use Scamper method to successfully create this fun idea. Click this link to view our presentation slides or click the video to view our presentation. 




Reflection

We do some research to understand the topic more since we did not attend the class. We enjoyed the process of doing this class activity together and we have a lot of fun together. It was a great brainstorming session and I get to analyze game and do some creative combination from different game myself. It is inspiring to me and I got a chance to explore my creativity here. 



Week 3
Class note
In today's class, we learned about Unity. Lecturer taught us several techniques using lecture notes and tutorial to teach us create a mini angry bird similar game and moving car mini game. I wanted to create a UFO smashing mini game but I haven't really learned how to smash a floating object yet so the outcome looks more like smashing a purple burger. The process is so fun and I get to be creative about it. The moving car game is a little bit hard to follow as I was still unfamiliar with Unity but once I get the hang of it, it wasn't bad. It was an interesting learning experience too. 

Our exercise back home is to create a rotating plane. We need to solve the warning issue in the unity, make the plane move and etc like the screenshot below. It was fun and entertaining, I have more confidence using Unity after these exercises and practicing. 

Class Activity
An Angry bird-similar game

Exercises
1. Sliding car, Hitting all the obstacles
Code:
For car movement:
public class NewBehaviourScript : MonoBehaviour
{
    public float speed = 20.0f ;

    public float turnspeed;

    public float horizontalInput;

    public float verticalInput;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame


    /*
    ####################################
    THE START OF PLAYER CONTROLLER
    ####################################

*/
    void Update()
    {
         // Apply Horizontal Input to manage Left/Right
         horizontalInput = Input.GetAxis("Horizontal");

         // Move Vehicle Forward/Backward
         verticalInput = Input.GetAxis("Vertical");
         
         // Move Vehicle Forward
         transform.Translate(Vector3.forward * Time.deltaTime * speed * verticalInput);

         // Move Vehicle Left / Right
         transform.Translate(Vector3.right * Time.deltaTime * turnspeed * horizontalInput);
         
        // Apply Horizontal Input to manage Left/Right
        // transform.Rotate(Vector3.up, Time.deltaTime * turnspeed * horizontalInput);
       
       
    }
   
}

For camera:
public class FollowPlayer : MonoBehaviour
{

    public GameObject player;
    public Vector3 offset = new Vector3(0, 5, -7);

    // Start is called before the first frame update
    void Start()
    {
       
    }

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

    void LateUpdate()
    {
    transform.position = player.transform.position + offset;
    }
 
}


2. Turning car, Avoid all the obstacles
Code:
For car movement:
public class NewBehaviourScript : MonoBehaviour
{
    public float speed = 20.0f ;

    public float turnspeed;

    public float horizontalInput;

    public float verticalInput;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame


    /*
    ####################################
    THE START OF PLAYER CONTROLLER
    ####################################

*/
    void Update()
    {
         // Apply Horizontal Input to manage Left/Right
         horizontalInput = Input.GetAxis("Horizontal");

         // Move Vehicle Forward/Backward
         verticalInput = Input.GetAxis("Vertical");
         
         // Move Vehicle Forward
         transform.Translate(Vector3.forward * Time.deltaTime * speed * verticalInput);

         // Move Vehicle Left / Right
         // transform.Translate(Vector3.right * Time.deltaTime * turnspeed * horizontalInput);
         
        // Apply Horizontal Input to manage Left/Right
        transform.Rotate(Vector3.up, Time.deltaTime * turnspeed * horizontalInput);
       
       
    }
   
}


3. Plane
Code:
For plane movement:
public class PlayerControllerX : MonoBehaviour
{
    public float speed;
    public float verticalInput;
   

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // get the user's vertical input
        verticalInput = Input.GetAxis("Vertical");

        // move the plane forward at a constant rate
        transform.Translate(Vector3.forward * Time.deltaTime * speed);

        // tilt the plane up/down based on up/down arrow keys
        transform.Translate(Vector3.up * Time.deltaTime * speed * verticalInput);
    }
}

For rotating propeller:
public class Propellerrotation : MonoBehaviour
{
     public float rotationSpeed;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
        transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);

    }
}

For camera:
public class FollowPlayerX : MonoBehaviour
{
    public GameObject plane;
    public Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.position = plane.transform.position + offset;
    }
}


Reflection

I do a lot of exercises and practices in Unity this week. Thanks to the lesson given by the lecturer, with the exercises and practices, I feel confident to create something using Unity. I learned a lot in this week and I hope that I can continue to keep it up in class. 



Week 4
Class Notes
Today, we learned about Destroy out of bound, spawn manager and detect collision. We are to create a mini game using the assets given. It's a simple shooting game. 

For Player Control:
public class PlayerControl : MonoBehaviour
{

    public float horizontalInput;
    public float speed = 10f;
    private float xRange = 20f;
    public GameObject projectilePrefab;
   

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
       
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);

         if (transform.position.x < -xRange)
        {
         transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
         }
         
         else if(transform.position.x > xRange)
         
        {
            transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
           
         }

         if (Input.GetKeyDown(KeyCode.Space))
         {
            Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
         }
    }
}


For Destroy Out of Bounds:
public class NewBehaviourScript : MonoBehaviour
{

    private float topBounds = 30.0f;
    private float lowerBounds = -10.0f;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       if(transform.position.z > topBounds)
        {            Destroy(gameObject);        }

        else if(transform.position.z < lowerBounds )
        {     Debug.Log("Game Over!");        
            Destroy(gameObject);        }
       
     
    }
}

For Spawn Manager:
public class SpawnManager : MonoBehaviour
{

    public GameObject[ ] animalPrefabs;
    private float spawnRangeX = 20;
    private float spawnPosZ = 20;
    private float startDelay = 2f;
    private float spawnInterval = 1.5f;

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);
    }

    // Update is called once per frame
    void Update()
    {
       
    }    
         
    void SpawnRandomAnimal()
    {
         {
            Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
            int animalIndex = Random.Range(0, animalPrefabs.Length);
           Instantiate( animalPrefabs[animalIndex],spawnPos, animalPrefabs[animalIndex].transform.rotation);
        }
    }
}

For Detect Collision:
public class DetectCollision : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

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

     private void OnTriggerEnter(Collider other)
     {
        Destroy(gameObject);
        Destroy(other.gameObject);
    }
 
}

For Move Forward:
public class MoveForward : MonoBehaviour
{

    public float speed = 40f;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
}

Class Activity Product:


Reflection

I found the class activity interesting. I have some difficulties following in class this time as I encountered some error that I done in coding. One mistakes followed by another and I cannot even try the game out. But thankfully, I have guidance from my lecturer to point out the problem and solve it out. I managed to finish the class activities in class. It is not hard to understand the coding but it is hard to do it, like I don't know which part I write it wrong. But I hope with more practices, I managed to improve my skills. 



Week 5
Class Notes
In today's class, we continued to explore Unity, we used the assets given to create a small game that the players have to avoid the obstacles. We even have chance to add in music and sound effect too. We also learned some new things like adding game over when the game is ended, moving the background, explore the animation in game and more. 

Code:
For Player Control:
public class PlayerControl : MonoBehaviour
{
    private Rigidbody playerRb;
    public float gravityModifier;
    public float jumpForce;
    public bool isOnGround=true;
    public bool gameOver;
    private Animator playerAnim;
    public ParticleSystem explosionParticle;
    public ParticleSystem dirtParticle;
    public AudioClip jumpSound;
    public AudioClip crashSound;
    private AudioSource playerAudio;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        Physics.gravity *= gravityModifier;
        playerAnim = GetComponent<Animator>();
        playerAudio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerRb.AddForce(Vector3.up * jumpForce,ForceMode.Impulse);
            isOnGround = false;
            playerAnim.SetTrigger("Jump_trig");
            dirtParticle.Stop();
            playerAudio.PlayOneShot(jumpSound,1.0f);
        }
    }  
   
    private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag ("Ground"))
    {
       
        isOnGround = true;
        dirtParticle.Play();
    }

    else if (collision.gameObject.CompareTag ("Obstacles"))
    {
        gameOver = true;
        playerAnim.SetBool("Death_b", true);
        playerAnim.SetInteger("DeathType_int", 1);
        explosionParticle.Play();
        dirtParticle.Stop();
        playerAudio.PlayOneShot(crashSound,1.0f);
        Debug.Log("Game Over!");
       
    }
}
   
}

For Move Left:
public class Moveleft : MonoBehaviour
{
    private PlayerControl playerControllerScript;
    private float speed = 5;
    private float leftBound = -15f;
   
    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerControl>();
    }

    // Update is called once per frame
    void Update()
    {
       if(playerControllerScript.gameOver == false)
       
       {
        transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
       
       if(transform.position.x < leftBound && gameObject.CompareTag("Obstacles"))
       {
        Destroy (gameObject);
       }
    }
}

For Spawn Manager:
public class SpawnManager : MonoBehaviour
{
    public GameObject obstaclePrefab;
    private Vector3 spawnPos = new Vector3(25,0,0);
    private float startDelay = 2;
    private float repeatRate = 2;
    private PlayerControl playerControllerScript;

    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerControl>();
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
       
    }
    void SpawnObstacle()
{
     if(playerControllerScript.gameOver == false)
    {
        Instantiate(obstaclePrefab, spawnPos,obstaclePrefab.transform.rotation);
    }
}

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

For Repeat Background:
public class RepeatBackground : MonoBehaviour
{
    private Vector3 startPos;
    private float repeatWidth;

    // Start is called before the first frame update
    void Start()
    {
        startPos = transform.position;
        repeatWidth = GetComponent<BoxCollider>().size.x / 2;
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x < startPos.x - repeatWidth)
        {
            transform.position = startPos;
        }
       
    }
}

Class Activity Product:

Reflection

I have a hard time following the class as I keep on making mistake. I could not finish the class activity in class so I have to do it at home myself. From this class activity, I know that I still have very limited knowledge in Unity and I really do not understand some of the coding. Thus, the next time I do class activity, I want to add in description on every coding I do, I hope that this will help me understand the coding better. Besides that, I now know that animation really help to bring game to the next level, so is the sound effect and music. Before adding these, the game seems stiff, but with the animation and sound, it feels like the game comes alive. 


Week 7
Class Notes

Today, we have online class. Our lecturer taught us using the same coding techniques to do for 2D game. Since our project is about 2D game, we started to learn back the same technques but for applying it for 2D. It is pretty easy to follow through as we already learned the same things before. And as I get more and more comfortable and used to the coding in Unity, I trust my instinct and memories more to apply the codes. 

For Player Control:
public class PlayerController : MonoBehaviour
{
    private float movespeed = 10.0f;
    private float horizontalInput;
    private Rigidbody2D playerRb;
    public float jumpforce = 10.0f;
    public bool isOnGround = true;
    public bool canDoubleJump = false;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.right * Time.deltaTime * movespeed * horizontalInput);

        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            playerRb.AddForce(Vector3.up * jumpforce, ForceMode2D.Impulse);
            isOnGround = false;
            canDoubleJump = true;
        }
        else if (Input.GetKeyDown(KeyCode.Space) && !isOnGround && canDoubleJump)
        {
            playerRb.AddForce(Vector3.up * jumpforce, ForceMode2D.Impulse);
            isOnGround = false;
            canDoubleJump = false;
        }
    }

    // OnCollisionEnter2D method must be outside the Update method
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "MyGround")
        {
            isOnGround = true;
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
      if(collision.gameObject.tag == "MyCoins")
      {
        Destroy(collision.gameObject);
      }

    }
}

For Door Trigger:
public class Doortrigger : MonoBehaviour
{
    public GameObject door;
    private GameObject player;
    private Animator anim;


    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Player");
        anim = door.GetComponent<Animator>();
    }

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

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.name == "Player")
        {
            //door.SetActive(false);
            anim.Play("Door open");
        }
    }
}  

Class Activity Product:

Reflection

After having this simple tutorial class to create 2D game, I feel I have more confident into creating my own game using the same techniques, since my idea is not that far off from this. I did get lost in the middle of the class, but was able to get back to it quickly as this time we have video to rewatch it. I do think that video helps me more as I can keep on rewatch it if I get lost in the middle of following the class and I did not need to bother and ask for help from someone else, I can understand the concept and learn from the mistake myself. 


Week 8
Independant Learning Week


Week 9

Class Notes

We continue to learn to create simple 2D game using the last exercise product. In today's class activity, we added in coins animation, coin count, condition to open door, add in door animation, key to open door and more. 

For Player Control:

public class PlayerController : MonoBehaviour
{
    private float movespeed = 10.0f;
    private float horizontalInput;
    private Rigidbody2D playerRb;
    public float jumpforce = 10.0f;
    public bool isOnGround = true;
    public bool canDoubleJump = false;
    public GameObject gemkey;
    public GameObject keylocation;
    bool isLeftWall = false;
    bool isRightWall = false;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.right * Time.deltaTime * movespeed * horizontalInput);

        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            playerRb.AddForce(Vector3.up * jumpforce, ForceMode2D.Impulse);
            isOnGround = false;
            canDoubleJump = true;
        }
        else if (Input.GetKeyDown(KeyCode.Space) && !isOnGround && canDoubleJump)
        {
            playerRb.AddForce(Vector3.up * jumpforce, ForceMode2D.Impulse);
            isOnGround = false;
            canDoubleJump = false;
        }

         else if (Input.GetKeyDown(KeyCode.Space) && isLeftWall)
        {
            playerRb.AddForce(Vector2.up * jumpforce, ForceMode2D.Impulse);
            playerRb.AddForce(Vector2.right * jumpforce, ForceMode2D.Impulse);
            isLeftWall = false;
        }

         else if (Input.GetKeyDown(KeyCode.Space) && isRightWall)
        {
            playerRb.AddForce(Vector2.up * jumpforce, ForceMode2D.Impulse);
            playerRb.AddForce(Vector2.left * jumpforce, ForceMode2D.Impulse);
            isRightWall = false;
        }
    }

    // OnCollisionEnter2D method must be outside the Update method
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "MyGround")
        {
            isOnGround = true;
            canDoubleJump = true;
        }

        else if (collision.gameObject.tag =="Left Wall")
        {
            isLeftWall = true;
        }

         else if (collision.gameObject.tag =="Right Wall")
        {
            isRightWall = true;
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
      if(collision.gameObject.tag == "MyCoins")
      {
        GameData.mycoins ++; //mycoins = mycoins +1
        Debug.Log(GameData.mycoins);

        collision.gameObject.GetComponent<Animator>().Play("Coin_collected");
       
        if(GameData.mycoins == 2)
      {
        Instantiate(gemkey, keylocation.transform.position, keylocation.transform.rotation);
      }
       
        Destroy(collision.gameObject, 1);
      }

    }
}

For GemKey Control:

public class GemKeyControl : MonoBehaviour
{
   
    // Start is called before the first frame update
    void Start()
    {
       
    }

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

    void OnTriggerEnter2D(Collider2D collsion)
    {
        GameData.haskey = true;
        Debug.Log(GameData.haskey);
        Destroy(gameObject);
    }
}

For Game Data:

using TMPro;

public class GameData : MonoBehaviour
{
    public static int mycoins;
    public TMP_Text myCoinsText;
    public static bool haskey = false;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        myCoinsText.text = mycoins + "";
    }
}

For Door Trigger:

public class Doortrigger : MonoBehaviour
{
    public GameObject door;
    private GameObject player;
    private Animator anim;


    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Player");
        anim = door.GetComponent<Animator>();
    }

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

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.name == "Player" && GameData.haskey==true)
        {
           
            //door.SetActive(false);
            anim.Play("Door open");
        }
    }
}  

Class Activity Product:


Reflection

I didn't able to go to school this week because I am not feeling well. Thus, I am really grateful that we have the recorded session for this week's class. I watch the video and follow the class in my own room. It was pretty smooth mostly, but I get stuck at the coin animation. My coins keep on moving from the first coin's position, I spent a lot of time to figure out what is wrong with my animation or code or the placement. But it turns out that I only need to tick the root motion. Thanks to my classmates, Sze Ching to teach me about this. So far, I think I get to follow the class better, thanks to our lecturer and my friends' guidance too. 



Week 10

Class Notes

Today, we have online class. We continue the last class activity, this time, we added animation for our player and enemy to slash. We start from adding animation from the template given by our lecturer, we use back the same technique, cut the image into individual image and make it into animation just like the last class activity for coin animation. Then, we adjust the animation to our peferences, the speed, the flow and such. 

After that, we start to add in enemy animation, in today's activity, we only want to do the player slashing the enemy and the enemy die. So, we add in scripts in both player and enemy controller, box collider, to trigger the action. We also adjust the animation of enemy dying to be more smooth, exp: adding fade out. 

Code:

For Player Controller:

private float movespeed = 10.0f;
    private float horizontalInput;
    private Rigidbody2D playerRb;
    public float jumpforce = 10.0f;
    public bool isOnGround = true;
    public bool canDoubleJump = false;
    public GameObject gemkey;
    public GameObject keylocation;
    bool isLeftWall = false;
    bool isRightWall = false;
    private Animator anim;
    public bool facingRight = true;


    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.right * Time.deltaTime * movespeed * horizontalInput);

        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            playerRb.AddForce(Vector3.up * jumpforce, ForceMode2D.Impulse);
            isOnGround = false;
            canDoubleJump = true;
        }
        else if (Input.GetKeyDown(KeyCode.Space) && !isOnGround && canDoubleJump)
        {
            playerRb.AddForce(Vector3.up * jumpforce, ForceMode2D.Impulse);
            isOnGround = false;
            canDoubleJump = false;
        }

        anim.SetFloat("MoveSpeed", Mathf.Abs(horizontalInput));
        anim.SetBool("isGrounded", isOnGround);

        if(horizontalInput>0 && facingRight == false)
        {
            Flipit();
        }
        else if(horizontalInput<0 && facingRight == true)
        {
            Flipit();
        }

        if(Input.GetAxis("Fire1")>0)
        {
            anim.SetTrigger("Attack");
        }
    }

    void Flipit()
    {
        Vector3 pScale = transform.localScale;
        pScale.x *= -1;
        transform.localScale = pScale;
        facingRight = !facingRight;
    }

    // OnCollisionEnter2D method must be outside the Update method
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "MyGround")
        {
            isOnGround = true;
            canDoubleJump = true;
        }

        else if (collision.gameObject.tag =="Left Wall")
        {
            isLeftWall = true;
        }

         else if (collision.gameObject.tag =="Right Wall")
        {
            isRightWall = true;
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
      if(collision.gameObject.tag == "MyCoins")
      {
        GameData.mycoins ++; //mycoins = mycoins +1
        Debug.Log(GameData.mycoins);

        collision.gameObject.GetComponent<Animator>().Play("Coin_collected");
       
        if(GameData.mycoins == 2)
      {
        Instantiate(gemkey, keylocation.transform.position, keylocation.transform.rotation);
      }
       
        Destroy(collision.gameObject, 1);
      }

    }


For Enemy Control:
 public int enemyHP = 10;
    Animator anim;


    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

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

    void OnTriggerEnter2D(Collider2D collision )
    {
        if(collision.gameObject.name == "SlashPoint")
        {
          Debug.Log("I'm Hit!");

          enemyHP -= 5;

          if(enemyHP <= 0)
          {
            anim.Play("Enemy_die");
            Destroy(gameObject, 1);
          }
        }

     
    }

Product:


Reflection
In today's class, I got lost a bit in the middle when adjusting the player walking. But I quickly get it back when lecturer give us time to follow back the class and do the code. It was a fun and interesting learning experience, and I believe that this activity will helps a lot in my project as we will definitely use these animation and triggers. 


Week 11
Class Notes
In today's class, we continue our class activity from last week. We do the health bar, where the life will decreases if the player got hurt. We then do a knockback motion for player if the player get attack. 

For Game Data:
 public static int mycoins;
    public TMP_Text myCoinsText;
    public static bool haskey = false;
    public Image HPBar;
    private float HPPercent;
    private float maxiHP;
    public GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        maxiHP = PlayerController.playerHP;
    }

    // Update is called once per frame
    void Update()
    {
        myCoinsText.text = mycoins + "";
        HPPercent = PlayerController.playerHP / maxiHP;
        HPBar.fillAmount = HPPercent;
        //Debug.Log(HPPercent);

        if(PlayerController.playerHP <= 0)
        {
            //Debug.Log("Player is Dead");
            player.GetComponent<Animator>().Play("Player_Die");
            player.GetComponent<PlayerController>().enabled = false;
        }
    }

For KnockBack:
 public float KnockBackForce = 10f;
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

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

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "MyEnemy")
        {
          if(collision.transform.position.x > transform.position.x)
          {
           rb.velocity = new Vector2(-KnockBackForce, KnockBackForce);
          }
          else
          {
           rb.velocity = new Vector2(KnockBackForce, KnockBackForce);
          }
         
        }
    }

Product: 


Reflection

Comments

Popular Posts