Search Unity

Help with a falling sand like game!!!

Discussion in 'Scripting' started by Rhyusaky, Mar 6, 2016.

  1. Rhyusaky

    Rhyusaky

    Joined:
    Jan 22, 2016
    Posts:
    25
    Hello, I'm working on a game style the powder toy, however I can not do the physical, can someone help me?
    The code's there:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public static var Map: Texture2D;
    4. //var Map_Pixels: Pixel[];
    5. //Init Test Array
    6. public var Map_Pixels = new Array(Pixel);
    7. //Finish Test Array
    8. var Pixels: Class_Pixel;
    9. var MapScale: Vector2;
    10. var InsertPosition: Vector2;
    11. var InsertPixel: int;
    12. var Mat: Material;
    13. private var iCount: int = 0;
    14. private var XY: Vector2;
    15. private var UseIndex: int;
    16. var pos: Vector3;
    17. private var fwd = transform.TransformDirection(Vector3.forward);
    18. private var hit: RaycastHit;
    19. var Mouse: Vector2;
    20. private var ShowInventory: boolean;
    21. var PhysicsDelay: float;
    22.  
    23. public var Pixel: Pixel;
    24.  
    25. //Inventory Variables In;
    26. var ItensInInventory: int[];
    27. private var SelectedItemInInventory: int;
    28. //Inventory Variables Out;
    29.  
    30. function Start() {
    31. var Pixel: Pixel;
    32. var Position: Vector2;
    33. Map = new Texture2D(MapScale.x,MapScale.y);
    34. for(var i:int=0;i<Map.height;i++)
    35.    for(var j:int=0;j<Map.width;j++)
    36.       {
    37.       //Pixel.Name = Pixels.ReturnPixelName(0);
    38.       //Pixel.Pixel = Pixels.ReturnPixelColor(0);
    39.       //Pixel.Reagents = Pixels.ReturnPixelReagents(0);
    40.       //Pixel.Action = Pixels.ReturnPixelAction(0);
    41.       //Pixel.Index = Pixels.ReturnPixelIndex(0);
    42.       //Pixel.Position = InsertPosition;
    43.       //Map_Pixels.Add(Pixel);
    44.       Map.SetPixel(i,j,Color.white);
    45.       }
    46. }
    47. function Update() {
    48. var Camera: Camera;
    49. Camera = Camera.main;
    50. var Ray = Camera.ScreenPointToRay(Input.mousePosition);
    51.     var pixelUV : Vector2 = hit.textureCoord;
    52.     pixelUV.x *= Map.width;
    53.     pixelUV.y *= Map.height;
    54.     InsertPosition = pixelUV;
    55. Mouse = Input.mousePosition;
    56.  
    57. Physics.Raycast(Ray,hit,60);
    58. if(hit.transform.gameObject != null) {
    59. //hit.textureCoord;
    60. //pos = hit.point;
    61. //InsertPosition = pos;
    62. }
    63. if(Input.GetKeyDown(KeyCode.Tab)) {
    64. ShowInventory = !ShowInventory;
    65. }
    66. //Mat = transform.GetComponent(Material);
    67. Mat.mainTexture = Map;
    68. if(Input.GetMouseButtonDown(1)) {
    69. DrawnPixel(InsertPosition,Map,Pixel);
    70. }
    71. TestGravity();
    72. }
    73. public function DrawnPixel(Position:Vector2,Texture:Texture2D,Pixel:Pixel) {
    74. iCount += 1;
    75. Pixel.Name = Pixels.ReturnPixelName(InsertPixel);
    76. Pixel.Pixel = Pixels.ReturnPixelColor(InsertPixel);
    77. Pixel.Reagents = Pixels.ReturnPixelReagents(InsertPixel);
    78. Pixel.Action = Pixels.ReturnPixelAction(InsertPixel);
    79. Pixel.Index = Pixels.ReturnPixelIndex(InsertPixel);
    80. Pixel.Position = InsertPosition;
    81.   //Init New Array
    82. Map_Pixels.Add(Pixel);
    83.  
    84. //print("The Array Is"+Map_Pixels);
    85.   //Finish New Array
    86.  
    87.    //Init Old Array
    88. //Map_Pixels = new Pixel[iCount];
    89. //Map_Pixels.SetValue(Pixel.Name,iCount);
    90. //Map_Pixels.SetValue(Pixel.Index,iCount);
    91. //Map_Pixels.SetValue(Pixel.Reagents,iCount);
    92. //Map_Pixels.SetValue(Pixel.Action,iCount);
    93. //Map_Pixels.SetValue(Pixel.Position,iCount);
    94.    //Finish Old Array
    95. Map.SetPixel(InsertPosition.x,InsertPosition.y,Pixel.Pixel);
    96. Map.Apply();
    97. TestGravity();
    98. }
    99. public function GetPixel(Index:int) {
    100. Pixel.Name = Pixels.ReturnPixelName(InsertPixel);
    101. Pixel.Pixel = Pixels.ReturnPixelColor(InsertPixel);
    102. Pixel.Reagents = Pixels.ReturnPixelReagents(InsertPixel);
    103. Pixel.Action = Pixels.ReturnPixelAction(InsertPixel);
    104. Pixel.Index = Pixels.ReturnPixelIndex(InsertPixel);
    105. }
    106. function TestGravity() {
    107. yield WaitForSeconds(PhysicsDelay);
    108. var LPixel: Color;
    109. var MPixel: Pixel;
    110. var number: int;
    111. for(XY.x=0;XY.x<Map.height;XY.x++)
    112.    for(XY.y=0;XY.y<Map.width;XY.y++)
    113.       {
    114.       number = XY.x+XY.y/Map.height/Map.width;
    115.      if(number > Map_Pixels.length) {
    116.      number = Map_Pixels.length;
    117.      }
    118.      
    119.       LPixel = Map.GetPixel(XY.x,XY.y-1);
    120.       MPixel = Map_Pixels[number];
    121.       if(LPixel == Color.white) {
    122.       MPixel.Position = MPixel.Position-Vector2(MPixel.Position.x,MPixel.Position.y-1);
    123.       Map.SetPixel(XY.x,XY.y-1,Map_Pixels[InsertPixel]);
    124.       Map.SetPixel(XY.x,XY.y,Color.white);
    125.       //FallPixel(XY,MPixel,Map);
    126.       print("The Pixel Position is:"+MPixel.Position);
    127.       }
    128.       }
    129.  
    130. }
    131. function FallPixel(XY:Vector2,Pixel:Pixel,Texture:Texture2D) {
    132. Map.SetPixel(XY.x,XY.y-1,Map_Pixels[UseIndex]);
    133. Map.SetPixel(XY.x,XY.y,Pixels.Pixels.GetValue(0));
    134. Map_Pixels.RemoveAt(iCount);
    135. Pixel.Position = XY;
    136. Map_Pixels.Add(Pixel);
    137. }
    138.  
    139. //Inventory Function In;
    140. function OnGUI() {
    141. var localnumber: int;
    142. var PixelName: String;
    143. if(ShowInventory == true) {
    144. for(var i:int=0;i<Pixels.Pixels.Length;i++)
    145. for(var j:int=0;j<Pixels.Pixels.Length;j++)
    146. {
    147. localnumber = i+j/9;
    148. if(j == 1) {
    149. localnumber += 3;
    150. }
    151.   if(j == 2) {
    152. localnumber += 6;
    153. }
    154. PixelName = Pixels.ReturnPixelName(localnumber);
    155. ItensInInventory = new int[i];
    156.   if(GUI.Button(Rect(100*i,50*j,100,50),PixelName)) {
    157.   print("The Button is: "+localnumber);
    158.   SelectedItemInInventory = localnumber;
    159.   InsertPixel = SelectedItemInInventory;
    160.   }
    161. }
    162. }
    163. }
    164. //Inventory Function Out;
    And Pixel class here:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. class Pixel {
    4. var Name: String;
    5. var Index: int;
    6. var Pixel: Color;
    7. var Position: Vector2;
    8. var Reagents: Color[];
    9. var Action: String;
    10. }
    11. var Pixels: Pixel[];
    12.  
    13. public function ReturnPixelColor(index:int) : Color
    14. {
    15.  
    16.   for (var i: int =0;i<Pixels.Length;i++)
    17.   {
    18.     if (Pixels[i].Index == index )
    19.     return Pixels[i].Pixel;
    20.   }
    21. }
    22. public function ReturnPixelName(index:int) : String
    23. {
    24.  
    25.   for (var i: int =0;i<Pixels.Length;i++)
    26.   {
    27.     if (Pixels[i].Index == index )
    28.     return Pixels[i].Name;
    29.   }
    30. }
    31. public function ReturnPixelPosition(index:int) : Vector2
    32. {
    33.  
    34.   for (var i: int =0;i<Pixels.Length;i++)
    35.   {
    36.     if (Pixels[i].Index == index )
    37.     return Pixels[i].Position;
    38.   }
    39. }
    40. public function ReturnPixelAction(index:int) : String
    41. {
    42.  
    43.   for (var i: int =0;i<Pixels.Length;i++)
    44.   {
    45.     if (Pixels[i].Index == index )
    46.     return Pixels[i].Action;
    47.   }
    48. }
    49. public function ReturnPixelSpecificReagent(index:int,Reactor:int) : Color
    50. {
    51.  
    52.   for (var i: int =0;i<Pixels.Length;i++)
    53.   {
    54.     if (Pixels[i].Index == index )
    55.     return Pixels[i].Reagents.GetValue(Reactor);
    56.   }
    57. }
    58. public function ReturnPixelReagents(index:int) : Color[]
    59. {
    60.  
    61.   for (var i: int =0;i<Pixels.Length;i++)
    62.   {
    63.     if (Pixels[i].Index == index )
    64.     return Pixels[i].Reagents;
    65.   }
    66. }
    67. public function ReturnPixelIndex(index:int) : int
    68. {
    69.  
    70.   for (var i: int =0;i<Pixels.Length;i++)
    71.   {
    72.     if (Pixels[i].Index == index )
    73.     return Pixels[i].Index;
    74.   }
    75. }
    Please, I need much of response.