tag:blogger.com,1999:blog-5616321214723803595.post7018301671535434315..comments2024-03-13T09:57:10.000-07:00Comments on Student Game Dev has moved!: Unity Voxel Tutorial Part 4: Destroying and placing blocksAnonymoushttp://www.blogger.com/profile/11218854695223703316noreply@blogger.comBlogger32125tag:blogger.com,1999:blog-5616321214723803595.post-70116558820971451912018-12-18T07:44:57.059-08:002018-12-18T07:44:57.059-08:00I know this is an old post, and you probably gave ...I know this is an old post, and you probably gave up on it or figured it out yourself, but I had this problem and found the solution. So if anyone is stuck with this problem, this is what caused it for me.<br /><br />In the PolygonGenerator the mesh.Optimize() was used, but as it has become obsolete I searched for what I could replace it with. I found MeshUtility.Optimize(mesh) and used it. However, this seems to be the cause of the slow-down. So I just removed it.Anonymoushttps://www.blogger.com/profile/09568404048853404288noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-68564679056589686152017-04-01T00:34:07.157-07:002017-04-01T00:34:07.157-07:00Arno. Do you mind sharing your code?
Arno. Do you mind sharing your code?<br />Anonymoushttps://www.blogger.com/profile/14263339781158457530noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-1510434327908838902015-08-25T08:59:47.845-07:002015-08-25T08:59:47.845-07:00The collision mesh is 3d thus its handled as a 3d ...The collision mesh is 3d thus its handled as a 3d object.Anonymoushttps://www.blogger.com/profile/09315161966388937262noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-80717916835634280012015-02-13T13:38:42.730-08:002015-02-13T13:38:42.730-08:00Mathf.floor is well instead of strange rounding.Mathf.floor is well instead of strange rounding.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-74570265297759206642015-02-02T15:27:10.662-08:002015-02-02T15:27:10.662-08:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/00016097175480685254noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-58006075109681570982015-01-15T02:18:19.885-08:002015-01-15T02:18:19.885-08:00Still messing around your tutorial ..
if you wan...Still messing around your tutorial .. <br /><br />if you want to draw the image2 you can add that :<br />// drawing the position targeted <br />Vector3 drawpoint = new Vector3(point.x,point.y,hit.point.z); // point is vector2 , we need to add the "Z" dimension<br />Debug.DrawLine(hit.point,drawpoint,Color.green);<br /><br /><br /><br />Plus i got an issue , since i used another vector3 to place my GameObject , or the terrain . <br />To rectify it, I added those vars under tScript declaration : <br /><br />private PolygonGenerator tScript;<br />private float tScriptX; // X coordinate of the terrain <br />private float tScriptY; // Y coordinate of the terrain <br /><br />And added that when we change the block into air on hit :<br />tScriptX=tScript.transform.position.x;<br />tScriptY=tScript.transform.position.y;<br /><br />tScript.Blocks[Mathf.RoundToInt(point.x-.5f-tScriptX),Mathf.RoundToInt(point.y+.5f-tScriptY)]=0;<br /><br /><br />this work since we have only one "Z" coordinate for all our objects , and since we work on a 2D example .Seydoux Nicolashttps://www.blogger.com/profile/06442971238532761841noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-19859641409446777202015-01-12T04:40:41.828-08:002015-01-12T04:40:41.828-08:00This comment has been removed by the author.Evangederhttps://www.blogger.com/profile/17505031630829222438noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-78977097369283981102014-11-25T16:11:07.632-08:002014-11-25T16:11:07.632-08:00I answer to my own question.
I found way to do th...I answer to my own question.<br /><br />I found way to do this by saving "public byte[,] blocks;" data just end of GenTerrain() function.<br /><br />Then in start() I don't use GenTerrain(), insted I load blocks[,] from memory.<br /><br />Thats all. Simple as that.Arnohttps://www.blogger.com/profile/10134216253668386220noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-31096816330857935882014-11-25T11:10:04.008-08:002014-11-25T11:10:04.008-08:00Hey, I have one question. If I undestood right thi...Hey, I have one question. If I undestood right this polygonGenerator script generate numbers 0, 1, 2 (air, sand, rock) and then turn it into blocks (terrain).<br /><br />So how can I save that list of numbers. Then maybe change some of those and put it back to the script which will turn it into blocks (terrain). So It wouldn't be "random" generator anymore and I could make different kind of maps with it.<br /><br />I hope you understood what I mean. Thank you!Arnohttps://www.blogger.com/profile/10134216253668386220noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-85707617655741739022014-09-15T15:35:50.096-07:002014-09-15T15:35:50.096-07:00Great tutorial, thanks alot. How would you handle ...Great tutorial, thanks alot. How would you handle player movement with this generated terrain mesh collider? <br />2D rigidbody? I can't get collisions to work with 2d box collider. Thanks.Anonymoushttps://www.blogger.com/profile/07817612752227831199noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-4585978526077488002014-07-26T23:00:19.522-07:002014-07-26T23:00:19.522-07:00Vector2 BlockCoordFromHitTriangle(RaycastHit hitIn...Vector2 BlockCoordFromHitTriangle(RaycastHit hitInfo, Vector2 rayVector)<br /> {<br /> // Get the collider and its mesh.<br /> MeshCollider meshCollider = hitInfo.collider as MeshCollider;<br /> Mesh mesh = meshCollider.sharedMesh;<br /><br /> // Extract local space coordinates of the triangle that the ray hit<br /> Vector3 p0 = mesh.vertices [mesh.triangles [hitInfo.triangleIndex * 3 + 0]];<br /> Vector3 p1 = mesh.vertices [mesh.triangles [hitInfo.triangleIndex * 3 + 1]];<br /> Vector3 p2 = mesh.vertices [mesh.triangles [hitInfo.triangleIndex * 3 + 2]];<br /><br /> // Transform local coords to world coords.<br /> p0 = hitInfo.collider.transform.TransformPoint(p0);<br /> p1 = hitInfo.collider.transform.TransformPoint(p1);<br /> p2 = hitInfo.collider.transform.TransformPoint(p2);<br /><br /> // Print debug output showing the 3 points of the triangle our ray hit.<br /> print("HIT TRIANGLE");<br /> print("P0: (" + p0.x.ToString() + ", " + p0.y.ToString() + ")");<br /> print("P1: (" + p1.x.ToString() + ", " + p1.y.ToString() + ")");<br /> print("P2: (" + p2.x.ToString() + ", " + p2.y.ToString() + ")");<br /><br /> // This will hold the vertices of the side of the triangle that the ray hit.<br /> Vector2[] sidePoints = new Vector2[2];<br /><br /> // Detect which two vertices of the triangle belong to the side the ray collided with.<br /> // We do this by finding the two points with the same Z coordinate.<br /> int index = 0;<br /> if ((int)p0.z == (int)hitInfo.point.z)<br /> {<br /> sidePoints [index] = p0;<br /> index++;<br /> }<br /> if ((int)p1.z == (int)hitInfo.point.z)<br /> {<br /> sidePoints [index] = p1;<br /> index++;<br /> }<br /> if ((int)p2.z == (int)hitInfo.point.z)<br /> {<br /> sidePoints [index] = p2;<br /> index++;<br /> }<br /><br /> // If only one point on the triangle is on the same Z coord as the hit point, then simply copy that point into<br /> // the second slot in the sidePoints array. This way the midpoint will be this point itself so the code<br /> // below will still get the correct block coordinates.<br /> if (index < 2)<br /> sidePoints [1] = sidePoints [0];<br /><br /><br /> // Print debug output showing the coordinates of our two points.<br /> print("SideP0: (" + sidePoints [0].x.ToString() + ", " + sidePoints [0].y.ToString() + ")");<br /> print("SideP1: (" + sidePoints [1].x.ToString() + ", " + sidePoints [1].y.ToString() + ")");<br /><br /> // Find the center point of the side the ray collided with.<br /> Vector2 center = new Vector2((sidePoints [0].x + sidePoints [1].x) / 2, <br /> (sidePoints [0].y + sidePoints [1].y) / 2);<br /><br /> // Print debug output showing the center point's coordinates.<br /> print("Center Point: (" + center.x.ToString() + ", " + center.y.ToString() + ")");<br /><br /> // Draw the normal for this RaycastHit.<br /> Debug.DrawLine(hitInfo.point, hitInfo.point + hitInfo.normal, Color.cyan);<br /><br /> // Halve and invert the normal.<br /> Vector2 normalInvertedHalved2D = new Vector2(hitInfo.normal.x, hitInfo.normal.y) * -0.5f;<br /><br /> // Add the halved and inverted normal to our center point to get a point inside the block we need to destroy.<br /> return center + normalInvertedHalved2D;<br /> }<br />}Anonymoushttps://www.blogger.com/profile/04834458025319904713noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-47234800769542341822014-07-26T23:00:10.186-07:002014-07-26T23:00:10.186-07:00Hey guys, I have managed to finally solve this tri...Hey guys, I have managed to finally solve this tricky and aggravating problem after hours of experimentation. The result is that I created a function that gets us block coordinates from the triangle that our ray collided with. Basically, it gets the vertices of the triangle the ray collided with. It then tests to see which 2 points on the triangle both reside on the same Z-coordinate as the collision point. If only one point on the triangle has the same Z-coordinate, it duplicates that point to serve as the 2nd point. Using the 2 points we can now find the mid point of the side of the triangle we hit. Then add half of the inverted normal and we get the center point of the block to destroy basically. If there was only one point that got duplicated, this still works because the normal will be at an angle which when inverted will still put the resulting point inside of the block we need to destroy. <br /><br />This code should work no matter what direction the Raycast comes from as well. Anyway, here is the code of this function for you guys:Anonymoushttps://www.blogger.com/profile/04834458025319904713noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-51266393112415869502014-07-26T19:48:53.080-07:002014-07-26T19:48:53.080-07:00As far as I can tell the third screenshot (the las...As far as I can tell the third screenshot (the laser part) is complete nonsense. The ray never passes thru all of the terrain. It always gets stuck on the corner of a block somewhere. It seems that the problem is the fact that the code does not take into account the orientation of the face it hit. For example if it hits the top-left corner of a block and considers it the top face, it gets the inverse of the normal of that face which moves it down half a block but its still on the left edge of the block at this point. As a result the adjustment moves it one block left, which is not the correct block. So it tries to remove the incorrect block and gets stuck repeatedly colliding with a block it cannot destroy because of this issue with the math.<br /><br />I cannot seem to get this to work no matter what I do and I'm not sure how the author could miss a problem this serious. I mean, the code simply does not work. I've checked and rechecked it. The suggestion in the above post does not solve it either.Anonymoushttps://www.blogger.com/profile/04834458025319904713noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-70121012321279878562014-04-22T13:48:44.392-07:002014-04-22T13:48:44.392-07:00I don't think this was a mistake on my part ca...I don't think this was a mistake on my part cause I tried copied and pasted some of the code. Well here <br /><br />tScript.blocks[Mathf.RoundToInt(point.x - 0.5f),Mathf.RoundToInt(point.y +0.5f)] = 0;<br /><br />When I got to this part I got stuck cause nothing would happen when I tried colliding my target with the terrain. I set the byte to = 1 and it would spawn a block on top. Which I found weird because it was supposed to turn the block at the bottom into stone. So after analyzing what was actually happening I tried changing + to - in the y and bam it worked! I don't know if I did something else wrong prior or it was a mistake :\<br /><br />tScript.blocks[Mathf.RoundToInt(point.x - 0.5f),Mathf.RoundToInt(point.y - 0.5f)] = 0;Anonymoushttps://www.blogger.com/profile/11256539263242230266noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-3398296403389052712014-04-17T16:02:32.306-07:002014-04-17T16:02:32.306-07:00After doing some digging it looks like the 3d rayc...After doing some digging it looks like the 3d raycasts, as we are using here, don't detect collisions with 2d objects, which is what we have generated.<br /><br />The best fixes I have found to make this function work is to use<br />http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.OverlapPoint.html<br />http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit2D.html<br /><br />However I will say that getting the line to draw appropriately after detecting from a 3d ray is a pain in the behind. I'd suggest doing a full on conversion to 2dRays and follow the rest of the tutorial normally.Mission0https://www.blogger.com/profile/03762785708736061310noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-13269591251346922502014-01-28T14:42:44.069-08:002014-01-28T14:42:44.069-08:00This comment has been removed by the author.vetoPWRhttps://www.blogger.com/profile/12920192842166230479noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-83980863263245712792014-01-28T14:22:34.986-08:002014-01-28T14:22:34.986-08:00This comment has been removed by the author.vetoPWRhttps://www.blogger.com/profile/12920192842166230479noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-7937009079257222842014-01-28T12:35:53.514-08:002014-01-28T12:35:53.514-08:00I cannot get the raycast hit to register (Unity 4....I cannot get the raycast hit to register (Unity 4.2.2). The distance displays correctly between the cast and receive object (shows blue) and follows correctly, but I cannot get the ray to intersect the generated poly faces. The object appears to be detected, and it's generator control is being found.<br /><br />Any ideas? I have been through the code line by line, and will be looking again.<br /><br />r<br /><br />vetoPWRhttps://www.blogger.com/profile/12920192842166230479noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-68827402225492255482013-12-31T04:26:19.114-08:002013-12-31T04:26:19.114-08:00You should create a new game object (Ctrl + Shift ...You should create a new game object (Ctrl + Shift + N) in the scene to put the script on, this will be the raycast's source. Then also create a gameobject to act as its target and drag the target from the Hierarchy tab to the raycaster's target variable in the Inspector tab. Make sure the z position of both the new gameobjects is 10 so that it's in line with the terrain.Anonymoushttps://www.blogger.com/profile/11218854695223703316noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-50624939963933911272013-12-30T09:54:41.710-08:002013-12-30T09:54:41.710-08:00Hi,
I don't get how to set up my scene in Uni...Hi, <br />I don't get how to set up my scene in Unity.<br />In the beginning with the first picture when we have "The raycast hitting the terrain".<br />I don't know to what object I need to apply the new script to.Anonymoushttps://www.blogger.com/profile/16054128863737365888noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-4604098503828999852013-12-21T10:58:26.670-08:002013-12-21T10:58:26.670-08:00Added here: http://studentgamedev.blogspot.no/p/pa...Added here: http://studentgamedev.blogspot.no/p/part-4-complete-code.htmlAnonymoushttps://www.blogger.com/profile/11218854695223703316noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-12260180243104191632013-12-20T11:54:27.854-08:002013-12-20T11:54:27.854-08:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/09726891578892294231noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-19155874430356675292013-12-10T02:41:29.660-08:002013-12-10T02:41:29.660-08:00Can you please upload the source for the scripts u...Can you please upload the source for the scripts up to the end of this part like you did in the second tutorial?Anonymoushttps://www.blogger.com/profile/00985426699144140323noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-68727404156327041182013-12-04T07:40:03.529-08:002013-12-04T07:40:03.529-08:00Alexandros, nice tutorials. Can't wait to sta...Alexandros, nice tutorials. Can't wait to start the 3D ones.<br /><br />@JohnnySabotage - I'm getting a steady 75fps regardless of using any of block builders, destroyers, or sitting idle.Anonymoushttps://www.blogger.com/profile/07405014906045666291noreply@blogger.comtag:blogger.com,1999:blog-5616321214723803595.post-81955416108905874062013-11-26T15:37:21.601-08:002013-11-26T15:37:21.601-08:00I get a pretty terrible performance hit with Rayca...I get a pretty terrible performance hit with RaycastExample and BlockPlaceExample. I normally get around 400+FPS but get dropped down to ~35 FPS. ColliderExample seems to do much better.Donovan Muellerhttps://www.blogger.com/profile/05970431201545049203noreply@blogger.com