Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Move : MonoBehaviour {
  6.     public KeyCode upKey;
  7.     public KeyCode downKey;
  8.     public KeyCode rightKey;
  9.     public KeyCode leftKey;
  10.  
  11.     public GameObject booster;
  12.  
  13.     public float speed = 16;
  14.  
  15.     public GameObject wallPrefab;
  16.  
  17.     int direction = 0;
  18.     int boostSignal = 0;
  19.  
  20.     Vector2 lastWallEnd;
  21.    
  22.     void Start () {
  23.         GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
  24.         direction = 1;
  25.         spawnWall();
  26.  
  27.         StartCoroutine(Spawn1());
  28.     }
  29.  
  30.     IEnumerator Spawn1()
  31.     {
  32.         yield return new WaitForSeconds(2);
  33.         Instantiate(booster, new Vector2(25, 25), Quaternion.identity);
  34.         StartCoroutine(Spawn2());
  35.     }
  36.  
  37.     IEnumerator Spawn2()
  38.     {
  39.         yield return new WaitForSeconds(2);
  40.         Instantiate(booster, new Vector2(100, 50), Quaternion.identity);
  41.         StartCoroutine(Spawn1());
  42.     }
  43.  
  44.     void React()
  45.     {
  46.         transform.position = new Vector3(65.97f, 0, 0);
  47.     }
  48.     void spawnWall ()
  49.     {
  50.         lastWallEnd = transform.position;
  51.  
  52.         GameObject g = (GameObject)Instantiate(wallPrefab, transform.position, Quaternion.identity);
  53.         wall = g.GetComponent<Collider2D>();
  54.     }
  55.  
  56.     void fitColliderBetween(Collider2D co, Vector2 a, Vector2 b)
  57.     {
  58.         co.transform.position = a + (b - a) * 0.5f;
  59.  
  60.         float dist = Vector2.Distance(a, b);
  61.         if (a.x != b.x)
  62.             co.transform.localScale = new Vector2(dist + 1, 1);
  63.         else
  64.             co.transform.localScale = new Vector2(1, dist + 1);
  65.     }
  66.  
  67.     void OnTriggerEnter2D(Collider2D co)
  68.     {
  69.         var wall = co.GetComponent<Wall>();
  70.         var speedBoost = co.GetComponent<SpeedBoost>();
  71.  
  72.         if (wall != null)
  73.         {
  74.             print("Player lost:" + name);
  75.             Destroy(gameObject);
  76.         }
  77.  
  78.         if (speedBoost != null)
  79.         {
  80.             speed += 10f;
  81.             boostSignal = 1;
  82.             Destroy(booster);
  83.         }
  84.  
  85.         if ((boostSignal == 1) && (direction == 1))
  86.         {
  87.             GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
  88.             boostSignal = 0;
  89.         }
  90.  
  91.         if ((boostSignal == 1) && (direction == 3))
  92.         {
  93.             GetComponent<Rigidbody2D>().velocity = -Vector2.up * speed;
  94.             boostSignal = 0;
  95.         }
  96.  
  97.         if ((boostSignal == 1) && (direction == 2))
  98.         {
  99.             GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
  100.             boostSignal = 0;
  101.         }
  102.  
  103.         if ((boostSignal == 1) && (direction == 4))
  104.         {
  105.             GetComponent<Rigidbody2D>().velocity = -Vector2.right * speed;
  106.             boostSignal = 0;
  107.         }
  108.     }
  109.     void Update () {
  110.         if (Input.GetKeyDown(upKey))
  111.         {
  112.             GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
  113.             direction = 1;
  114.             spawnWall();
  115.         }
  116.         else if (Input.GetKeyDown(downKey))
  117.         {
  118.             GetComponent<Rigidbody2D>().velocity = -Vector2.up * speed;
  119.             direction = 3;
  120.             spawnWall();
  121.         }
  122.         else if (Input.GetKeyDown(rightKey))
  123.         {
  124.             GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
  125.             direction = 2;
  126.             spawnWall();
  127.         }
  128.         else if (Input.GetKeyDown(leftKey))
  129.         {
  130.             GetComponent<Rigidbody2D>().velocity = -Vector2.right * speed;
  131.             direction = 4;
  132.             spawnWall();
  133.         }
  134.  
  135.  
  136.  
  137.         fitColliderBetween(wall, lastWallEnd, transform.position);
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement