Guest User

Untitled

a guest
Apr 1st, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Generation : MonoBehaviour {
  5.  
  6. public GameObject GroundObject;
  7. GameObject block;
  8.  
  9. GameObject SelectedObject;
  10. public GameObject Curser;
  11.  
  12. public int width;
  13. public int length;
  14. int i = 0;
  15.  
  16. [Range(0,1)]
  17. public float gridSpacing;
  18.  
  19.  
  20. Vector2[] mappedPositions;
  21. public Vector2 gridPosition;
  22.  
  23.  
  24.  
  25.  
  26. void CreateStage() {
  27.  
  28. mappedPositions = new Vector2[width * length];
  29.  
  30. for(int x = 0; x < length; x++)
  31. {
  32. for(int y = 0; y < width; y++)
  33. {
  34.  
  35. block = (GameObject)Instantiate (GroundObject, new Vector3 (x+(gridSpacing*x), 0, y+(gridSpacing*y)), Quaternion.identity);
  36. block.name = "X:"+ x + " " + "Y:" + y;
  37.  
  38. mappedPositions[i].x = x;
  39. mappedPositions[i].y = y;
  40. i++;
  41.  
  42. }
  43. }
  44. Camera.main.transform.position = new Vector3(length / 2, 20, width / 2);
  45. }
  46.  
  47. void MoveSquare()
  48. {
  49. if(Input.GetKeyDown(KeyCode.UpArrow))
  50. {
  51. if(gridPosition.y != length - 1)
  52. {
  53. gridPosition.y++;
  54. }
  55. }
  56. if(Input.GetKeyDown(KeyCode.DownArrow))
  57. {
  58. if(gridPosition.y != 0)
  59. {
  60. gridPosition.y--;
  61. }
  62. }
  63. if(Input.GetKeyDown(KeyCode.LeftArrow))
  64. {
  65. if(gridPosition.x != 0)
  66. {
  67. gridPosition.x--;
  68. }
  69. }
  70. if(Input.GetKeyDown(KeyCode.RightArrow))
  71. {
  72. if(gridPosition.x != width - 1)
  73. {
  74. gridPosition.x++;
  75. }
  76. }
  77.  
  78. }
  79.  
  80. void Start () {
  81. CreateStage();
  82. gridPosition = new Vector2(0,0);
  83. }
  84.  
  85. void Update () {
  86. MoveSquare();
  87.  
  88. SelectedObject = GameObject.Find("X:"+ gridPosition.x +" Y:" + gridPosition.y);
  89. Curser.transform.position = new Vector3(SelectedObject.transform.position.x, 2, SelectedObject.transform.position.z);
  90.  
  91.  
  92.  
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment