- Home /
what is the best way of making a laser gun that shoots long ray of la
any ideas about how to make that because i don't know if its right to instantiate a long cylinder every time the user press the fire button, i want him to shoot a ray of lazer but i don't know how to do that and i don't want the laser to destroy it self after it collides with other obects i want it to stop going any longer... Thanks in advance
Answer by TehWut · Aug 21, 2011 at 02:54 AM
Just use Raycasting, and then a LineRenderer from the point it hits to the weapon. Or, just make a particle system following the bullet (if its a GameObject).
i don't want to have a bullet i just want laser, i'll try ur way and i'll be back
Try this: It should work much better for what your looking for:
http://unity3d.com/support/documentation/Components/class-TrailRenderer.html
Answer by sketchers1 · Aug 21, 2011 at 05:31 AM
what i did, is this: 1st. make a new empty game object. find a stiped pattern online with 3 stripes. the 2 outside stipes are black and the inside stripe is the color of the lazer. 1. Upload that image to unity assets. and add that pattern to the new object you made. 2. Create a new script with this code:
{
/* im 13
*/ var height = 2.0; var time = 2.0; var alwaysUp = false; var minDistance = 0.1;
var startColor = Color.white; var endColor = Color (1, 1, 1, 0);
class TronTrailSection { var point : Vector3; var upDir : Vector3; var time : float; }
private var sections = new Array();
function LateUpdate () { var position = transform.position; var now = Time.time;
// Remove old sections while (sections.length > 0 && now > sections[sections.length - 1].time + time) { sections.Pop(); }
// Add a new trail section if (sections.length == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance) { var section = TronTrailSection (); section.point = position; if (alwaysUp) section.upDir = Vector3.up; else section.upDir = transform.TransformDirection(Vector3.up); section.time = now; sections.Unshift(section); }
// Rebuild the mesh var mesh : Mesh = GetComponent(MeshFilter).mesh; mesh.Clear();
// We need at least 2 sections to create the line if (sections.length < 2) return;
var vertices = new Vector3[sections.length 2]; var colors = new Color[sections.length 2]; var uv = new Vector2[sections.length * 2];
var previousSection : TronTrailSection = sections[0]; var currentSection : TronTrailSection = sections[0];
// Use matrix instead of transform.TransformPoint for performance reasons var localSpaceTransform = transform.worldToLocalMatrix;
// Generate vertex, uv and colors for (var i=0;i<sections.length;i++) { previousSection = currentSection; currentSection = sections[i]; // Calculate u for texture uv and color interpolation var u = 0.0;
if (i != 0) u = Mathf.Clamp01 ((Time.time - currentSection.time) / time);
// Calculate upwards direction
var upDir = currentSection.upDir;
// Generate vertices
vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height);
uv[i * 2 + 0] = Vector2(u, 0);
uv[i * 2 + 1] = Vector2(u, 1);
// fade colors out over time
var interpolatedColor = Color.Lerp(startColor, endColor, u);
colors[i * 2 + 0] = interpolatedColor;
colors[i * 2 + 1] = interpolatedColor;
}
// Generate triangles indices var triangles = new int[(sections.length - 1) 2 3]; for (i=0;i<triangles.length / 6;i++) { triangles[i 6 + 0] = i 2; triangles[i 6 + 1] = i 2 + 1; triangles[i 6 + 2] = i 2 + 2;
triangles[i * 6 + 3] = i * 2 + 2;
triangles[i * 6 + 4] = i * 2 + 1;
triangles[i * 6 + 5] = i * 2 + 3;
}
// Assign to mesh
mesh.vertices = vertices; mesh.colors = colors; mesh.uv = uv; mesh.triangles = triangles;
}
}
@script RequireComponent (MeshFilter)
3.apply this script to the object u want. 4. add a MeshREnderer to the game Object 5.Assign A particle Material to the MeshRenderer. 6. Make A new lazer Prefab. 7. Drag the game object u have created to the prefab 8. ummmmm make the texture for the prefab FX>Specular or sumthing like that. 9. I think that you want to make it a rigidbody without gravity but i forgot. 10. If you want to see an example of it in my game, just tell me how to post a video on this and i can show you. :D
Answer by em2 · Aug 21, 2011 at 05:55 AM
Maybe you could you use and empty game object and attach a trail rendered paticle component to it and add the textures you want to apply (laser colour, transparency, etc), and then treat the empty game object like your projectile (for collisions, etc). Just a thought.