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 PlayerController : MonoBehaviour {
- Vector3 pos;
- float speed = 1.0f;
- private Rigidbody2D rb;
- void Start () {
- pos = transform.position; // pozitia initiala
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- if(other.gameObject.CompareTag("Pickup"))
- {
- Destroy (other.gameObject);
- }
- }
- void FixedUpdate () {
- if(Input.GetKey(KeyCode.A) && transform.position == pos) {
- transform.Translate(-1.0f, 0.0f, 0.0f);
- transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * 2*speed);
- }
- if (Input.GetKey (KeyCode.D) && transform.position == pos) {
- transform.Translate (1.0f, 0.0f, 0.0f);
- transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * 2 * speed);
- }
- if (Input.GetKey (KeyCode.W) && transform.position == pos) {
- transform.Translate (0.0f, 1.0f, 0.0f);
- transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
- }
- if (Input.GetKey (KeyCode.S) && transform.position == pos) {
- transform.Translate (0.0f, -1.0f, 0.0f);
- transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
- }
- }
- void Update(){
- pos = transform.position;
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- public class CarrotsGenerator : MonoBehaviour {
- [SerializeField] int requiredCarrots = 20;
- [SerializeField] GameObject carrotPrefab = null;
- [SerializeField] int requiredStones = 5;
- [SerializeField] GameObject stonesPrefab = null;
- [SerializeField] int requiredTrees= 5;
- [SerializeField] GameObject treesPrefab = null;
- [SerializeField] int requiredCaves = 5;
- [SerializeField] GameObject cavesPrefab = null;
- private void Start () {
- if (carrotPrefab == null || requiredCarrots > 10 * 8) { return; } // protect against some errors
- if (stonesPrefab == null || requiredStones > 10 * 8) { return; }
- if (treesPrefab == null || requiredTrees> 10 * 8) { return; }
- if (cavesPrefab == null || requiredCaves > 10 * 8) { return; }
- bool[,] occupiedPositions = new bool[10, 8]; // 2D array of bools to store if a position is occupied
- occupiedPositions[0, 7] = true; // rabbit position (in reality it is -4.5, 3.5)
- GenerateObjects(requiredCarrots,occupiedPositions,carrotPrefab);
- GenerateObjects(requiredStones,occupiedPositions,stonesPrefab);
- GenerateObjects(requiredTrees,occupiedPositions,treesPrefab);
- GenerateObjects(requiredCaves,occupiedPositions,cavesPrefab);
- }
- void GenerateObjects(int numberRequired, bool[,] occupiedPositions, GameObject objectPrefab){
- for (int i = 0; i < numberRequired; ++i) {
- int x, y;
- do {
- x = UnityEngine.Random.Range (0, 10);
- y = UnityEngine.Random.Range (0, 8);
- } while (occupiedPositions [x, y]);
- if (!objectPrefab.Equals (carrotPrefab)) { // avoid cases when the rabbit is stuck at the very beginning
- if (!(((x == 1 && y == 7) && occupiedPositions [0, 6].Equals (true)) ||
- ((x == 0 && y == 6) && occupiedPositions [1, 7].Equals (true))))
- occupiedPositions [x, y] = true;
- Instantiate (objectPrefab, new Vector3 (x - 4.5f, y - 3.5f, 0), UnityEngine.Quaternion.identity);
- } else {
- occupiedPositions [x, y] = true;
- Instantiate (objectPrefab, new Vector3 (x - 4.5f, y - 3.5f, 0), UnityEngine.Quaternion.identity);
- }
- }
- }
- // Update is called once per frame
- void Update () {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement