- Home /
How to make LineRenderer in GameObject Facing?
I'm trying to Draw a line in front of my character but its not working. It's my code
I Tried this
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.forward, 50);
if (hitInfo.collider != null) {
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, hitInfo.point);
} else {
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, transform.position + transform.forward * 50);
}
and i tried this
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.forward, 50);
if (hitInfo.collider != null) {
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, transform.forward * 50);
} else {
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, transform.forward * 50);
}
My LineRenderer not getting Y axis, how i correctly this? I'm using Joystick to rotate the player.
the second solution will not work, but why is not working the first one? what happens when you try that one?
The first solution draw the line, but the line not being drawed in the correctly direction, i dont know how i get the correctly angle.
Can you share a screenshot to see how it looks like using the first code?
Are you calling that code into an Update function? As a side note, don't call GetComponent<> that often, since it is a performance heavy function. If you need the component a lot of times, just store it into a variable inside Start() or Awake().
No, i'm calling the code when the player touch the right joystick. $$anonymous$$y issue is only the line rotation direction.
I don't think I can help you then, sorry. Your code should work, since it works for me and I can't see any flaws in it.
$$anonymous$$aybe the problem is elsewhere in your script? Is this the only place where you change the line renderer?
Answer by MrRightclick · Feb 05, 2019 at 12:41 PM
You're using Transform.forward which equals to (0, 0, 1). If you're moving and rotating on the X and Y axes, perhaps try using Transform.up?
edit:
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, transform.up * 50);
hello, line renderers doesnt work with directions, but with 2 points, so if he needs to get the point in the forward direction, he just needs to apply player.forward, i dont know why his first attempt doesnt work to be honest.
Yes, but he's drawing the line using
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, transform.forward * 50);
which sets the line end to a a point on (transform.forward * 50), which goes on the Z-axis, hence drawing the line "downwards" from the character, or in other words into the ground.
This is all assu$$anonymous$$g he's using the 2D view of Unity, which means X is horizontal, and Y is vertical, leaving Z to deal with depth (in other words, up towards the sky and down towards the ground for the 2D character).
thats true, my fault i didnt see the screenshot of the game and i was imagine a 3d game. then changing to transform.left should fix it. but i am not sure if he means that the y of the linerenderer is 0, he dont even need to uodate the line by script if he just needs to set it up to look in one direction.