Vignana-Jyothi / vnr-ar-vr

0 stars 0 forks source link

Game Design : add win/loose, sounds & bonus points. #7

Closed head-iie-vnr closed 4 months ago

head-iie-vnr commented 4 months ago

Scope

head-iie-vnr commented 4 months ago

Work Scope: End game with 'Win Text' if all the coins are selected.

Set Win criteria.

The total coins are 4 on the board. If the collected coin count matches then set the WinText.

using UnityEngine;
using UnityEngine.UI;

using TMPro;

public class ScoreManager : MonoBehaviour
{
    public static ScoreManager instance;
    public TextMeshProUGUI scoreText;
    public GameObject winText;
    private int score = 0;
    private static int MAX_COINS = 4; 

    void Awake()
    {
        Debug.Log("Called Score Manager");
        if (instance == null)
        {
            instance = this;
            Debug.Log("new instane is created");
            winText.gameObject.SetActive(false);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    void Start()
    {
        UpdateScoreText();
    }

    public void AddScore(int amount)
    {
        score += amount;
        UpdateScoreText();
        if (score == MAX_COINS) {
            Debug.Log("You Win");
            winText.gameObject.SetActive(true);
        }
    }

    void UpdateScoreText()
    {
        scoreText.text = "Score: " + score;
    }
}

ScoreManager > Inpector > 'Score Manager' script > variables section Bind the variables to the UI components image

Now you should see that WinText will be shown once you collect all the coins.

image

head-iie-vnr commented 4 months ago

Edit the settings of the WinText properly

image

head-iie-vnr commented 4 months ago

Create GameoutArea 'plane' object & Create a script 'GameoutArea.cs'

Attach the script as component to the GameoutArea object.

Set colors to the plane. Enable 'Collide' and Trigger enabled.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using TMPro;

public class GameoutArea : MonoBehaviour
{
    public TextMeshProUGUI winText;

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Ball fell off the board.");

        if (other.gameObject.CompareTag("Player"))
        {
            winText.text = "Oops.. That's Sad!!";
            winText.gameObject.SetActive(true);
        }
    }
}
head-iie-vnr commented 4 months ago

When the player falls off the board, it shows like below. image

image

I have renamed the variable 'winText' as 'GameEndText' so that it can be used for both win & game over criteria.

head-iie-vnr commented 4 months ago

Download audios from freeaudio.org

image

In the ScoreManager.cs

Add instance public variable.

   public AudioSource countSound;

Update the AddScore method to play the sound

    public void AddScore(int amount)
    {
        score += amount;
        countSound.Play();
        UpdateScoreText();
        if (score == MAX_COINS) {
            Debug.Log("You Win");
            winText.gameObject.SetActive(true);
            Time.timeScale = 0;
        }
    }

Add 'AudioSource' component to the ScoreManager UI component.

image

Link the 'Coin Sound' to ScoreManager.

image

Question : If two sounds are added in Same ScoreManager UI object, then both will get played. How to stop it???

head-iie-vnr commented 4 months ago

The best strategy is to, create audio UI components which have one unique sound with them.

image

Add 'AudioSource' component to the UI elements. Then link them to the ScoreManager

image

This will make this unambigious & easy to maintain.

Note: remove Play on Awake attribute to those Audio Source components added

image

head-iie-vnr commented 4 months ago

Decide on the Position & rotation values for each zone.

Zone1: Position (0, 40, -40), Rotation (45, 0, 0) image

Zone2

Position (-40, 40, 0), Rotation (45, 90, 0) image

Zone3

Position (0, 40, 40) Rotation (45, 180, 0)

image

Zone4

Zone4: Position (40, 40, 0) Rotation (45, 270, 0) image

head-iie-vnr commented 4 months ago

For sounds signup at

https://freesound.org/

image

ScoreManager.cs


    public void AddScore(int amount)
    {
        score += amount;
        if (amount == 1) {
            coinSound.Play();
        } else {
            goldenBallCoinSound.Play();
        }
        coinsCollected++;
        UpdateScoreText();
        if (coinsCollected == MAX_COINS) {
            Debug.Log("You Win");
            winText.gameObject.SetActive(true);
            Time.timeScale = 0;
        }
    }

Added Golden ball If the coin is collected using the golden ball then there will be bonus score. image

Game will be over when all coins are collected, not by score achieved. image