- Home /
LineRenderer (strange bug)
Hello, I have a pretty weird problem with the LineRenderer component: I want to join each WayPoint (capsule) of my scene with LineRenderer in a progressive way. I managed to do it with the code below but each time it touches a WayPoint (capsule), a line is created to the point 0,0,0 for 1 frame before disappearing. I do not understand ... my code is correct and does not mention doing that.
my assumption is that it comes when I increment its size '' lineRenderer.positionCount ++ "which makes me create a time 0,0,0 before the position changes.
Do you have an idea to avoid this temporary line please? Here is my code that I commented the 5/6 lines of code concerned by the LineRenderer with '' // here '':
LineRenderer lineRenderer;
private int indexLine = 0;
public float linearSpeed = 5;
private void Start()
{
lineRenderer = gameObject.GetComponent<LineRenderer>(); //here
lineRenderer.positionCount = 1; //here
lineRenderer.SetPosition(0, start.transform.position); //here
NavigateTo(start, end);
}
void Update()
{
if (currentPath != null && currentPath.Count > 0)
{
if (moveTimeCurrent < moveTimeTotal)
{
moveTimeCurrent += Time.deltaTime;
if (moveTimeCurrent > moveTimeTotal)
moveTimeCurrent = moveTimeTotal;
var pointALongLine = Vector3.Lerp (currentWaypointPosition, currentPath.Peek(), moveTimeCurrent / moveTimeTotal); //here
lineRenderer.SetPosition(indexLine, pointALongLine); //here
} else
{
currentWaypointPosition = currentPath.Pop();
if (currentPath.Count == 0)
Stop ();
else
{
lineRenderer.positionCount++; //here
indexLine = indexLine + 1; //here
moveTimeCurrent = 0;
moveTimeTotal = (currentWaypointPosition - currentPath.Peek()).magnitude / linearSpeed;
}
Answer by fafase · Mar 22, 2019 at 06:39 AM
I'd say it comes from the fact that in the else part, you are adding a new entry in the array without setting any value to it. So it gets default.
You could try to add the value and set it as the current value so it won't show the full line for a frame again.
lineRenderer.positionCount++; //here
lineRenderer.SetPosition(lineRenderer.positionCount - 1, currentWaypointPosition);
This should add a new entry and set it as the last one.
Oh...I did not think about it! Thank you very much fafase!
PS: I specify for those with the problem that this is '' lineRenderer.SetPosition (lineRenderer.positionCount-1, currentWaypointPosition); '' because length doesn't exist for lineRenderer.
Your answer
Follow this Question
Related Questions
Add specific movement on double click 1 Answer
Mathf.Lerp not working 2 Answers
Move object while rotating another one 1 Answer
Setting the position of linerenderer 1 Answer
How do I translate around a circle? 3 Answers