- Home /
Line Renderer not Displaying on Collider2D.hit.point
I am trying to get an aim assist line in my game that will draw a narrow line between a character's position and the mouse world position. I have it working so that if there is no collider between the two it draws the line correctly.
I am using a Physics2D linecast to check if there is a collider between the two points on layer 8 which seems to be working correctly. I am using the raycasthit2d.collider.point to get the point in the gameworld where the ray hit the layer and that seems to be getting the correct point, but when I pass it into my lineRenderer.SetPosition it is not drawing the line at all
Here is the actual snippet of code that I need looked at that isn't working
aimAssistLineRenderer.SetPosition(0, _activeCharacterBehavior.transform.position);
aimAssistLineRenderer.SetPosition(1, new Vector3(hit.point.x, hit.point.y, 1f));
This is the relevant code that is executed in a game object's Update() method
public GameObject aimAssistLine;
public LineRenderer aimAssistLineRenderer;
void PlayerAiming ()
{
var mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (aimAssistLine == null)
{
aimAssistLine = new GameObject();
aimAssistLineRenderer = aimAssistLine.AddComponent<LineRenderer>();
aimAssistLineRenderer.SetPosition(0, _activeCharacterBehavior.transform.position);
aimAssistLineRenderer.SetPosition(1, Camera.main.ScreenToWorldPoint(Input.mousePosition));
aimAssistLineRenderer.SetWidth(.07f,.07f);
}
else
{
var hit = Physics2D.Linecast(_activeCharacterBehavior.transform.position, mouseWorldPos, 1 << 8); //Terrain Layer Mask
if (hit.collider == null) //This works just fine
{
aimAssistLineRenderer.SetPosition(0, _activeCharacterBehavior.transform.position);
aimAssistLineRenderer.SetPosition(1, mouseWorldPos);
}
else //This does not show any line
{
aimAssistLineRenderer.SetPosition(0, _activeCharacterBehavior.transform.position);
aimAssistLineRenderer.SetPosition(1, new Vector3(hit.point.x, hit.point.y, 1f));
}
}
_activeCharacterGameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
Physics2DHelper.ForceLookAt2D (mouseWorldPos, _activeCharacterGameObject.transform);
}
Current Behavior:
Expected Behavior:
Answer by Oliver1135 · Jan 04, 2015 at 04:07 PM
fixed the problem, line was being rendered underneath everything in my scene I forgot I needed to care about the z coordinate even though I was working in 2d.
Fixed code here
aimAssistLineRenderer.SetPosition(0, _activeCharacterBehavior.transform.position);
aimAssistLineRenderer.SetPosition(1, new Vector3(hit.point.x, hit.point.y, -10f));
Your answer

Follow this Question
Related Questions
Better line reflection? 0 Answers
line renderer help 1 Answer
how change linerenderer with png or material.... 0 Answers
Creating a LineRenderer: its always null 2 Answers
create Gizmos such as a wire sphere or line in Update Function 1 Answer