Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. public class LivingGridPerspective : MonoBehaviour
  7. {
  8. // Start
  9. public Camera TargetCamera;
  10. public float CubeWidth = 1;
  11. public float CubeMargin = 1;
  12. public float zPlane = 0;
  13. public float movementSpeed = 20;
  14. private GameObject[] cubes;
  15. private int currentCubeIndex = 0;
  16. private List<System.Func<float, Vector3>> offsetMethods;
  17. void Start()
  18. {
  19. InitializeCubes();
  20. InitializeOffsetMethods();
  21. }
  22.  
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. MoveCubes();
  27. }
  28.  
  29. private void MoveCubes()
  30. {
  31. // if(currentCubeIndex >= cubes.Count()){
  32. // currentCubeIndex = 0;
  33. // Debug.Log(Time.realtimeSinceStartup);
  34. // }
  35. // var currentCube = cubes[currentCubeIndex++];
  36.  
  37.  
  38. //var method = offsetMethods[Random.Range(0, offsetMethods.Count)];
  39. // var posOffset = Vector3.forward * Mathf.Cos(Time.realtimeSinceStartup) * Time.deltaTime * movementSpeed;
  40. // currentCube.transform.position += posOffset;
  41.  
  42. for (var i = 0; i < cubes.Count(); i++){
  43. var posOffset = Vector3.forward * Mathf.Cos(Time.realtimeSinceStartup) * Time.deltaTime * movementSpeed;
  44. var currentCube = cubes[i];
  45. currentCube.transform.position += posOffset;
  46. }
  47.  
  48. // foreach (var cube in cubes)
  49. // {
  50. // // var method = offsetMethods[i++%offsetMethods.Count];
  51. // var method = offsetMethods[Random.Range(0, offsetMethods.Count)];
  52.  
  53. // var posOffset = method(Time.realtimeSinceStartup) * (1 / movementQuotient);
  54.  
  55. // cube.transform.position += posOffset;
  56. // }
  57. }
  58.  
  59. private void InitializeOffsetMethods()
  60. {
  61. offsetMethods = new List<System.Func<float, Vector3>>(){
  62. (float time) => Vector3.forward*Mathf.Cos(time),
  63. // (float i) => Vector3.forward*Mathf.Sin(i)
  64. };
  65.  
  66. }
  67.  
  68. private void InitializeCubes()
  69. {
  70. var ppu = TargetCamera.PixelsPerUnit(zPlane);
  71. var cubeWidthInPixels = (CubeWidth + CubeMargin) * ppu;
  72. var cubeColCount = Mathf.Ceil((Screen.width) / cubeWidthInPixels);
  73. var cubeRowCount = Mathf.Ceil((Screen.height) / cubeWidthInPixels);
  74.  
  75. cubes = new GameObject[(int)cubeColCount*(int)cubeRowCount];
  76. for (var col = 0; col < (int)cubeColCount; col++)
  77. {
  78. for (var row = 0; row < (int)cubeRowCount; row++)
  79. {
  80.  
  81. var cubeCenter = (new Vector2(col, row) * cubeWidthInPixels);
  82. var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  83. Destroy(cube.GetComponent<BoxCollider>());
  84.  
  85. cube.transform.parent = transform;
  86. cube.transform.localScale = new Vector3(CubeWidth, CubeWidth, CubeWidth);
  87. cube.transform.position = TargetCamera.ScreenToWorldPoint(new Vector3(cubeCenter.x + cubeWidthInPixels / 2, cubeCenter.y + cubeWidthInPixels / 2, zPlane));
  88. Debug.Log(col*(int)cubeRowCount + row);
  89. cubes[col*(int)cubeRowCount + row] = cube;
  90. }
  91. }
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement