Lesson08

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

public class AudioManager : MonoBehaviour
{

    private AudioSource audio;

    public AudioClip sound01;
    public AudioClip sound02;
    public AudioClip sound03;

    void Start()
    {
        audio = gameObject.AddComponent<AudioSource>();
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            audio.PlayOneShot(sound01);
        }
        else if (other.gameObject.tag == "Target")
        {
            audio.PlayOneShot(sound02);
        }
        else audio.PlayOneShot(sound03);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bounce : MonoBehaviour
{

    public float bounce = 10.0f;
    public int scorepoint = 10;
    GameObject go;

    void Start()
    {
        go = GameObject.Find("GameObject");
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            Vector3 norm = other.contacts[0].normal;
            Vector3 vel = new Vector3(-norm.x, 0f, -norm.z);
            other.rigidbody.AddForce(vel.normalized * bounce, ForceMode.VelocityChange);
            go.GetComponent<GameManager>().AddScore(scorepoint);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Broken : MonoBehaviour
{

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            Destroy(gameObject, 0.2f);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bumper : MonoBehaviour
{

    public float bounce = 10f;

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            other.rigidbody.AddForce(0f, bounce/6, bounce, ForceMode.Impulse);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{

    public int life = 3;
    public GameObject ballPrefab;
    public Text textGameOver;
    private int score;
    private Text textScore;
    private Text textLife;
    private bool inGame;

    void Start()
    {
        textGameOver.enabled = false;
        score = 0;
        textScore = GameObject.Find("Score").GetComponent<Text>();
        textLife = GameObject.Find("BallLife").GetComponent<Text>();
        SetScoreText(score);
        SetLifeText(life);
        inGame = true;
    }

    private void SetScoreText(int score)
    {
        textScore.text = "Score : " + score.ToString();
    }

    private void SetLifeText(int life)
    {
        textLife.text = "Ball : " + life.ToString();
    }

    public void AddScore(int point)
    {
        if (inGame)
        {
            score += point;
            SetScoreText(score);
        }
    }

    void Update()
    {
        if (inGame)
        {
            GameObject ballObj = GameObject.Find("Ball");
            if (ballObj == null)
            {
                life--;
                SetLifeText(life);
                if (life > 0)
                {
                    GameObject newBall = Instantiate(ballPrefab);
                    newBall.name = ballPrefab.name;
                }
                else
                {
                    life = 0;
                    textGameOver.enabled = true;
                    inGame = false;
                }
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Kill : MonoBehaviour
{

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            Destroy(other.gameObject, 0.1f);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{

    public float speed = 15f;

    void FixedUpdate()
    {
        var velox = speed * Input.GetAxisRaw("Horizontal");
        GetComponent<Rigidbody>().velocity = new Vector3(velox, 0f, 0f);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotation : MonoBehaviour
{

    public float rotAngle = 4.0f;

    void FixedUpdate()
    {
        transform.Rotate(0f, -rotAngle, 0f);
    }
}