Hello all.
First off, let me say I am new to unity, have some scripting experience, and battling UnityScript. Been pulling out what little hair I have left on this. I am working on a 2d game and the script I’m having trouble with is this pseudo destructible ground thing.
I Have a plane with a background image that has a scorched earth look to it, and a mesh that has a grass texture on it in front of it.
All this script is supposed to do is wind the grass mesh triangle clockwise based off of the index fed from a raycast so that it looks like the ground is dissapearing.
At tops this can only happen once every 4-5 seconds, so it shouldn’t eat up too much of the processor hopefully (mobile game). Eventually I want to have it wind the 2 triangles to the left and right (+ the 2 on the Z that I use for collision) of the raycasted index to make a little “crater”, throw up a few pieces of ground, but for the moment this is giving me enough grief.
So what’s happening is when I click on the mesh it will apparently wind whichever triangles it feels like. I am getting no errors (my syntax is improving I guess) but I have hit a wall…made of broken triangles. Here’s the code:
var triangleIDX : int = 0;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var hit : RaycastHit;
if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit)) {
triangleIDX = hit.triangleIndex*3;
var meshCollider = hit.collider as MeshCollider;
var mesh : Mesh = meshCollider.GetComponent(MeshFilter).mesh;
var vertices = mesh.vertices;
var triangles = mesh.triangles;
var uvs : Vector2[] = new Vector2[vertices.Length];
mesh.Clear();
mesh.vertices = vertices;
for (var i = 0; i < triangleIDX; i=i+3)
{
var tmp = triangles*;*
_ triangles = triangles[i+2];_
* triangles[i+2] = tmp;*
* }*
* for (i = 0 ; i < uvs.Length; i++)*
uvs = Vector2 (vertices_.x, vertices*.z);
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
//mesh.Optimize();
}
}
}
Does anyone know what is happening here? I’m guessing its something like the triangle strips are throwing off the raycast, no idea really, but after reading what I could find in the reference about these strips…I am still pretty lost. Any help with this would be greatly appreciated. Thanks.
-Ads*
*update: Clarified question and updated code.
*Another code update_