Line Renderer glitch?

I got this strange error happing. I made a line renderer for a laser beam for my space ship and it works sorta. It seems as you and the target of the line renderer move the line renderer becomes out of sync or something just for a moment before it “Pops” back to its target. Some times it glitches bad, some times its barly noticable. Here is my script for it. Maby I did something wrong in the script some where.

using UnityEngine;
using System.Collections;

public class MiningBeam : MonoBehaviour {

	private LineRenderer lineRenderer;
	private float counter;
	private float dist;

	public Transform origin;
	//public Transform destination;
	public Transform target = null ;
	public Transform HelperVar ;

	public float lineDrawSpeed = 6f;

	// Use this for initialization
	void Start () {

	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonUp (0)) {
			RaycastHit hit;
			Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
			gameObject.GetComponent<LineRenderer>().enabled = true;
			if (Physics.Raycast (ray1, out hit))
				target = hit.transform;
			else {
				target = null;
				gameObject.GetComponent<LineRenderer>().enabled = false;
		// this will be the final rotation that points towards the target !
		Quaternion HelperRotation ;
		if(counter < dist){
			counter += 0.1f / lineDrawSpeed;
			float x = Mathf.Lerp (0, dist, counter);
			Vector3 pointA = origin.position;
			Vector3 pointB = target.position;
			Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
			lineRenderer.SetPosition (1, pointAlongLine);
			if (counter < 0){
				counter = 0;
		lineRenderer = GetComponent<LineRenderer>();
		lineRenderer.SetPosition (0, origin.position);
		lineRenderer.SetWidth (1.45f, 1.45f);
		dist = Vector3.Distance (origin.position, target.position);

I’m not sure what glitch you actually see, but i see some issues in your code:

  • Lerp expects as third parameter the “t”-value in the range of 0 and 1, however you compare it to “dist” which is a distance in your world and most likely larger than 1.
  • lineDrawSpeed is named wrong :wink: It’s not a speed it’s more a time / delay as it will take longer if the value is getting larger. A speed works the other way round. Usually you move faster when the speed increases.
  • Your counter increase is frame based and not time based. So depending on your frames-per-second you’ll see huge differences in how fast your operation ends. You should multiply the added value by Time.deltaTime.
  • You probably want to reset your counter when you selet a new target,

So you have to decide if you want to use a counter value from 0 to 1 or from 0 to dist.

// counter goes from 0 to "dist"

if(counter < dist){
    counter += lineDrawSpeed * Time.deltaTime; // a real speed in world units per second
    float x = Mathf.Clamp01(counter / dist); // get a value between 0 and 1
    Vector3 pointA = origin.position;
    Vector3 pointB = target.position;
    Vector3 pointAlongLine = x * (pointB - pointA) + pointA;
    lineRenderer.SetPosition (1, pointAlongLine);

// counter goes from 0 to 1

if(counter < 1){
    counter += Time.deltaTime / animTime; // animTime is a time in seconds how long it takes to finish
    Vector3 pointA = origin.position;
    Vector3 pointB = target.position;
    Vector3 pointAlongLine = Mathf.Clamp01(counter) * (pointB - pointA) + pointA;
    lineRenderer.SetPosition (1, pointAlongLine);