Is worldspace/localspace coords screwing me over here?

Hey all.

I have a setup where I want little lightning arcs to appear along the surface of a hand mesh in my scene. So ive done the following:

Get the array of vertices in the hand mesh->pick 3 random points from that array-> use those points as start, middle and end positions of my linerenderer, that does the lightning effect.

This procedure is flawed, in that it could give me lightning inside the handmesh, im aware of that, but thats a different issue. The issue I THINK im having is that, the positions I feed to the linerenderer are interpreted as worldspace coordinates, and I want it to think of them as objectspace (Along the hand mesh).

Could this be what is going on? I post my code, and a screenshot of the scene +inspector where the position vectors are shown(they dont deviate all the much from the values in the screenshot).

Code first:

var lightningWidth = 0.05;				// line renderer line width
var numVertices = 10;					// number of line segments per bolt
function FixedUpdate() {


	 var mesh : Mesh = GetComponent(MeshFilter).mesh;
	 
	 var vertices : Vector3[] = mesh.vertices;
	
	var lr = GetComponent(LineRenderer);
	var temp_start_point:Vector3;
	var temp_middlepoint1:Vector3;
	var temp_end_point:Vector3;
	var start_point:Vector3;
	var middlepoint1:Vector3;
	var end_point: Vector3;
	
	temp_start_point = vertices[Random.Range(0,vertices.Length)];
	temp_middlepoint1 = vertices[Random.Range(0,vertices.Length)];
	temp_end_point = vertices[Random.Range(0,vertices.Length)];
	
	
	//Debug.DrawLine(Vector3.zero,start_point,Color.red);
	
	lr.SetWidth (lightningWidth, lightningWidth);
	lr.SetVertexCount (numVertices);
	start_point = temp_start_point+transform.position;
	middlepoint1 =temp_middlepoint1+transform.position;
	end_point = temp_end_point+transform.position;

		lr.SetPosition(0, start_point);
		lr.SetPosition(1, middlepoint1);
		lr.SetPosition(2, end_point);
	}

Yes the vertices for a line renderer will certainly be in world space. While you could manage to grab the hand’s vertex positions in world space, you already state that you aren’t satisfied with how it looks.

http://forum.unity3d.com/threads/60844-Lightning

Perhaps you should look into a shader based approach? Not only would it look better, it would be far more performant.

The Vector3’s you pull from the mesh will be in local-space.
try this instead:

start_point = transform.TransformPoint(temp_start_point);
middlepoint1 =transform.TransformPoint(temp_middlepoint1);
end_point = transform.TransformPoint(temp_end_point);

Well I tried out the shaderbased solution, which is alright as a backup solution, however it “follows” the mesh a bit too much (Which is what that shader is meant to do) making it appear as a shield type thing more than random little arcs of lightning. I could use that shader for something else in my project though, so much obliged =)

Antitheory, I tried out the TransformPoint function, but it didnt change anything unfortunately, which I dont quite understand if im honest. Any ideas?

Well it wouldn’t have changed anything if your object were not rotated or scaled, because in your other code you already had accounted for the position discrepancy (by adding transform.position).

It could have something to do with the import settings of the model by looking at the imported model in your assets… (usually the model is scaled down .01 or something when it’s first imported… but I think the vertex values should take this into account anyways.

This is most likely not your problem, but just as a note, the line renderer also tends to act weard sometines when you have a small amount of verticies. Maybe also try to and more points.