Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class LivingGridPerspective : MonoBehaviour
- {
- // Start
- public Camera TargetCamera;
- public float CubeWidth = 1;
- public float CubeMargin = 1;
- public float zPlane = 0;
- public float movementSpeed = 20;
- private GameObject[] cubes;
- private int currentCubeIndex = 0;
- private List<System.Func<float, Vector3>> offsetMethods;
- void Start()
- {
- InitializeCubes();
- InitializeOffsetMethods();
- }
- // Update is called once per frame
- void Update()
- {
- MoveCubes();
- }
- private void MoveCubes()
- {
- // if(currentCubeIndex >= cubes.Count()){
- // currentCubeIndex = 0;
- // Debug.Log(Time.realtimeSinceStartup);
- // }
- // var currentCube = cubes[currentCubeIndex++];
- //var method = offsetMethods[Random.Range(0, offsetMethods.Count)];
- // var posOffset = Vector3.forward * Mathf.Cos(Time.realtimeSinceStartup) * Time.deltaTime * movementSpeed;
- // currentCube.transform.position += posOffset;
- for (var i = 0; i < cubes.Count(); i++){
- var posOffset = Vector3.forward * Mathf.Cos(Time.realtimeSinceStartup) * Time.deltaTime * movementSpeed;
- var currentCube = cubes[i];
- currentCube.transform.position += posOffset;
- }
- // foreach (var cube in cubes)
- // {
- // // var method = offsetMethods[i++%offsetMethods.Count];
- // var method = offsetMethods[Random.Range(0, offsetMethods.Count)];
- // var posOffset = method(Time.realtimeSinceStartup) * (1 / movementQuotient);
- // cube.transform.position += posOffset;
- // }
- }
- private void InitializeOffsetMethods()
- {
- offsetMethods = new List<System.Func<float, Vector3>>(){
- (float time) => Vector3.forward*Mathf.Cos(time),
- // (float i) => Vector3.forward*Mathf.Sin(i)
- };
- }
- private void InitializeCubes()
- {
- var ppu = TargetCamera.PixelsPerUnit(zPlane);
- var cubeWidthInPixels = (CubeWidth + CubeMargin) * ppu;
- var cubeColCount = Mathf.Ceil((Screen.width) / cubeWidthInPixels);
- var cubeRowCount = Mathf.Ceil((Screen.height) / cubeWidthInPixels);
- cubes = new GameObject[(int)cubeColCount*(int)cubeRowCount];
- for (var col = 0; col < (int)cubeColCount; col++)
- {
- for (var row = 0; row < (int)cubeRowCount; row++)
- {
- var cubeCenter = (new Vector2(col, row) * cubeWidthInPixels);
- var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
- Destroy(cube.GetComponent<BoxCollider>());
- cube.transform.parent = transform;
- cube.transform.localScale = new Vector3(CubeWidth, CubeWidth, CubeWidth);
- cube.transform.position = TargetCamera.ScreenToWorldPoint(new Vector3(cubeCenter.x + cubeWidthInPixels / 2, cubeCenter.y + cubeWidthInPixels / 2, zPlane));
- Debug.Log(col*(int)cubeRowCount + row);
- cubes[col*(int)cubeRowCount + row] = cube;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement