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?