So, I’m trying to detect collision on my laser, and it almost works, but it only covers half of my line.
(ignore the red line)
The two dots represent the points I’ve created via script.
It is the same thing on the other side.
private List<Vector2> CalculateColliderPoints()
{
Vector3[] positions = GetPositions();
float width = HitLine.GetComponent<LineRenderer>().startWidth;
float m = (positions[1].y-positions[0].y)/(positions[1].x-positions[0].x);
float deltaX = (width/2f)*(m/Mathf.Pow(m*m+1,.5f));
float deltaY = (width/2)*(1/Mathf.Pow(1+m*m,.5f));
Vector3[] offsets = new Vector3[2];
offsets[0]= new Vector3(-deltaX,deltaY);
offsets[0]= new Vector3(deltaX,-deltaY);
List<Vector2> colliderPositions = new List<Vector2>{
positions[0]+offsets[0],
positions[1]+offsets[0],
positions[1]+offsets[1],
positions[0]+offsets[1]
};
return colliderPositions;
}
private void OnDrawGizmos() {
Gizmos.color = UnityEngine.Color.magenta;
if (ColliderPoints!=null) ColliderPoints.ForEach(p=>Gizmos.DrawSphere(p,.1f));
}
private Vector3[] GetPositions()
{
Vector3[] positions = new Vector3[HitLine.GetComponent<LineRenderer>().positionCount];
HitLine.GetComponent<LineRenderer>().GetPositions(positions);
return positions;
}
This the main code, and then it applies the positions here,
private void FixedUpdate() {
if (IsFiring&&HitLine.GetComponent<LineRenderer>().startWidth>=.85f) // the second part is to see if the laser is in fire mode
{
ColliderPoints = CalculateColliderPoints();
}
}
its also worth mentioning the beam changes sizes, bt even when it is smaller it fails to get the true corner,
If anybody has any tips or experience with this please help me out, I’m not good with this kind of stuff, Thank you.