HomebrewGr

Φτιάξτε ένα Breakout Game (Beginners - Unity)

Ο παρακάτω οδηγός είναι για την δημιουργία ενός απλού παιχνιδιού, για την γρήγορη εκμάθηση της Unity.

Παρακάτω είναι ο κώδικας των στοιχείων του παιχνιδιού σε C#.

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();
}

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();
}
}

TimedDestroy

using UnityEngine;
using System.Collections;
public class TimedDestroy : MonoBehaviour {

public float destroyTime = 1f;

// Use this for initialization
void Start () {

Destroy (gameObject, destroyTime);

}

}

Ο οδηγός δεν είναι δικός μου αλλά είναι από την σειρά μαθημάτων της Unity.

Full Post

Related topics:

Πως να αλλάξετε το title tag για καλύτερο SEO σε Blogger [BlogSpot] blogs

Πως να ξεκλειδώσεις τα αρχεία pdf online

Πως να παίξετε το Unreal P.T. στο PC σας

Πως κάνουμε softmod στο Wii

Firmware USB Hack για τις TV LG σειρές LH/LF