Nachdem wir durch die letzten beiden Folgen sowohl eine Plattform gefunden als auch den Unity App Store kennengelernt haben, starten wir nun mit einem neuen Projekt. Wir erstellen uns ein 2D Jump and Run Spiel.
BulletDestruction.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletDestruction : MonoBehaviour {
public float lifeSpan = 2.0f;
// Use this for initialization
void Start () {
Destroy (gameObject, lifeSpan);
}
// Update is called once per frame
void Update () {
}
}
checkpoint.cs
using UnityEngine;
using System.Collections;
public class checkpoint : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Spieler") {
Debug.Log ("Checkpoint erreicht");
// neuen checkpoint abspeichern
levelManager.currentCheckpoint = gameObject;
}
}
}
Enemy.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class enemy : MonoBehaviour {
public LevelManager levelManager;
public Vector3 startPos;
public Vector3 newPos;
public Vector3 tempPos;
public float speed;
public SpriteRenderer sr;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType();
startPos = transform.position;
// Zufällige Geschwindigkeit generieren
speed = Random.Range(5f, 15f);
tempPos = startPos;
sr = gameObject.GetComponent ();
}
// Update is called once per frame
void Update () {
newPos = startPos;
newPos.x = newPos.x + Mathf.PingPong (Time.time * speed, 6) - 3;
transform.position = newPos;
// Bewegung positiv
if (newPos.x > tempPos.x) {
sr.flipX = true;
}
// Bewegung negativ
else
{
sr.flipX = false;
}
// Koordinaten aktueller Frame temporär abspeichern
tempPos = newPos;
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Spieler")
{
//SceneManager.LoadScene (SceneManager.GetActiveScene().name);
levelManager.RespawnPlayer ();
}
if (other.gameObject.tag == "Bullet") {
// Gegner zerstört werden
Destroy (other.gameObject, 0f);
Destroy (gameObject, 0.2f);
}
}
}
LevelManager.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour {
// aktuelle Checkpoint
public GameObject currentCheckpoint;
// spieler gameobjekt
public GameObject spieler;
// lebensanzeige
public int leben;
public Text lebenText;
public Text timerText;
public float roundTimer;
void Start()
{
lebenText.text = leben.ToString ();
}
void Update(){
if (Input.GetKeyDown (KeyCode.Escape)) {
// spiel beendet werden
Debug.Log ("Spiel wird beendet");
Application.Quit ();
}
roundTimer = roundTimer + Time.deltaTime;
//int roundTimerInt = (int)roundTimer;
timerText.text = ((int)roundTimer).ToString ();
//timerText.text = roundTimerInt.ToString();
}
public void RespawnPlayer()
{
// Leben abziehen
leben = leben - 1;
// Lebensanzeige aktualisieren
lebenText.text = leben.ToString ();
// Überprüfen ob Spieler noch Leben hat
if (leben > 0)
{
// falls ja -> Respawn
// spieler an die position des checkpoints bringen
spieler.transform.position = currentCheckpoint.transform.position;
}
else
{
// falls nein -> Spielende!
Time.timeScale = 0.0f;
Debug.Log ("Game over");
}
}
}
LevelSelector.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelSelector : MonoBehaviour {
public string sceneToLoad;
// Use this for initialization
void Start () {
// Level 01 auf aktiv schalten
PlayerPrefs.SetInt("scene00", 1);
if (PlayerPrefs.GetInt (sceneToLoad.ToString ()) == 1)
{
// Level aktiv => Button aktivieren
this.GetComponent()
nextlevel.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class nextlevel : MonoBehaviour {
public string sceneToLoad;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Spieler") {
Debug.Log ("Nächstes Level");
// SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
LoadLevel();
}
}
void LoadLevel()
{
// nächste Szene laden
SceneManager.LoadScene(sceneToLoad);
// fortschritt speichern
if (PlayerPrefs.GetInt (sceneToLoad.ToString ()) == 0) {
// level noch nicht aktiv -> freischalten
PlayerPrefs.SetInt (sceneToLoad.ToString (), 1);
Debug.Log ("Level freigeschaltet? " + PlayerPrefs.GetInt (sceneToLoad.ToString ()));
}
}
}
neustart.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class neustart : MonoBehaviour {
public LevelManager levelManger;
void Start(){
levelManger = FindObjectOfType ();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Spieler") {
Debug.Log ("Spieler tot");
levelManger.RespawnPlayer ();
} else
Debug.Log ("Es ist nicht der Spieler");
}
}
PlatformFalling.cs
using UnityEngine;
using System.Collections;
public class PlatformFalling : MonoBehaviour {
private Rigidbody2D rb2d_Platform;
public float delay;
private GameObject platform;
// Use this for initialization
void Start ()
{
rb2d_Platform = GetComponent ();
platform = this.gameObject;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.collider.tag == "Spieler")
{
// Plattform nach unten fallen
Debug.Log("Spieler erkannt - Plattform fällt");
//StartCoroutine (Fall());
StartCoroutine(Kaputt());
}
}
IEnumerator Fall()
{
yield return new WaitForSeconds (delay);
rb2d_Platform.isKinematic = false;
}
IEnumerator Kaputt()
{
yield return new WaitForSeconds (delay);
Destroy (platform);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Killzone")
{
Destroy (platform);
}
}
}
PlatformMoving.cs
using UnityEngine;
using System.Collections;
public class PlatformMoving : MonoBehaviour {
private Vector3 startPos;
private Vector3 newPos;
public float speed;
// Use this for initialization
void Start ()
{
// Aktuelle Position der Plattform auslesen
startPos = transform.position;
// Zufällige Geschwindigkeit
speed = Random.Range(5f, 10f);
}
// Update is called once per frame
void Update ()
{
newPos = startPos;
newPos.x = newPos.x + Mathf.PingPong (Time.time * speed, 6) - 3;
transform.position = newPos;
//transform.position.x = newPos.x;
}
}
Player.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : MonoBehaviour {
private int coinCounter;
public Text scoreText;
public Text highscoreText;
// Use this for initialization
void Start () {
coinCounter = 0;
//lade den aktuellen highscore
highscoreText.text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
scoreText.text = coinCounter.ToString ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Coin")
{
//Grafik deaktivieren
other.gameObject.GetComponent ().enabled = false;
//Collider deaktivieren
other.gameObject.GetComponent().enabled = false;
//Sound der Münze abspielen
AudioSource audio = other.gameObject.GetComponent();
audio.Play ();
//Münze zerstören
Destroy (other.gameObject, audio.clip.length);
coinCounter++;
scoreText.text = coinCounter.ToString();
//Debug.Log ("Score: " +coinCounter);
if (coinCounter > PlayerPrefs.GetInt ("Highscore"))
{
//überschreibe den highscore
PlayerPrefs.SetInt ("Highscore", coinCounter);
highscoreText.text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
}
}
}
PlayerBullet.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBullet : MonoBehaviour {
public Transform bulletSpawn;
public Transform player;
public Rigidbody2D bulletPrefab;
Rigidbody2D clone;
public float bulletSpeed = 300f;
// Use this for initialization
void Start () {
bulletSpawn = GameObject.Find ("BulletSpawn").transform;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1"))
{
// Feuer frei -> Attack()
Debug.Log("Feuer frei!");
Attack ();
}
}
void Attack()
{
if (bulletSpawn.position.x > player.position.x) {
clone = Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
clone.AddForce (bulletSpawn.transform.right * bulletSpeed);
}
else
{
clone = Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
clone.AddForce (bulletSpawn.transform.right * -bulletSpeed);
}
}
}
QuitOnClick.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuitOnClick : MonoBehaviour {
public void Quit()
{
Debug.Log ("Button geglickt - Spiel beenden");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit ();
#endif
}
}
StartOnClick.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StartOnClick : MonoBehaviour {
public string sceneName;
public void StartLevel()
{
SceneManager.LoadScene (sceneName);
}
}
Video.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Video : MonoBehaviour {
public MovieTexture movie;
// Use this for initialization
void Start () {
// Video abspielen
movie.Play();
}
// Update is called once per frame
void Update ()
{
// Video fertig abgespielt?
if (!movie.isPlaying)
{
// Nächste Szene springen
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
}