Unity Official Tutorial --- CREATING A BREAKOUT GAME FOR BEGINNERS

ATTENTION PLEASE:
If you wanna learn official tutorials, please single click the hyperlink below. This blog is just used to MAKE PDF for learning.

CREATING A BREAKOUT GAME FOR BEGINNERS

In this live training session we look at creating a Breakout style game including basic game mechanics, user interface and a game manager script.

Paddle

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {
    public float paddleSpeed = 1f;
    private Vector3 playerPos = new Vector3 (0, -9.5f, 0);

    void Update () 
    {
        float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
        playerPos = new Vector3 (Mathf.Clamp (xPos, -8f, 8f), -9.5f, 0f);
        transform.position = playerPos;
    }
}

Ball

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    public float ballInitialVelocity = 600f;
    private Rigidbody rb;
    private bool ballInPlay;

    void Awake () {
        rb = GetComponent<Rigidbody>();   
    }

    void Update () {
        if (Input.GetButtonDown("Fire1") && ballInPlay == false)
        {
            transform.parent = null;
            ballInPlay = true;
            rb.isKinematic = false;
            rb.AddForce(new Vector3(ballInitialVelocity, ballInitialVelocity, 0));
        }
    }
}   

GM

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GM : MonoBehaviour {

    public int lives = 3;
    public int bricks = 20;
    public float resetDelay = 1f;
    public Text livesText;
    public GameObject gameOver;
    public GameObject youWon;
    public GameObject bricksPrefab;
    public GameObject paddle;
    public GameObject deathParticles;
    public static GM instance = null;
    private GameObject clonePaddle;

    // Use this for initialization
    void Awake ()  {
        if (instance == null)
            instance = this;
        else if (instance != this)
            Destroy (gameObject);
        Setup();
    }

    public void Setup(){
        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
        Instantiate(bricksPrefab, transform.position, Quaternion.identity);
    }

    void CheckGameOver(){
        if (bricks < 1){
            youWon.SetActive(true);
            Time.timeScale = .25f;
            Invoke ("Reset", resetDelay);
        }

        if (lives < 1){
            gameOver.SetActive(true);
            Time.timeScale = .25f;
            Invoke ("Reset", resetDelay);
        }
    }

    void Reset(){
        Time.timeScale = 1f;
        Application.LoadLevel(Application.loadedLevel);
    }

    public void LoseLife(){
        lives--;
        livesText.text = "Lives: " + lives;
        Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity);
        Destroy(clonePaddle);
        Invoke ("SetupPaddle", resetDelay);
        CheckGameOver();
    }

    void SetupPaddle(){
        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    }

    public void DestroyBrick(){
        bricks--;
        CheckGameOver();
    }
}

Bricks

using UnityEngine;
using System.Collections;

public class Bricks : MonoBehaviour {

    public GameObject brickParticle;

    void OnCollisionEnter (Collision other) {
        Instantiate(brickParticle, transform.position, Quaternion.identity);
        GM.instance.DestroyBrick();
        Destroy(gameObject);
    }
}

DeadZone

using UnityEngine;
using System.Collections;

public class DeadZone : MonoBehaviour {

    void OnTriggerEnter (Collider col){
        GM.instance.LoseLife();
    }
}

TimedDestory

using UnityEngine;
using System.Collections;

public class TimedDestroy : MonoBehaviour {

    public float destroyTime = 1f;

    // Use this for initialization
    void Start () {
        Destroy (gameObject, destroyTime);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值