- Home /
Figured out a way to make lasers, using raycasting and lineRenderers
Make A Laser?
I'm trying to make a laser for a weapon in a space ship game I'm making but not having much luck. I've tried using raycasts and a line renderer but then the line only shows when you hit something and I need it to show all the time. Does anyone have any ideas? I'm using unity free and JavaScript.
EDIT:
here is my script so far:
#pragma strict
var LRenderer : LineRenderer;
function Start () {
gameObject.AddComponent(LineRenderer);
LRenderer = GetComponent(LineRenderer);
LRenderer.SetPosition(0, transform.position);
LRenderer.SetPosition(1, transform.position);
LRenderer.SetColors(Color.blue, Color.white);
}
function Update () {
var ray = new Ray (transform.position, transform.forward);
var hit : RaycastHit;
if(Input.GetKey(KeyCode.Space)){
if(Physics.Raycast(ray, hit)){
LRenderer.SetPosition(0, ray.origin);
LRenderer.SetPosition(1, hit.point);
}
}
}
The issue is I can figure out how to make the ray be drawn while shooting as well as when it hits. It this case the linerenderer only shows when the ray hits, and I want it to show while the ray is firing along the ray.
Line renderer can show whenever you want it to. Post your script for the laser so far.
Does Debug.DrawRay(); work in the compiled version of the game? Or just in the editor?
Answer by Benproductions1 · Sep 11, 2013 at 12:04 AM
Hello,
Did you forget to google it? Just type the title into google (as stated in the FAQ):
http://lmgtfy.com?q=Unity+Make+a+Laser
Hope this helps,
Benproductions1
To be fair I personally found setting up a nice laser to be one of the more difficult aspects of my current project, and there are still aspects of it I wish to improve even with extensive google searches. But it does seem that some people don't search enough or at all before asking questions here.
Can be difficult to tell who is just new to program$$anonymous$$g and/or lazy and who is trying their best for many hours/days but still can't figure something out.
It does look like his code is a little bit light for someone who gave serious effort.
I did clean my code up a bit before posting, and have been trying to do this all day. I have a week and a half deadline to do this, so some help would be appreciated.
@lancer if my answer answered your question (or helped you do it) you should accept my answer ins$$anonymous$$d of closing the question. Closed questions usually means "bad question" :)
Your answer didn't help, sorry, I figured out my own way of doing it.
Follow this Question
Related Questions
Audio play problem on raycast hit colider.gameObject.tag 1 Answer
Raycast object as far as possible? 1 Answer
ray cast not working ? 2 Answers
Detect face of cube clicked 4 Answers
Raycast script help? 1 Answer