Am I not understanding the Line Renderer, or simply bad at math? [Bullet Trails]

Hey, so I’m trying to make bullet trails with the line renderer. Im raycasting for bullets, so its not actual trails, more like line segments showing up for a brief second at a semi-random place on the direction Vector of the shot fired. However, my code is not producing the results I was hoping. I’m a little tried, so maybe I just dun goofed in the math. Anyhow heres the logic

  • Raycast outwards, save hit information
  • save direction vector from Line Renderer (placed at the end of the barrel of the gun) to the hit point
  • Turn on the line renderer intermittently, and set the starting point of a line to be on the direction vector * some scalar
  • set the end point of the line renderer to be on the direction vector, but with some scalar *2
  • When done displaying, move faaar faaar away to Vector3(10000,10000,10000)

And heres the code:

public lineRenderer line;
public Vector2 bulletTrailLengthMinMax;
private bool bulleTrailAvailable;

 if(Physics.Raycast (ray, out hit, Mathf.Infinity)){

            Quaternion rot= Quaternion.FromToRotation(Vector3.up, hit.normal);

                 hit.collider.gameObject.SendMessage("WasShot",damagePerShot,SendMessageOptions.DontRequireReceiver);
			
			if (Random.value * 100f <= chanceOfBulletTrail  bulletTrailAvailable ) {
				hitVector = hit.point - line.transform.position;
				StartCoroutine(DrawBulletTrail());
				
				
			}
      

        }

     StartCoroutine(CoolDown());

    }
	
	private IEnumerator DrawBulletTrail()
	{
		bulletTrailAvailable = false;
		float startPoint = Random.Range(0.1f,0.5f); 
		float lineLenght = Random.Range(bulletTrailLengthMinMax.x*hitVector.sqrMagnitude,bulletTrailLengthMinMax.y*hitVector.sqrMagnitude);
		line.SetPosition(0,line.transform.position+hitVector * startPoint );
		line.SetPosition(1,line.transform.position+hitVector * lineLenght);
		yield return new WaitForSeconds(Time.deltaTime*2f);		 
		//line.SetPosition(0,new Vector3(10000,10000,10000));
		//line.SetPosition(1,new Vector3(10000,10000,10000));
		
		bulletTrailAvailable = true;
		
	}

This, for some reason draws hugely long line segments (they sometimes go backwards through the player).

Im probably screwing up this simple math, could someone check the logic for me?

you’re using the sqrmagnitudes when you set lineLenght… I think you might be wanting to use the actual magnitudes here.

I think an easier way to do this is to save the normal vector direction, hit point and distance.

normal = (hit.point - transform.position).normalized;
position = hit.position;
distance = Vector3.Distance(hit.point, transform.position);

Then set the points at hit.point * scalar of the normalized vector

randomLength = Random.Range (3F, 6F);
random = Random.Range(0 + randomLength, distance - randomLength);

line.SetPosition(0,  position - (random * normal) - (randomLength * normal));
line.SetPosition(1, position - (random * normal));

Something like that. And instead of moving the linerender to hell. Just set its vertex count to 0 when it is not used, and 2 when you use it…

Dammit, both responses seem to use an actual magnitude or a Vector3.distance. I dont think this would be a good thing, since this happens ALOT (this is a machine gun). I will try and implement the changes, see if it works. If it does, I know what the problem was, but will probably have to find another way :slight_smile: Thanks for input though

Well it was because I was not using the proper distances. I’ll stick with Vector3.distance for now, since it seems slightly faster than (a-b).magnitude. Thanks for the help!