Unity Community,
I have seen several instances of how to modify the vertices within a mesh but I have been stumped for the better part of the day trying to make a script work properly. For a tower defense game, the laser turrets have a super ability where they all point to a single laser turret and add their energy (think Deathstar). I was originally utilizing a line renderer with no issues but it significantly increased the draw call. Now I am utilizing a dynamically generated plane as the laser which allows me to benefit from batching. Everything worked great up until the activation of the super ability. The two vertices which are supposed to represent the position of the turret in super mode appear to be correct as shown in the graphic. I have debug log information along with a screenshot to demonstrate the problem. What am I missing with the vertices modification which prevents the ‘support turret’ from shooting at the Super Laser on the left?
//LaserTurrets is the reference to the Super Laser
public void LaserSupport(GameObject SuperTurret){
//Muzzle gameobject is the position just at the front of the barrel.
Vector3[] vert = new Vector3[]{
new Vector3(Muzzle.transform.localPosition.x+(LineWidth/2),0,Muzzle.transform.localPosition.z), //0
new Vector3(SuperTurret.transform.position.x+(LineWidth/2),SuperTurret.transform.localPosition.y,SuperTurret.transform.position.z), //1
new Vector3(SuperTurret.transform.position.x-(LineWidth/2),SuperTurret.transform.localPosition.y,SuperTurret.transform.position.z), //2
new Vector3(Muzzle.transform.localPosition.x-(LineWidth/2),0,Muzzle.transform.localPosition.z),//3
};
Debug.Log ("Value of Vert0: " + vert[0]);
Debug.Log ("Value of Vert1: " + vert[1]);
Debug.Log ("Value of Vert2: " + vert[2]);
Debug.Log ("Value of Vert3: " + vert[3]);
int[] tris = new int[]{
0,1,2,
0,2,3
};
Vector3[] normals = new Vector3[]{
new Vector3(-1,0,1), //0
new Vector3(1,0,1), //1
new Vector3(1,0,-1), //2
new Vector3(-1,0,-1),//3
};
Vector2[] uv = new Vector2[]{
new Vector2(0,0),
new Vector2(1,0),
new Vector2(1,1),
new Vector2(0,1)
};
Mesh mesh = new Mesh ();
mesh.vertices = vert;
mesh.normals = normals;
mesh.uv = uv;
mesh.triangles = tris;
mesh.RecalculateNormals ();
if(!GetComponent<MeshFilter>()){
gameObject.AddComponent<MeshFilter>();
}
if(!GetComponent<MeshRenderer>()){
gameObject.AddComponent<MeshRenderer>();
}
gameObject.GetComponent<MeshRenderer> ().material = LineMaterial;
this.GetComponent<MeshFilter> ().renderer.enabled = true;
gameObject.GetComponent<MeshFilter> ().mesh = mesh;
}
The super turret is currently shooting the enemies while the ‘support turret’ is shooting off into nowhere.