ysh

2020-07-28  本文已影响0人  我是快乐星猫

Game development -三个游戏

1.roll-a-ball

2.space shooter

3.hover

roll-a-ball

Introduction to my game

A simple ball rolling game, players control the ball, eat all the floating square is victory. The game scene is limited to a flat surface and is bounded by walls. Each square represents a certain score, which is displayed in the upper left corner in real time.

Game design

Source code

CameraController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

    public GameObject player;
    private Vector3 offset;

    // Use this for initialization
    void Start () {
        offset = transform.position;
    }
    
    // Update is called once per frame
    void LateUpdate () {
        transform.position = player.transform.position + offset;
    }
}

PlayerController.cs

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

public class PlayerController : MonoBehaviour {

    public float speed;
    private int score;
    public Text scoretext;

    // Use this for initialization
    void Start () {
        score = 0;
        SetCountText();
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    void FixedUpdate(){
        float moveH = Input.GetAxis("Horizontal");
        float moveV = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveH,0.0f,moveV);

        Rigidbody body = GetComponent<Rigidbody>();
        body.AddForce(movement * speed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider other){
        if(other.gameObject.tag == "pickup"){
        other.gameObject.SetActive(false);
        score++;
        SetCountText();
    }
  }

    void SetCountText(){
        scoretext.text = "Score:" + score.ToString();
    }
}

Rotator.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour {

    private Vector3 rotate;

    // Use this for initialization
    void Start () {
        rotate = new Vector3(15,30,45);
    }
    
    // Update is called once per frame
    void Update () {
        transform.Rotate(rotate * Time.deltaTime);
    }
}

Concrete explanation

Screenshots in game

1.png
2.png

space shooter

Introduction to my game

The player controls a spacecraft, fires bullets to shoot down meteorites to score points, and ends the game when the plane is hit by a meteorite. Adjust the effect of light and shadow in the background picture of the game. Detection includes aircraft and meteorite collisions, bullets and meteorite collisions, with different explosion effects and sound effects. Real-time display of the current score, the game can be restarted after the end.

Game design

Source code

BoundaryDestroy.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoundaryDestroy : MonoBehaviour {

    void OnTriggerExit(Collider other) {
        Destroy(other.gameObject);
    }

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

ContactDestroy.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ContactDestroy : MonoBehaviour {

    public GameObject asteroidexplosion;
    public GameObject playerexplosion;
    private GameController controller;

    void OnTriggerEnter(Collider other) {
        if(other.tag == "Boundary"){
            return;
        }

        if (other.tag != "Player")
        {
            controller.AddScore(10);
        }

        Destroy(other.gameObject);
        Destroy(gameObject);

        GameObject tmp = Instantiate(asteroidexplosion, transform.position, transform.rotation) as GameObject;
        Destroy(tmp,1);

        if (other.tag == "Player"){
            tmp = Instantiate(playerexplosion, other.transform.position, other.transform.rotation) as GameObject;
            Destroy(tmp, 1);
            controller.EndGame();
        }
    }

    // Use this for initialization
    void Start () {
        GameObject tmp = GameObject.FindGameObjectWithTag("GameController");
        controller = tmp.GetComponent<GameController>();
        if (controller == null) {
            Debug.LogError("false");
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

GameController.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {

    public GameObject asteroid;
    public Vector3 spawnValues;
    public int asteroidCount;
    public float spawnWait;
    public float startWait;
    public Text scoretext;
    private int score;
    private bool gameEnded;


    // Use this for initialization
    void Start () {
        gameEnded = false;
        scoretext.gameObject.SetActive(false);
        scoretext.text = "Score:0";
        StartCoroutine(SpawnWaves());
    }

    public void AddScore(int points){
        score += points;
        scoretext.text = "Score:" + score;
    }

    IEnumerator SpawnWaves(){
        yield return new WaitForSeconds(startWait);

        while(true){
        for (int i = 0; i < asteroidCount; i++)
        {
            Vector3 spawnAt = new Vector3(
                Random.Range(-spawnValues.x, spawnValues.x),
                spawnValues.y,
                spawnValues.z);

            Instantiate(asteroid, spawnAt, Quaternion.identity);
            yield return new WaitForSeconds(spawnWait);
            }
        }
    }
    // Update is called once per frame
    void Update () {
        if (gameEnded){
            if(Input.GetKeyDown(KeyCode.R)){
                Scene level = SceneManager.GetActiveScene();
                SceneManager.LoadScene(level.name);
            }
        }
    }

    public void EndGame()
    {
        gameEnded = true;
        scoretext.gameObject.SetActive(true);
    }
}

Mover.cs

using System.Collections.Generic;
using UnityEngine;

public class Mover : MonoBehaviour {

    public float speed;

    // Use this for initialization
    void Start () {
        GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

PlayerController.cs

using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Boundary {
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour {

    public float speed;
    public Boundary boundary;
    public float tilt;

    //shots
    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;

    void FixedUpdate() {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        GetComponent<Rigidbody>().velocity = movement * speed;

        GetComponent<Rigidbody>().position = new Vector3(
            Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
            );
         GetComponent<Rigidbody>().rotation = Quaternion.Euler(
             0.0f,0.0f,
             GetComponent<Rigidbody>().velocity.x * -tilt);
             
    }

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKey("space")&&Time.time>nextFire){
            nextFire = Time.time + fireRate;
            Instantiate(shot,shotSpawn.position,shotSpawn.rotation);
        } 
    }
}

RandomRotator.cs

using System.Collections.Generic;
using UnityEngine;

public class RandomRotator : MonoBehaviour {

    public float tumble;

    // Use this for initialization
    void Start () {
        GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

Concrete explanation

Screenshots in game

1.png
2.png
3.png
4.png

hover

Introduction to my game

A 3D game. Players gain points by manipulating a ship to destroy randomly generated turrets based on time. The turrets rotate regularly and fire bullets. When the turrets find the players (within a distance of the turrets), the turrets will track and shoot the players. The game ends when the player's ship is destroyed.

Game design

Source code

BoltController.cs

using System.Collections.Generic;
using UnityEngine;

public class BoltController : MonoBehaviour {
    public int number;
    public GameObject explosion;
    public GameObject explosion2;
    private GameController controller;
    // Use this for initialization
    void Start () {
    }
    
    // Update is called once per frame
    void Update () {
        number = 0;
        GameObject tmp = GameObject.FindGameObjectWithTag("GameController");
        controller = tmp.GetComponent<GameController>();
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Destroy(gameObject, 0);
            number++;
            Instantiate(explosion, transform.position, transform.rotation);

            Destroy(explosion);
            if (number == 8)
            {
                Destroy(other.gameObject, 0);
                Instantiate(explosion2, transform.position, transform.rotation);
                Destroy(explosion2);
                controller.EndGame();
            }

        }
    }
}

BoundaryDestory.cs

using System.Collections.Generic;
using UnityEngine;

public class BoundaryDestory : MonoBehaviour {
    public GameObject player;
    private Vector3 offset2;
    // Use this for initialization
    void Start () {
        offset2 = transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    void LateUpdate()
    {
       transform.position = player.transform.position + offset2;
    }
    void OnTriggerExit(Collider other) {
        if (other.gameObject.tag == "Bolt")
        {
            Destroy(other.gameObject);
        }
    }
}

CameraControlller.cs

using System.Collections.Generic;
using UnityEngine;

public class CameraControlller : MonoBehaviour {
    public GameObject player;
    private Vector3 offset;
    public float rotationDamping;
    public float distance;
    public float height;
    
    // Use this for initialization
    void Start () {
        offset = transform.position;
        

    }
    
    // Update is called once per frame
    void Update () {
        
    }
    void LateUpdate() {
        float wantedRotationAngle = player.transform.eulerAngles.y;
        float currentRotationAngle = transform.eulerAngles.y;
        currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
        Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
        transform.position = player.transform.position;
        transform.position -= currentRotation * Vector3.forward * distance;
        Vector3 newPosition = new Vector3(transform.position.x, height, transform.position.z);
        transform.position = newPosition;

        transform.LookAt(player.transform);
    }   
}

ContactDestory.cs

using System.Collections.Generic;
using UnityEngine;

public class ContactDestory : MonoBehaviour {
    public GameObject explosion;
    public GameObject shot;
    public Transform shotSpawn;
    private float nextFire;
    public float fireRate;
    private GameController controller;
    // Use this for initialization
    void Start () {
        GameObject tmp = GameObject.FindGameObjectWithTag("GameController");
        controller = tmp.GetComponent<GameController>();
        if (controller == null) {
            Debug.LogError("Unable to find the GameController script");
        
        }
    }
    
    // Update is called once per frame
    void Update () {
        if (Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        }
    }
    void OnTriggerEnter(Collider other) {
        if (other.gameObject.tag == "Bolt") {
            Destroy(gameObject, 0);
            Destroy(other.gameObject, 0);
            controller.addScore(10);
            Instantiate(explosion, transform.position, transform.rotation);
            
            Destroy(explosion);
        }
        if (other.gameObject.tag == "Player")
        {
            Destroy(gameObject, 0);
            Destroy(other.gameObject, 0);
            Instantiate(explosion, transform.position, transform.rotation);

            Destroy(explosion);
            controller.EndGame();
        }
        
    }
}

GameBoundaryDestory.cs

using System.Collections.Generic;
using UnityEngine;

public class GameBoundaryDestory : MonoBehaviour {
    public GameObject player;
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Bolt" || other.gameObject.tag == "Turret_Bolt")
        {
            Destroy(other.gameObject);
        }
    }
}

GameController.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {
    public GameObject turret;
    public Vector3 spawnValues;
    public int turretCount;
    public float spawnWait;
    public float startWait;
    public int score = 0;
    public GUIText scoreText;
    public GUIText endText;
    private bool gameEnded;
    // Use this for initialization
    void Start()
    {
        gameEnded = false;
        endText.gameObject.SetActive(false);
        scoreText.text = "Score: 0";
        StartCoroutine(SpawnWaves());
        
    }
    
    // Update is called once per frame
    void Update () {
        if (gameEnded)
        {
            if(Input.GetKeyDown(KeyCode.R)){
                Scene level = SceneManager.GetActiveScene();
                SceneManager.LoadScene(level.name);
            
            }
        }
    }
    public void EndGame() {
        gameEnded = true;
        endText.gameObject.SetActive(true);
    }
    public void addScore(int points)
    {
        score += points;
        scoreText.text = "Score: " + score;
    }
    IEnumerator SpawnWaves()
    {
        yield return new WaitForSeconds(startWait);
        for (int i = 0; i < turretCount; i++)
        {
            Vector3 spawnAt = new Vector3(
            Random.Range(-spawnValues.x, spawnValues.x),
            spawnValues.y,
            Random.Range(-spawnValues.z, spawnValues.z));
            Instantiate(turret, spawnAt, Quaternion.identity);
            yield return new WaitForSeconds(spawnWait);
        }
        
        
    }
}

Mover.cs

using System.Collections.Generic;
using UnityEngine;

public class Mover : MonoBehaviour {
    public float speed;
    // Use this for initialization
    void Start () {
        GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

PlayerController.cs

using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour {
    public GameObject explosion;
    public GameObject explosion2;
    public float turnSpeed;
    public float speed;
    public Boundary boundary;
    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;
    private int number;
    private Vector3 offset;
    private GameController controller;
    // Use this for initialization
    

    void Start () {
        //number = 0;
        offset = transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        GameObject tmp = GameObject.FindGameObjectWithTag("GameController");
        controller = tmp.GetComponent<GameController>();
        if (controller == null)
        {
            Debug.LogError("Unable to find the GameController script");

        }
        if (Input.GetKey("space") && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        }
    }
    void FixedUpdate() {
        float moveHorzontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        if(moveHorzontal !=0)
        {
            transform.Rotate(new Vector3(0.0f,moveHorzontal * turnSpeed,0.0f));
        }
        if(moveVertical !=0)
        {
            Vector3 fwd = transform.forward;
            GetComponent<Rigidbody>().velocity = fwd * speed * moveVertical;

        }
       /* GetComponent<Rigidbody>().position = new Vector3(
            Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0.0f,
            Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax));
        */
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Turret_Bolt")
        {
            Destroy(other.gameObject, 0);
            number++;
            Instantiate(explosion, transform.position, transform.rotation);
            
            Destroy(explosion);
            if (number == 8)
            {
                Destroy(gameObject, 0);
                Instantiate(explosion2, transform.position, transform.rotation);
                Destroy(explosion2);
                controller.EndGame();
            }

        }
    }
}

RandomRotator.cs

using System.Collections.Generic;
using UnityEngine;

public class RandomRotator : MonoBehaviour {
    public float turn;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    void FixedUpdate() {
        transform.Rotate(new Vector3(0.0f, turn, 0.0f));
    }
    
}

Concrete explanation

Screenshots in game

1.png

Forgive Warships for Upgrading to Sky Explorer


2.png
3.png
4.png
5.png

Note: Mr. Yan is the most handsome teacher in Zhejiang University of Technology.

上一篇 下一篇

猜你喜欢

热点阅读