Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Move : MonoBehaviour {
- public KeyCode upKey;
- public KeyCode downKey;
- public KeyCode rightKey;
- public KeyCode leftKey;
- public GameObject booster;
- public float speed = 16;
- public GameObject wallPrefab;
- int direction = 0;
- int boostSignal = 0;
- Vector2 lastWallEnd;
- void Start () {
- GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
- direction = 1;
- spawnWall();
- StartCoroutine(Spawn1());
- }
- IEnumerator Spawn1()
- {
- yield return new WaitForSeconds(2);
- Instantiate(booster, new Vector2(25, 25), Quaternion.identity);
- StartCoroutine(Spawn2());
- }
- IEnumerator Spawn2()
- {
- yield return new WaitForSeconds(2);
- Instantiate(booster, new Vector2(100, 50), Quaternion.identity);
- StartCoroutine(Spawn1());
- }
- void React()
- {
- transform.position = new Vector3(65.97f, 0, 0);
- }
- void spawnWall ()
- {
- lastWallEnd = transform.position;
- GameObject g = (GameObject)Instantiate(wallPrefab, transform.position, Quaternion.identity);
- wall = g.GetComponent<Collider2D>();
- }
- void fitColliderBetween(Collider2D co, Vector2 a, Vector2 b)
- {
- co.transform.position = a + (b - a) * 0.5f;
- float dist = Vector2.Distance(a, b);
- if (a.x != b.x)
- co.transform.localScale = new Vector2(dist + 1, 1);
- else
- co.transform.localScale = new Vector2(1, dist + 1);
- }
- void OnTriggerEnter2D(Collider2D co)
- {
- var wall = co.GetComponent<Wall>();
- var speedBoost = co.GetComponent<SpeedBoost>();
- if (wall != null)
- {
- print("Player lost:" + name);
- Destroy(gameObject);
- }
- if (speedBoost != null)
- {
- speed += 10f;
- boostSignal = 1;
- Destroy(booster);
- }
- if ((boostSignal == 1) && (direction == 1))
- {
- GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
- boostSignal = 0;
- }
- if ((boostSignal == 1) && (direction == 3))
- {
- GetComponent<Rigidbody2D>().velocity = -Vector2.up * speed;
- boostSignal = 0;
- }
- if ((boostSignal == 1) && (direction == 2))
- {
- GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
- boostSignal = 0;
- }
- if ((boostSignal == 1) && (direction == 4))
- {
- GetComponent<Rigidbody2D>().velocity = -Vector2.right * speed;
- boostSignal = 0;
- }
- }
- void Update () {
- if (Input.GetKeyDown(upKey))
- {
- GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
- direction = 1;
- spawnWall();
- }
- else if (Input.GetKeyDown(downKey))
- {
- GetComponent<Rigidbody2D>().velocity = -Vector2.up * speed;
- direction = 3;
- spawnWall();
- }
- else if (Input.GetKeyDown(rightKey))
- {
- GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
- direction = 2;
- spawnWall();
- }
- else if (Input.GetKeyDown(leftKey))
- {
- GetComponent<Rigidbody2D>().velocity = -Vector2.right * speed;
- direction = 4;
- spawnWall();
- }
- fitColliderBetween(wall, lastWallEnd, transform.position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement