Advertisement
andrzejiwaniuk

Space Shooter scripts Unity3d

Jan 18th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.97 KB | None | 0 0
  1. /*
  2.     C:\Users\Andrzej\Documents\Unity projekty\Space Shooter\Assets\Scripts\PlayerController.cs
  3. */
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. [System.Serializable]
  8. public class Boundary{
  9.     public float xMin, xMax, zMin, zMax;
  10.  
  11. }
  12.  
  13.  
  14. public class PlayerController : MonoBehaviour {
  15.  
  16.     public float speed;
  17.     public Boundary boundary;
  18.     public float tilt;
  19.     public GameObject shot;
  20.     public Transform shotSpawn;
  21.     public float fireRate;
  22.    
  23.     private float nextFire;
  24.  
  25.      
  26.  
  27.     void Update(){
  28.         if (Input.GetButton("Fire1") && Time.time > nextFire) {
  29.             nextFire = Time.time + fireRate;
  30.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
  31.             audio.Play ();
  32.         }
  33.     }
  34.  
  35.     void FixedUpdate(){
  36.    
  37.  
  38.  
  39.         //na telefon ruch
  40.  
  41.         float moveHorizontal;// = Input.acceleration.x * 4f;
  42.         float moveVertical; // = Input.acceleration.y * 4f;
  43.  
  44.  
  45.         //na klawiature ruch
  46.  
  47.         //float moveHorizontal = Input.GetAxis("Horizontal");
  48.         //float moveVertical = Input.GetAxis("Vertical");
  49.         moveHorizontal = Input.GetAxis("Horizontal");
  50.         moveVertical = Input.GetAxis("Vertical");
  51.        
  52.         //na telefon
  53. //      moveHorizontal = Input.acceleration.x * 4f;
  54. //      moveVertical = Input.acceleration.y * 4f;
  55.  
  56.    
  57.  
  58.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
  59.         rigidbody.velocity = movement * speed;
  60.  
  61.         rigidbody.position = new Vector3
  62.         (
  63.                 Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
  64.                 0.0f,
  65.                 Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
  66.         );
  67.  
  68.         rigidbody.rotation = Quaternion.Euler(0.0f,0.0f, rigidbody.velocity.x * -tilt);
  69.     }
  70.  
  71. }
  72.  
  73. /*
  74.     GameController.cs
  75. */
  76.  
  77. using UnityEngine;
  78. using System.Collections;
  79.  
  80. public class GameController : MonoBehaviour {
  81.  
  82.     public GameObject hazard;
  83.     public Vector3 spawnValues;
  84.  
  85.     public int hazardCount;
  86.     public float spawnWait;
  87.     public float startWait;
  88.     public float waveWait;
  89.  
  90.     public GUIText scoreText;
  91.     public int score;
  92.  
  93.     public GUIText restartText;
  94.     public GUIText gameOverText;
  95.  
  96.     private bool gameOver;
  97.     private bool restart;
  98.  
  99.  
  100.     void Start ()
  101.     {
  102.         gameOver = false;
  103.         restart = false;
  104.         restartText.text = "";
  105.         gameOverText.text = "";
  106.         score = 0;
  107.         UpdateScore ();
  108.         StartCoroutine (SpawnWaves ());
  109.     }
  110.     void Update ()
  111.     {
  112.         if (restart)
  113.         {
  114.             //nacisniecie klawisz R lub dotkniecie ekranu
  115.             if (Input.GetKeyDown (KeyCode.R) || Input.touchCount > 0)
  116.             {
  117.                 Application.LoadLevel (Application.loadedLevel);
  118.             }
  119.             if(Input.GetKeyDown (KeyCode.Q))
  120.                 Application.Quit();
  121.         }
  122.     }
  123.  
  124.     IEnumerator SpawnWaves ()
  125.     {
  126.         yield return new WaitForSeconds (startWait);
  127.         while (true)
  128.         {
  129.             for (int i = 0; i < hazardCount; i++)
  130.             {
  131.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
  132.                 Quaternion spawnRotation = Quaternion.identity;
  133.                 Instantiate (hazard, spawnPosition, spawnRotation);
  134.                 yield return new WaitForSeconds (spawnWait);
  135.             }
  136.             yield return new WaitForSeconds (waveWait);
  137.             if (gameOver)
  138.             {
  139. //              restartText.text = "Dotknij ekran aby zrestartować grę ";
  140.                 restartText.text = "Naciśnij 'R' aby zrestartować grę"; // + "\n" + "Q - wyjscie";
  141.                 //restartText.text = "'R' - restart" + "\n" + "Q - Quit";
  142.                 restart = true;
  143.                 break;
  144.             }
  145.         }
  146.  
  147.     }
  148.     public void AddScore (int newScoreValue)
  149.     {
  150.         score += newScoreValue;
  151.         UpdateScore ();
  152.     }
  153.    
  154.     void UpdateScore ()
  155.     {
  156.         scoreText.text = "Wynik: " + score;
  157.         //scoreText.text = "Your score: " + score;
  158.     }
  159.  
  160.     public void GameOver ()
  161.     {
  162.         gameOverText.text = "Koniec gry!" + "\n" + "Autor: Andrzej Iwaniuk";
  163.         //gameOverText.text = "Game over!" + "\n" + "Author: Andrzej Iwaniuk";
  164.         gameOver = true;
  165.     }
  166. }
  167.  
  168.  
  169. /*
  170.     DestroyByBoundry.cs
  171.  
  172. */
  173. using UnityEngine;
  174. using System.Collections;
  175.  
  176. public class DestroyByBoundry : MonoBehaviour {
  177.  
  178.     void OnTriggerExit(Collider other) {
  179.         // Destroy everything that leaves the trigger
  180.         Destroy(other.gameObject);
  181.     }
  182. }
  183.  
  184.  
  185. /*
  186.     C:\Users\Andrzej\Documents\Unity projekty\Space Shooter\Assets\Scripts\GoogleMobileAdsDemoScript.cs
  187. */
  188.  
  189. using System;
  190. using UnityEngine;
  191. using GoogleMobileAds;
  192. using GoogleMobileAds.Api;
  193.  
  194. public class GoogleMobileAdsDemoHandler : IInAppPurchaseHandler
  195. {
  196.     private readonly string[] validSkus = { "android.test.purchased" };
  197.  
  198.     //Will only be sent on a success.
  199.     public void OnInAppPurchaseFinished(IInAppPurchaseResult result)
  200.     {
  201.         result.FinishPurchase();
  202.         GoogleMobileAdsDemoScript.OutputMessage = "Purchase Succeeded! Credit user here.";
  203.     }
  204.  
  205.     //Check SKU against valid SKUs.
  206.     public bool IsValidPurchase(string sku)
  207.     {
  208.         foreach(string validSku in validSkus) {
  209.             if (sku == validSku) {
  210.                 return true;
  211.             }
  212.         }
  213.         return false;
  214.     }
  215.  
  216.     //Return the app's public key.
  217.     public string AndroidPublicKey
  218.     {
  219.         //In a real app, return public key instead of null.
  220.         get { return null; }
  221.     }
  222. }
  223.  
  224. // Example script showing how to invoke the Google Mobile Ads Unity plugin.
  225. public class GoogleMobileAdsDemoScript : MonoBehaviour
  226. {
  227.  
  228.     private BannerView bannerView;
  229.     private InterstitialAd interstitial;
  230.     private static string outputMessage = "";
  231.  
  232.     public static string OutputMessage
  233.     {
  234.         set { outputMessage = value; }
  235.     }
  236.     //dodany kod przeze mnie
  237.     void Start () {
  238.         RequestBanner();
  239.         bannerView.Show();
  240.  
  241.    
  242.     }
  243.  
  244.     void OnDestroy() {
  245.         bannerView.Destroy();
  246.     }
  247.     //dodany kod przeze mnie
  248.     /*void OnGUI()
  249.     {
  250.         // Puts some basic buttons onto the screen.
  251.         GUI.skin.button.fontSize = (int) (0.05f * Screen.height);
  252.         GUI.skin.label.fontSize = (int) (0.025f * Screen.height);
  253.  
  254.         Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
  255.                                    0.8f * Screen.width, 0.1f * Screen.height);
  256.         if (GUI.Button(requestBannerRect, "Request Banner"))
  257.         {
  258.             RequestBanner();
  259.         }
  260.  
  261.         Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
  262.                                        0.8f * Screen.width, 0.1f * Screen.height);
  263.         if (GUI.Button(showBannerRect, "Show Banner"))
  264.         {
  265.             bannerView.Show();
  266.         }
  267.  
  268.         Rect hideBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
  269.                                        0.8f * Screen.width, 0.1f * Screen.height);
  270.         if (GUI.Button(hideBannerRect, "Hide Banner"))
  271.         {
  272.             bannerView.Hide();
  273.         }
  274.  
  275.         Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
  276.                                           0.8f * Screen.width, 0.1f * Screen.height);
  277.         if (GUI.Button(destroyBannerRect, "Destroy Banner"))
  278.         {
  279.             bannerView.Destroy();
  280.         }
  281.  
  282.         Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
  283.                                                 0.8f * Screen.width, 0.1f * Screen.height);
  284.         if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
  285.         {
  286.             RequestInterstitial();
  287.         }
  288.  
  289.         Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.675f * Screen.height,
  290.                                              0.8f * Screen.width, 0.1f * Screen.height);
  291.         if (GUI.Button(showInterstitialRect, "Show Interstitial"))
  292.         {
  293.             ShowInterstitial();
  294.         }
  295.  
  296.         Rect destroyInterstitialRect = new Rect(0.1f * Screen.width, 0.8f * Screen.height,
  297.                                                 0.8f * Screen.width, 0.1f * Screen.height);
  298.         if (GUI.Button(destroyInterstitialRect, "Destroy Interstitial"))
  299.         {
  300.             interstitial.Destroy();
  301.         }
  302.  
  303.         Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
  304.                                    0.8f * Screen.width, 0.05f * Screen.height);
  305.         GUI.Label(textOutputRect, outputMessage);
  306.     }*/
  307.  
  308.     private void RequestBanner()
  309.     {
  310.         #if UNITY_EDITOR
  311.             string adUnitId = "unused";
  312.         #elif UNITY_ANDROID
  313.             string adUnitId = "ca-app-pub-xxx";      //moj Google AdMob AdUnit
  314.         #elif UNITY_IPHONE
  315.             string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
  316.         #else
  317.             string adUnitId = "unexpected_platform";
  318.         #endif
  319.  
  320.         // Create a 320x50 banner at the top of the screen.
  321.         bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
  322.         // Register for ad events.
  323.         bannerView.AdLoaded += HandleAdLoaded;
  324.         bannerView.AdFailedToLoad += HandleAdFailedToLoad;
  325.         bannerView.AdOpened += HandleAdOpened;
  326.         bannerView.AdClosing += HandleAdClosing;
  327.         bannerView.AdClosed += HandleAdClosed;
  328.         bannerView.AdLeftApplication += HandleAdLeftApplication;
  329.         // Load a banner ad.
  330.         bannerView.LoadAd(createAdRequest());
  331.     }
  332.  
  333.     private void RequestInterstitial()
  334.     {
  335.         #if UNITY_EDITOR
  336.             string adUnitId = "unused";
  337.         #elif UNITY_ANDROID
  338.             string adUnitId = "ca-app-pub-xxx";     //moj Google AdMob AdUnit
  339.         #elif UNITY_IPHONE
  340.             string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
  341.         #else
  342.             string adUnitId = "unexpected_platform";
  343.         #endif
  344.  
  345.         // Create an interstitial.
  346.         interstitial = new InterstitialAd(adUnitId);
  347.         // Register for ad events.
  348.         interstitial.AdLoaded += HandleInterstitialLoaded;
  349.         interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
  350.         interstitial.AdOpened += HandleInterstitialOpened;
  351.         interstitial.AdClosing += HandleInterstitialClosing;
  352.         interstitial.AdClosed += HandleInterstitialClosed;
  353.         interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
  354.         GoogleMobileAdsDemoHandler handler = new GoogleMobileAdsDemoHandler();
  355.         interstitial.SetInAppPurchaseHandler(handler);
  356.         // Load an interstitial ad.
  357.         interstitial.LoadAd(createAdRequest());
  358.     }
  359.  
  360.     // Returns an ad request with custom ad targeting.
  361.     private AdRequest createAdRequest()
  362.     {
  363.         return new AdRequest.Builder()
  364.                 .AddTestDevice(AdRequest.TestDeviceSimulator)
  365.                 .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
  366.                 .AddKeyword("game")
  367.                 .SetGender(Gender.Male)
  368.                 .SetBirthday(new DateTime(1985, 1, 1))
  369.                 .TagForChildDirectedTreatment(false)
  370.                 .AddExtra("color_bg", "9B30FF")
  371.                 .Build();
  372.  
  373.     }
  374.  
  375.     private void ShowInterstitial()
  376.     {
  377.         if (interstitial.IsLoaded())
  378.         {
  379.             interstitial.Show();
  380.         }
  381.         else
  382.         {
  383.             print("Interstitial is not ready yet.");
  384.         }
  385.     }
  386.  
  387.     #region Banner callback handlers
  388.  
  389.     public void HandleAdLoaded(object sender, EventArgs args)
  390.     {
  391.         print("HandleAdLoaded event received.");
  392.     }
  393.  
  394.     public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
  395.     {
  396.         print("HandleFailedToReceiveAd event received with message: " + args.Message);
  397.     }
  398.  
  399.     public void HandleAdOpened(object sender, EventArgs args)
  400.     {
  401.         print("HandleAdOpened event received");
  402.     }
  403.  
  404.     void HandleAdClosing(object sender, EventArgs args)
  405.     {
  406.         print("HandleAdClosing event received");
  407.     }
  408.  
  409.     public void HandleAdClosed(object sender, EventArgs args)
  410.     {
  411.         print("HandleAdClosed event received");
  412.     }
  413.  
  414.     public void HandleAdLeftApplication(object sender, EventArgs args)
  415.     {
  416.         print("HandleAdLeftApplication event received");
  417.     }
  418.  
  419.     #endregion
  420.  
  421.     #region Interstitial callback handlers
  422.  
  423.     public void HandleInterstitialLoaded(object sender, EventArgs args)
  424.     {
  425.         print("HandleInterstitialLoaded event received.");
  426.     }
  427.  
  428.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
  429.     {
  430.         print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
  431.     }
  432.  
  433.     public void HandleInterstitialOpened(object sender, EventArgs args)
  434.     {
  435.         print("HandleInterstitialOpened event received");
  436.     }
  437.  
  438.     void HandleInterstitialClosing(object sender, EventArgs args)
  439.     {
  440.         print("HandleInterstitialClosing event received");
  441.     }
  442.  
  443.     public void HandleInterstitialClosed(object sender, EventArgs args)
  444.     {
  445.         print("HandleInterstitialClosed event received");
  446.     }
  447.  
  448.     public void HandleInterstitialLeftApplication(object sender, EventArgs args)
  449.     {
  450.         print("HandleInterstitialLeftApplication event received");
  451.     }
  452.  
  453.     #endregion
  454. }
  455.  
  456. /*
  457.     C:\Users\Andrzej\Documents\Unity projekty\Space Shooter\Assets\Scripts\RandomRotator.cs
  458. */
  459.  
  460. using UnityEngine;
  461. using System.Collections;
  462.  
  463. public class RandomRotator : MonoBehaviour {
  464.  
  465.     public float tumble;
  466.    
  467.     void Start ()
  468.     {
  469.         rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
  470.     }
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement