- Home /
How to draw beams?
Such as homeworld, etc. The "Beam" is likely a simple quad stretched between two points, where the Z axis is always rotated to face the camera. (fyi I'm not certain the particle system would work in this case, as it's not as straightforward to generate an exact beam between two points, and I haven't had much luck generating a solid beam of particles. I might look into this a little more, though)
The challenges as I seem them are:
The texture will be stretched the longer the beam. I would likely want to tile the texture instead. A solid beam wouldn't matter, but a beam with energy waves would need to be tiled.
It seems like the beam's Z Axis would be rotated to face the camera, so you always see the full flat beam, not a flat poly if you are a low angle.
The way I think I would go about this is:
- Find the distance between the beam emitter and the target.
- Create a quad of that length, and the width of the beam.Not sure just yet how to tile the texture depending on the length of the beam/quad.
- Find the y rotation difference between the two points, and rotate the beam that amount.
- Do the same for the x axis.
- The Z rotation would depend on the location of the camera in relation to the Z axis...if that makes any sense. In my head...it almost seems like the beam's Z rotation will always be perpindicular to camera view angle.
Anyone have any suggestions on going about this?
Answer by e-bonneville · Apr 28, 2010 at 02:30 AM
I've got one simple solution - use Unity's built in Line Renderer, the docs for which are found here.
Thanks! I completely forgot about the line renderer. I think part of my brain filed it under "drawing 1 pixel lines". Silly me. I'll check it out, and see if it meets my needs.
This looks like it will be perfect. I assume in order have a "flowing" effect, you will just swap out textures, each one shifted slightly?
Answer by 3dDude · Apr 28, 2010 at 01:58 PM
the way i do it is i use raycasts first create a empty GameObject then make a cylinder scale its z axis to .5 then rotate is 90 degrees on the x then make it a child of the empty gameObject set the x position and the z position to 0 then make the z to .5 so that the empty gameObject is at the very back of the empty gameObject. the code attached to the empty gameObject should be:
function Update () { var hit : RaycastHit;
if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),hit)){ var distance = Vector3.Distance(transform.position,hit.point);
transform.localScale.z = distance;
} else{transform.localScale.z = 5000;}
}
So i don't know how to make it have a texture but you can use a plane or somthing that will work with a texture. but make the y 1 not 0.5.