Roll-a-ball

Neues Projekt erstellen

Setting up the game

Moving the player

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

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

Setting up the Play Area

Creating Collectable Objects

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.

Collecting the Pick Ups

using UnityEngine;
using System.Collections;
 
public class PlayerController : MonoBehaviour
{
    public float speed;
    private Rigidbody rb;
 
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    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);
        }
    }
}

Es werden viele Ressourcen gebraucht beim Berechnen jedes Frames. Die Würfel sind nämlich noch „static colliders“, sie sollten jedoch „dynamic colliders“ werden.

Daher müssen folgende Punkte noch durchgeführt werden:

Displaying Score an Text

Counter

Rückmeldung "Gewonnen"

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