-Updated-
Alright I’ve managed to create a laser effect thanks to Dele’s help. Now I have to figure out how to shoot it, I was able to attach it to my ship, but every time I would shoot it would just stay in place. Any help?
-Updated-
Here’s what I have so far, from help from a previous forum, this will be an open script so you help out it befits you.
Alright, I’m making a si-fi style spaceship shooter. It’s still in early development, but I trying to make a tracer similar to the pics below. I have the rocket script from the fps tutorial that I altered, but I still can’t figure it out. It’s just smoke and I want more of a sci-fi laser or plasma tracer. Any idea on how to do this? I have examples of what I want it to look like below, mines the first one. (not too bad for a couple days of work)
I looked over the script they had there and tried to make sense of it, but couldn’t really understand it. Ok the code calls for a line render and shoots it in the forward direction, then if it impacts something then you are able to transform it, right? Which could be an explosion effect with damage…But when I put the code into unity on a model it calls for a line render, but I have no objects that can act as a line render. How do I create one? I’m good at modeling and texturing so that isn’t a problem, I just don’t get what I need to do next. If any one can help me out that’d be great here’s the code.
var line : LineRenderer;
function Update () {
var ray = new Ray (transform.position, transform.forward);
var hit : RaycastHit;
line.SetPosition(0, ray.origin);
if (Physics.Raycast (ray, hit, 100)) {
line.SetPosition(1, hit.point);
} else {
line.SetPosition(1, ray.GetPoint(100));
}
}
Eve (the second screenshot doesn’t show a laser platform though) uses a combination of things to achieve their laser effect. A line renderer, particles and actual geometry with an animated texture at the point of impact.
Thanks for the help, but I’m not super good at programming, I can try though. Can anyone help me with writing the script? It’ll be an open to the public script so everyone be able to use it in their games. It’ll be perfect for almost any game, so lets get going and try to write this.
You’re more likely to get help if you at least try it, then ask specific questions as you get stuck. Read the manual, perform searches on the forums, etc. You can’t just expect people to do everything for you. With that said, I realize that it can be difficult to know where to start when it comes to this particular topic.
Ok thanks for the suggestions and I looked over the script they had there and tried to make sense of it, but couldn’t really understand it. Ok the code calls for a line render and shoots it in the forward direction, then if it impacts something then you are able to transform it, right? Which could be an explosion effect with damage…But when I put the code into unity on a model it calls for a line render, but I have no objects that can act as a line render. How do I create one? I’m good at modeling and texturing so that isn’t a problem, I just don’t get what I need to do next. If any one can help me out that’d be great here’s the code.
var line : LineRenderer;
function Update () {
var ray = new Ray (transform.position, transform.forward);
var hit : RaycastHit;
line.SetPosition(0, ray.origin);
if (Physics.Raycast (ray, hit, 100)) {
line.SetPosition(1, hit.point);
} else {
line.SetPosition(1, ray.GetPoint(100));
}
}
Create a cube or something. Drag and drop that script onto the cube. Then create an empty game object. Attach a “Line Renderer” component to the empty game object (Component=>Miscellaneous=>Line Renderer). Assign a material to the object and adjust any line rendering settings that you want. For a material I would suggest using something like the “particles/additive” shader, but use whatever shader you want. Drag and drop the new Line Renderer object that you created onto the exposed script variable attached to the cube.
You should see a line shooting out from the cube when you hit play. If you move objects that have a collider component in front of the line, the line will stop when it hits the object.
There are two different things happening in the script:
1. A Ray is being cast. This is an invisible ray being shot out from a specified position, at a specified angle, and for a specified length. If it collides with a collider, you can collect all kinds of information (like the exact point in 3d space where it hit the collider, or the object that it hit, etc.).
2. The Line Renderer is using the information gathered from the ray to draw a visual line. It uses the object as the start point for the line. If the ray hits a collider, it uses the hit point for the end of the line, otherwise it uses a point from where the ray ended (the length defined for the ray). It uses the Line Renderer object to render this line between those two points.
If you’re unsure about some of the calls (like transform.position), look it up in the scripting reference ( Unity - Scripting API: ). They explain quite a few things very well, and often give examples.
Thank you very much Dele all I needed was some guidance and now I have a decent looking laser effect. I just now need to tweak it so that it shoots out only on command like when I want to shoot or when an enemy wants to shoot. I looked over the link you sent me and I’m unsure where to start and how to do this, but thank you very much for your detailed and quick response Dele. There’s a screen shot of the line render effect I did on the top of the post if you didn’t see it yet.
Having the enemy shoot might be a little more tricky than having the player shoot. So starting with the player shooting might be best. I’m not going to write it for you, but I’ll point you in the right direction.
In order to allow the player to “shoot” the laser, you’ll need two things to start.
1. You’ll need to be able to know when the user is holding down the fire button.
2. You’ll need to place all that code you already have into a conditional statement, so that it only executes when the fire button is pressed.
This should get you started:
If you do not follow what is going on, you might want to study scripting a bit. The link below is where I first learned how to script. Technically it is Jscript, but the syntax is virtually identical to Javascript and can explain the basics like variables, conditional statements, loops, etc. http://msdn.microsoft.com/en-us/library/6974wx4d(VS.85).aspx