# Convert a modified Transform.forward to world space

I am trying to make an electricity system, where it is given the start and end points, and it works out a random sequence of points in the rough direction of the end point. These points are set as vertices on a line renderer, and it should end up joining up as a bolt of electricity. My problem is that it needs to move 'forward' from start to end. The easy solution is that the electricity comes from a game object, which is set to LookAt the end point. Then, every time it sets a 'nextPoint', it adds to the transform.forward.

nextPoint = new Vector3 ((startingPoint.forward.x + divergence.x),
(startingPoint.forward.y + divergence.y), distance);


divergence is a Vector3 containing a random increase on the x and y axis, and 1 on the z axis.

This works, in a way. The problem is that it then assumes these are world-space coordinates. So the line will go from starting point, to (eg. 0.3, 0.75, 1), and then back to the end point. I can't find a localToWorld conversion for a vector though, so I am not sure how to get the transform.forward + whatever, converted from local to world space.

• In the past when I've done electricity effects, I've used midpoint displacement to get the desired fractal look. Apr 29 '16 at 5:39
• That's cool, I hadn't seen that page. But how would I render it in a 3d game? I know he mentions it, but I'd prefer it to be genuinely 3d, and so I can't use quads or planes, and cylinders would look dodgy, I think. Apr 29 '16 at 7:17
• @DMGregory Could you provide this is an answer but give a real code example rather than his pseudocode? I have tried for days to get this to work but the line looks ridiculous. May 5 '16 at 5:00

What is happening is that your nextPoint vector is not actually describing a point in world space; it is describing a direction and distance away from your starting point. So in order to get the world space position of where this vector is pointing to, take the world space position of your starting point, then add your offset vector to it:

nextPoint = startingPoint.position + new Vector3((startingPoint.forward.x +
divergence.x), (startingPoint.forward.y + divergence.y), distance);


Side note: Instead of making a game object and setting its rotation with LookAt, then using the forward vector, you can also very easily calculate the direction vector yourself. Your underlying question is "Starting from startingPoint, in which direction do I have to go to reach endPoint?" Given two Vector3s, startingPoint and endPoint, the direction from start to end is simply:

Vector3 direction = (endPoint - startPoint).normalized;


This will give you a vector that is pointing in the correct direction, normalized to have a length of 1; exactly how Transform.forward works.

If you would like to know how far the two points are apart (for example so you can evenly space a certain number of points between then), you can get the length of the direction vector like so:

float distance = (endPoint - startPoint).magnitude;