Space Shooter

This examples based on Space Shooter Tutorial.

This example was made by using CSharpParser to parse the following script:

  • Done_BGScroller.cs
  • Done_DestroyByBoundary.cs
  • Done_DestroyByContact.cs
  • Done_DestroyByTime.cs
  • Done_EvasiveManeuver.cs
  • Done_GameController.cs
  • Done_Mover.cs
  • Done_PlayerController.cs
  • Done_RandomRotator.cs
  • Done_WeaponController.cs

Original Script:

using UnityEngine;
using System.Collections;

public class Done_BGScroller : MonoBehaviour
{
  public float scrollSpeed;
  public float tileSizeZ;

  private Vector3 startPosition;

  void Start ()
  {
    startPosition = transform.position;
  }

  void Update ()
  {
    float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ);
    transform.position = startPosition + Vector3.forward * newPosition;
  }
}
using UnityEngine;
using System.Collections;

public class Done_DestroyByBoundary : MonoBehaviour
{
  void OnTriggerExit (Collider other) 
  {
    Destroy(other.gameObject);
  }
}
using UnityEngine;
using System.Collections;

public class Done_DestroyByContact : MonoBehaviour
{
  public GameObject explosion;
  public GameObject playerExplosion;
  public int scoreValue;
  private Done_GameController gameController;

  void Start ()
  {
    GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
    if (gameControllerObject != null)
    {
      gameController = gameControllerObject.GetComponent <Done_GameController>();
    }
    if (gameController == null)
    {
      Debug.Log ("Cannot find 'GameController' script");
    }
  }

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

    if (explosion != null)
    {
      Instantiate(explosion, transform.position, transform.rotation);
    }

    if (other.tag == "Player")
    {
      Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
      gameController.GameOver();
    }
    
    gameController.AddScore(scoreValue);
    Destroy (other.gameObject);
    Destroy (gameObject);
  }
}
using UnityEngine;
using System.Collections;

public class Done_DestroyByTime : MonoBehaviour
{
  public float lifetime;

  void Start ()
  {
    Destroy (gameObject, lifetime);
  }
}
using UnityEngine;
using System.Collections;

public class Done_EvasiveManeuver : MonoBehaviour
{
  public Done_Boundary boundary;
  public float tilt;
  public float dodge;
  public float smoothing;
  public Vector2 startWait;
  public Vector2 maneuverTime;
  public Vector2 maneuverWait;

  private float currentSpeed;
  private float targetManeuver;

  void Start ()
  {
    currentSpeed = GetComponent<Rigidbody>().velocity.z;
    StartCoroutine(Evade());
  }
  
  IEnumerator Evade ()
  {
    yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y));
    while (true)
    {
      targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x);
      yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
      targetManeuver = 0;
      yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y));
    }
  }
  
  void FixedUpdate ()
  {
    float newManeuver = Mathf.MoveTowards (GetComponent<Rigidbody>().velocity.x, targetManeuver, smoothing * Time.deltaTime);
    GetComponent<Rigidbody>().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
    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, 0, GetComponent<Rigidbody>().velocity.x * -tilt);
  }
}
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;

public class Done_GameController : MonoBehaviour
{
    public GameObject[] hazards;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    public Text scoreText;
    public Text restartText;
    public Text gameOverText;

    private bool gameOver;
    private bool restart;
    private int score;

    void Start()
    {
        gameOver = false;
        restart = false;
        restartText.text = "";
        gameOverText.text = "";
        score = 0;
        UpdateScore();
        StartCoroutine(SpawnWaves());
    }

    void Update()
    {
        if (restart)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            }
        }
    }

    IEnumerator SpawnWaves()
    {
        yield return new WaitForSeconds(startWait);
        while (true)
        {
            for (int i = 0; i < hazardCount; i++)
            {
                GameObject hazard = hazards[Random.Range(0, hazards.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(hazard, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(spawnWait);
            }
            yield return new WaitForSeconds(waveWait);

            if (gameOver)
            {
                restartText.text = "Press 'R' for Restart";
                restart = true;
                break;
            }
        }
    }

    public void AddScore(int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }

    public void GameOver()
    {
        gameOverText.text = "Game Over!";
        gameOver = true;
    }
}
using UnityEngine;
using System.Collections;

public class Done_Mover : MonoBehaviour
{
  public float speed;

  void Start ()
  {
    GetComponent<Rigidbody>().velocity = transform.forward * speed;
  }
}
using UnityEngine;
using System.Collections;

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

public class Done_PlayerController : MonoBehaviour
{
  public float speed;
  public float tilt;
  public Done_Boundary boundary;

  public GameObject shot;
  public Transform shotSpawn;
  public float fireRate;
   
  private float nextFire;
  
  void Update ()
  {
    if (Input.GetButton("Fire1") && Time.time > nextFire) 
    {
      nextFire = Time.time + fireRate;
      Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
      GetComponent<AudioSource>().Play ();
    }
  }

  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);
  }
}
using UnityEngine;
using System.Collections;

public class Done_RandomRotator : MonoBehaviour 
{
  public float tumble;
  
  void Start ()
  {
    GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
  }
}
using UnityEngine;
using System.Collections;

public class Done_WeaponController : MonoBehaviour
{
  public GameObject shot;
  public Transform shotSpawn;
  public float fireRate;
  public float delay;

  void Start ()
  {
    InvokeRepeating ("Fire", delay, fireRate);
  }

  void Fire ()
  {
    Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    GetComponent<AudioSource>().Play();
  }
}

Generated Script:

using UnityEngine;
using System.Collections;

public class Done_BGScroller : MonoBehaviour {
  public float scrollSpeed = 0F;
  public float tileSizeZ = 0F;
  private Vector3 startPosition = Vector3.zero;

  void Start() {
    startPosition = base.transform.position;
  }

  void Update() {
    float newPosition;
    newPosition = Mathf.Repeat((Time.time * scrollSpeed), tileSizeZ);
    base.transform.position = (startPosition + (Vector3.forward * newPosition));
  }
}
using UnityEngine;
using System.Collections;

public class Done_DestroyByBoundary : MonoBehaviour {

  void OnTriggerExit(Collider other) {
    Object.Destroy(other.gameObject);
  }
}
using UnityEngine;
using System.Collections;

public class Done_DestroyByContact : MonoBehaviour {
  public GameObject explosion;
  public GameObject playerExplosion;
  public int scoreValue = 0;
  private Done_GameController gameController;

  void Start() {
    GameObject gameControllerObject = null;
    gameControllerObject = GameObject.FindGameObjectWithTag("GameController");
    if((gameControllerObject != null)) {
      gameController = gameControllerObject.GetComponent<Done_GameController>();
    }
    if((gameController == null)) {
      Debug.Log("Cannot find 'GameController' script");
    }
  }

  void OnTriggerEnter(Collider other) {
    if(((other.tag == "Boundary") || (other.tag == "Enemy"))) {
      return;
    }
    if((explosion != null)) {
      Object.Instantiate<GameObject>(explosion, base.transform.position, base.transform.rotation);
    }
    if((other.tag == "Player")) {
      Object.Instantiate<GameObject>(playerExplosion, other.transform.position, other.transform.rotation);
      gameController.GameOver();
    }
    gameController.AddScore(scoreValue);
    Object.Destroy(other.gameObject);
    Object.Destroy(base.gameObject);
  }
}
using UnityEngine;
using System.Collections;

public class Done_DestroyByTime : MonoBehaviour {
  public float lifetime = 0F;

  void Start() {
    Object.Destroy(base.gameObject, lifetime);
  }
}
using UnityEngine;
using System.Collections;

public class Done_EvasiveManeuver : MonoBehaviour {
  public Done_Boundary boundary = new Done_Boundary();
  public float tilt = 0F;
  public float dodge = 0F;
  public float smoothing = 0F;
  public Vector2 startWait = Vector2.zero;
  public Vector2 maneuverTime = Vector2.zero;
  public Vector2 maneuverWait = Vector2.zero;
  private float currentSpeed = 0F;
  private float targetManeuver = 0F;

  void Start() {
    currentSpeed = base.GetComponent<Rigidbody>().velocity.z;
    base.StartCoroutine(Evade());
  }

  IEnumerator Evade() {
    yield return new WaitForSeconds(Random.Range(startWait.x, startWait.y));
    while(true) {
      targetManeuver = (Random.Range(1, dodge) * -(Mathf.Sign(base.transform.position.x)));
      yield return new WaitForSeconds(Random.Range(maneuverTime.x, maneuverTime.y));
      targetManeuver = 0;
      yield return new WaitForSeconds(Random.Range(maneuverWait.x, maneuverWait.y));
    }
  }

  void FixedUpdate() {
    float newManeuver;
    newManeuver = Mathf.MoveTowards(base.GetComponent<Rigidbody>().velocity.x, targetManeuver, (smoothing * Time.deltaTime));
    base.GetComponent<Rigidbody>().velocity = new Vector3(newManeuver, 0F, currentSpeed);
    base.GetComponent<Rigidbody>().position = new Vector3(Mathf.Clamp(base.GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0F, Mathf.Clamp(base.GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax));
    base.GetComponent<Rigidbody>().rotation = Quaternion.Euler(0, 0, (base.GetComponent<Rigidbody>().velocity.x * -(tilt)));
  }
}
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;

public class Done_GameController : MonoBehaviour {
  public GameObject[] hazards = new GameObject[0];
  public Vector3 spawnValues = Vector3.zero;
  public int hazardCount = 0;
  public float spawnWait = 0F;
  public float startWait = 0F;
  public float waveWait = 0F;
  public Text scoreText;
  public Text restartText;
  public Text gameOverText;
  private bool gameOver = false;
  private bool restart = false;
  private int score = 0;

  void Start() {
    gameOver = false;
    restart = false;
    restartText.text = "";
    gameOverText.text = "";
    score = 0;
    UpdateScore();
    base.StartCoroutine(SpawnWaves());
  }

  void Update() {
    if(restart) {
      if(Input.GetKeyDown(KeyCode.R)) {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
      }
    }
  }

  IEnumerator SpawnWaves() {
    GameObject hazard = null;
    Vector3 spawnPosition;
    Quaternion spawnRotation;
    yield return new WaitForSeconds(startWait);
    while(true) {
      for(int index = 0; index < hazardCount; index += 1) {
        hazard = hazards[Random.Range(0, hazards.Length)];
        spawnPosition = new Vector3(Random.Range(-(spawnValues.x), spawnValues.x), spawnValues.y, spawnValues.z);
        spawnRotation = Quaternion.identity;
        Object.Instantiate<GameObject>(hazard, spawnPosition, spawnRotation);
        yield return new WaitForSeconds(spawnWait);
      }
      yield return new WaitForSeconds(waveWait);
      if(gameOver) {
        restartText.text = "Press 'R' for Restart";
        restart = true;
        break;
      }
    }
  }

  public void AddScore(int newScoreValue) {
    score += newScoreValue;
    UpdateScore();
  }

  void UpdateScore() {
    scoreText.text = ("Score: " + score);
  }

  public void GameOver() {
    gameOverText.text = "Game Over!";
    gameOver = true;
  }
}
using UnityEngine;
using System.Collections;

public class Done_Mover : MonoBehaviour {
  public float speed = 0F;

  void Start() {
    base.GetComponent<Rigidbody>().velocity = (base.transform.forward * speed);
  }
}
using UnityEngine;
using System.Collections;

[System.SerializableAttribute]
public class Done_Boundary : object {
  public float xMin = 0F;
  public float xMax = 0F;
  public float zMin = 0F;
  public float zMax = 0F;
}

public class Done_PlayerController : MonoBehaviour {
  public float speed = 0F;
  public float tilt = 0F;
  public Done_Boundary boundary = new Done_Boundary();
  public GameObject shot;
  public Transform shotSpawn;
  public float fireRate = 0F;
  private float nextFire = 0F;

  void Update() {
    if((Input.GetButton("Fire1") && (Time.time > nextFire))) {
      nextFire = (Time.time + fireRate);
      Object.Instantiate<GameObject>(shot, shotSpawn.position, shotSpawn.rotation);
      base.GetComponent<AudioSource>().Play();
    }
  }

  void FixedUpdate() {
    float moveHorizontal;
    float moveVertical;
    Vector3 movement;
    moveHorizontal = Input.GetAxis("Horizontal");
    moveVertical = Input.GetAxis("Vertical");
    movement = new Vector3(moveHorizontal, 0F, moveVertical);
    base.GetComponent<Rigidbody>().velocity = (movement * speed);
    base.GetComponent<Rigidbody>().position = new Vector3(Mathf.Clamp(base.GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0F, Mathf.Clamp(base.GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax));
    base.GetComponent<Rigidbody>().rotation = Quaternion.Euler(0F, 0F, (base.GetComponent<Rigidbody>().velocity.x * -(tilt)));
  }
}
using UnityEngine;
using System.Collections;

public class Done_RandomRotator : MonoBehaviour {
  public float tumble = 0F;

  void Start() {
    base.GetComponent<Rigidbody>().angularVelocity = (Random.insideUnitSphere * tumble);
  }
}
using UnityEngine;
using System.Collections;

public class Done_WeaponController : MonoBehaviour {
  public GameObject shot;
  public Transform shotSpawn;
  public float fireRate = 0F;
  public float delay = 0F;

  void Start() {
    base.InvokeRepeating("Fire", delay, fireRate);
  }

  void Fire() {
    Object.Instantiate<GameObject>(shot, shotSpawn.position, shotSpawn.rotation);
    base.GetComponent<AudioSource>().Play();
  }
}

 

Graphs preview:

 

Requirement

Unity 3D Version 2018.1 or higher.
uNode – Visual Scripting Version 1.6.6 or higher, this requirement is optional.

 

Download

Was this helpful to you?