- Home /
Linerenderer between RaycastHit.point and a Gameobject
Hello
I am currently trying to make a Quake 3 style instagib game, as it is right now i've completed most of the scripts, but there's one thing i cant really figure out.
How do i create a line Between the point where the ray hit a gameobject and the muzzle of my weapon?
I'm programming in C#, so it would be appreciated if any answers/examples provided would be in C#, if this is not possible then Javascript works as well, im just being lazy :)
Thanks in advance.
- Seth
Answer by CHPedersen · May 03, 2011 at 08:51 AM
The way I see it, you've mostly answered the question yourself. :) You should be able to do that with a LineRenderer. The LineRenderer renders a line with a width and some texture you choose between all points specified in an array it's got, called "Positions". If you create the LineRenderer at runtime, you then simply insert the position of the muzzle of the weapon first, then the point returned by RaycastHit.point. The LineRenderer should then draw a straight 3D-line between those two points.
Remember, when working with LineRenderers at runtime, to specify its vertex count with LineRenderer.SetVertexCount before you add positions to it. If you don't, its SetPosition method will throw index out of range exceptions.
As an added comment, it might be wise to set LineRenderer.useWorldSpace to true, or it might make it more cumbersome to position the line properly.
Thanks a bunch for the help, its all working as intended now :)