- Home /
Line Renderer. How to lock z axis for side scroller?
I'm making a side scrolling platformer/shooter and want the player to fire a laser when the left mouse button is held and the laser to follow the mouse (twin stick shooter type controls). The line should origin from the players gun and to infinity, unless it hits collision or an enemy.
I sourced the following code, which I attached to the player, which sort of works for mockup visualisation, but if it doesnt hit any assets it wont render, and if there are assets on a different value along the z axis, i.e. background decorations, when I click the line will render from the player to that asset (forgive the indentation, I'm new to scripting and trying my best):
#pragma strict
@script RequireComponent (LineRenderer)
var mouse : Vector2;
var hit : RaycastHit;
var range : float = 100.0;
var line : LineRenderer;
var lineMaterial : Material;
var ray : Ray;
function Start()
{
line = GetComponent(LineRenderer);
line.SetVertexCount(2);
line.renderer.material = lineMaterial;
line.SetWidth(0.1f, 0.25f);
}
function Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, range))
{
if(Input.GetMouseButton(0))
{
line.enabled = true;
line.SetPosition(0, transform.position);
line.SetPosition(1, hit.point );
}
else
line.enabled = false;
}
}
I want the line to render regardless, and be locked to 0 on the z axis, the same as the player. Any help would be greatly appreciated. I have searched extensively for answers but not found anything specific.
Answer by robertbu · Sep 30, 2013 at 03:32 PM
On line 30, insert:
hit.point.z = 0.0;
Or alternately if you are going to use hit.point;
var v3 = hit.point;
v3.z = 0.0;
line.SetPosition(1, v3);
Thanks for replying. If I change line 30 to:
line.SetPosition(1, hit.point.z = 0.0 );
I get a compile error.
Alternatively, if I add:
var v3 = hit.point; v3.z = 0.0;
and change line 30 to:
line.SetPosition(1, v3);
Then my laser will project straight up with a finite range.
Could you possibly show me in context, if I have done it wrong? (I can confirm my world co-ords are set up with z as the depth).
#pragma strict
@script RequireComponent (LineRenderer)
var mouse : Vector2;
var hit : RaycastHit;
var range : float = 100.0;
var line : LineRenderer;
var line$$anonymous$$aterial : $$anonymous$$aterial;
var ray : Ray;
function Start()
{
line = GetComponent(LineRenderer);
line.SetVertexCount(2);
line.renderer.material = line$$anonymous$$aterial;
line.SetWidth(0.1f, 0.25f);
}
function Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, range))
{
if(Input.Get$$anonymous$$ouseButton(0))
{
line.enabled = true;
line.SetPosition(0, transform.position);
hit.point.z = 0.0;
line.SetPosition(1, hit.point );
}
else
line.enabled = false;
}
}
That works brilliantly. Thank you.
FAO others looking for this solution:
It will still not render if there is nothing for the cursor to hit, but that can be solved temporarily by placing a plane in the background. Also the line will pass through collision unless the cursor is over the actual collision being aimed at. But for the purpose of my question - robertbu has solved it.
Note you can solve the "not render if there is nothing for the cursor to hit" issue by detecting when it does hit, establishing a default distance and using Camera.ScreenToWorldPoint() to place the point.
Thanks. I'm assu$$anonymous$$g the default distance would be 0 along the z axis in world space. I just need to work out how to implement it, like I said I'm still getting my head around scripting, but I n eed to experiment. $$anonymous$$y initial reaction would be to replace Camera.main.ScreenPointToRay with Camera.ScreenToWorldPoint, but the lack of Ray makes me think it will be an issue. I'll try after work.
Your answer
Follow this Question
Related Questions
LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer
LineRenderer not working 0 Answers
LineRendering Forward 1 Answer
Creating a Ray Gun 1 Answer
Laser using line renderer 1 Answer