====== Roll-a-ball ====== ===== Neues Projekt erstellen ===== ==== Setting up the game ==== * File - New Project * Pfad wählen - 3D * File - Save Scene (../Assets/_Scenes) Titel: Minigame * GameObject - 3D-Object - Plain * Umbenennen in "Ground" * Reset, um die Position auf X=0, Y=0 und Z=0 zu * Gismos - Show Grid weghaken * Scale von Ground ändern auf X=2, Y=1, Z=2 * ein Plain hat keine Höhe, daher wirkt sich Y=1 nicht aus. * 3D-Object - Sphere * umbenennen in "Player" * auf (0,0.5,0) setzen (Reset) * Neuen Folder "Materials" erstellen * Create Material * Umbenennen in "Background" * Albedo auf ein Dunkelblau stellen * Background auf "Ground" ziehen -> wird blau * Directional Light * Position: (0,3,0) * Rotation: (50,60,0) ==== Moving the player ==== * Kugel "Player" anklicken * Component - Physics - Rigidbody * Add Component - New Script - C# * Name: PlayerController using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { //Public - Variable speed wird im Object-Editor sichtbar und kann auf 10 gesetzt werden. public float speed; //Private Variable rb vom Type Rigidbody private Rigidbody rb; void Start() { //Referenz zum Rigidbody rb = GetComponent(); } void FixedUpdate() //für physikalische Änderungen { // Input vom Keyboard in zwei Variablen float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); //Neue Variable movement vom Typ Vector3 //besteht aus x,y und z-Koordinate, //x-Koord ist moveHorizontal und z ist moveVertical) Vector3 movement = new Vector3(moveHorizontal, 0.0f , moveVertical); //fügt eine Kraft zu Rigidbody hinzu rb.AddForce(movement*speed); } } ==== Moving the camera ==== * Kamera positionieren: * Position (0,10,-10) * Rotation (45,0,0) * Projection "Perspective" * Main Camera anklicken - New Script (Name: CameraController): using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset; void Start () { //Differenz zwischen Kamera und Player offset = transform.position - player.transform.position; } //LateUpdate - nach der Bewegung des Frames void LateUpdate () { transform.position = player.transform.position + offset; } } * Player Game Object in Camera Controller ziehen, um eine Beziehung zwischen den Objekten herzustellen. ==== Setting up the Play Area ==== * GameObject - Empty * Name "Walls" * GameObject - 3DObject - Cube * West Wall (als Child von Walls) * Scale (0.5, 2, 20.5) * Position (-10,0,0) * Edit - Duplicate (East-Wall) * North-Wall: Scale (20.5,2,0.5) * South-Wall: Position (0,0,-10) ==== Creating Collectable Objects ==== * GameObject - Cube * Name: Pick Up * Position: (0,0.5,0) * Scale (0.5,0.5,0.5) * Rotation (45,45,45) * Add Component - New Script - "Rotator" * In Folder _Scripts ziehen * Code eingeben using UnityEngine; using System.Collections; public class Rotator : MonoBehaviour { void Update () { transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime); } } === "Fertigteile" === Die sich drehenden Würfel sollen Instanzen eines prefabs werden. Damit genügt es, die Eigenschaft des prefabs zu ändern, und sämtliche Würfel werden geändert. * Create - New Folder - Prefab * Den Würfel "Pick Up" in den neuen Ordner ziehen * GameObject - Create Empty - "Pick Ups" * Dieses leere Objekt soll im Ursprung (0,0,0) positioniert sein. * "Pick Up"-Object in leeres "Pick Ups" ziehen * Duplizieren (Strg+d) * Material erstellen * Material auf den Würfel "Pick Up" im Ordner Prefab ziehen - alle sollten die Farbe kriegen. ==== Collecting the Pick Ups ==== * PlayerController ändern auf: using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public float speed; private Rigidbody rb; void Start() { rb = GetComponent(); } void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); rb.AddForce(movement*speed); } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Pick Up")) { other.gameObject.SetActive(false); } } } * Objekt "Pick up" im Reiter Project anklicken * Neuen Tag hinzufügen mit dem Namen "Pick Up" -> muss genauso heißen, wie im Script! * Tag den Blöcken zuweisen (sollte funktionieren, wenn man Tag dem Prefab zuweist) * In Pick Up - Prefab: Box Collider "Is Trigger" anhaken. Es werden viele Ressourcen gebraucht beim Berechnen jedes Frames. Die Würfel sind nämlich noch "static colliders", sie sollten jedoch "dynamic colliders" werden. * Static Colliders sollen sich nicht bewegen (Wände, Boden) * Dynamic Colliders müssen Rigidbody haben und können sich bewegen (rotieren). Daher müssen folgende Punkte noch durchgeführt werden: * Component "Rigidbody" zu Prefab hinzufügen. * Use Gravity und Is Kinematic anhaken. ==== Displaying Score an Text ==== === Counter === * Im Script PlayerController folgende Variable hinzufügen: * private int count; * In void Start: * count = 0; * In OnTriggerEnter: * count=count+1; * Create * UI - Text * erzeugt ein Canvas "Text" und EventSystem * Name von "Text ändern": Count Text. * Weiß machen * Text Script: Text: "Count Text" * UI-Elemente müssen Kinder eines Canvas sein. * Anchors Presets * Shift und ALT drücken und linken oben anklicken. * Pos X auf 10, Pos Y auf -10 setzen. * Script PlayerController * folgende Code hinzufügen: * using UnityEngine.UI; * public Text countText; * Erstelle neue Funktion: * void SetCountText() * { countText.text= "Count: " + count.ToString(); } * * in Start: SetCountText(); * in OnTriggerEnter: SetCountText(); * in Unity das "Count Text" - Objekt ziehen in "Count Text" vom Player Controller (Script) === Rückmeldung "Gewonnen" === * Create - UI - Text * Umbenennen in "Win Text" * Weiß machen * Text "Win Text" * Größe 24 * Paragraph Alignment Center - Center * Position auf Posy = 75 z.B seten * in PlayerController * public Text winText; * in Start: winText.text=""; * in SetCountText() * if (count>=12) * { winText.text = "You Win!"; } * GameObject wieder in Script ziehen, um eine Referenz zu erzeugen. Hier der Code vom Player-Controller: using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerController : MonoBehaviour { public float speed; public Text countText; public Text winText; private Rigidbody rb; private int count; void Start () { rb = GetComponent(); count = 0; SetCountText (); winText.text = ""; } void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rb.AddForce (movement * speed); } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ( "Pick Up")) { other.gameObject.SetActive (false); count = count + 1; SetCountText (); } } void SetCountText () { countText.text = "Count: " + count.ToString (); if (count >= 12) { winText.text = "You Win!"; } } }