Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyController : MonoBehaviour
- {
- public Sprite[] sprites;
- public int direction = 0; // 0 - вверх, 1 - вниз, 2 - влево, 3 - вправо.
- public float speed = 0.05f;
- private SpriteRenderer render;
- private Vector2 step;
- private Rect bgRect;
- public GameObject bullet;
- public float shotTimer = 3f;
- public float shotSpeed = 3f;
- public float moveTimer = 0;
- public float maxMoveTimer = 1f;
- private System.Random rnd;
- public int rndSeed = 0;
- void Start()
- {
- rnd = new System.Random(rndSeed);
- render = GetComponent<SpriteRenderer>();
- step = new Vector2();
- GameObject bg = GameObject.FindGameObjectWithTag("Background");
- Vector2 bg_pos = bg.transform.position;
- Sprite bg_sprite = bg.GetComponent<SpriteRenderer>().sprite;
- Camera cam = Camera.main;
- Vector2 point1 = cam.ScreenToWorldPoint(bg_pos);
- float w = -point1.x * 2;
- float h = -point1.y * 2;
- //point1.x = -point1.x;
- bgRect = new Rect(point1.x, point1.y, w, h);
- }
- void Update()
- {
- transform.rotation = Quaternion.identity;
- if (direction == 0)
- {
- step = new Vector2(0, speed);
- }
- if (direction == 1)
- {
- step = new Vector2(0, -speed);
- }
- if (direction == 2)
- {
- step = new Vector2(-speed, 0);
- }
- if (direction == 3)
- {
- step = new Vector2(speed, 0);
- }
- render.sprite = sprites[direction];
- Vector2 pos = gameObject.transform.position;
- pos += step;
- if (CheckMove(pos))
- {
- gameObject.transform.position = pos;
- }
- else
- {
- step = new Vector2();
- }
- }
- bool CheckMove(Vector2 newPosition)
- {
- return bgRect.Contains(newPosition);
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- //Debug.Log(collision);
- if (collision.gameObject.tag == "Wall")
- {
- step = new Vector2();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement