I need to change the points, to test it without moving the points i have to move the LaserReceiver instead and it’s not supposed to move in the game.
I solved it with a workaround, i check if the laser hits a laserreceiver and then execute a public function to activate it from the laser script.
But since the collision problem is still there.
I can post my function but it’s pretty long (104 lines) and i was told my code is weird because i use a lot of spaces, if you don’t struggle to read it here is the RayCast function.
Code
void RaycastStart()
{
if(OnLaserRayCastEvent!= null)
{
OnLaserRayCastEvent();
}
_linerenderer.enabled =true;
_edgecollider.enabled =true;
ContactFilter2D contactFilter = new ContactFilter2D().NoFilter();
if(_boxSigns.Length ==0)
{
//hitresults = Physics2D.Raycast(transform.position,transform.up);
hit = Physics2D.Raycast(transform.position,transform.up);
}
else
{
if(BoxesStatusActive())
{
// _lasercollidList.Clear();
hit = Physics2D.Raycast(transform.position,transform.up);
}
else
{
hit = Physics2D.Raycast(transform.position,transform.up,0.5f);
_linerenderer.enabled =false;
_edgecollider.enabled =false;
}
}
if(hit.collider.gameObject.tag == "Barrier")
{
//Debug.Log("BARRIER HIT");
_lasercollidList.Clear();
Physics2D.Raycast(transform.position,transform.up, contactFilter, hitList );
for (int i = 0; i < hitList.Count; i++)
{
_lasercollidList.Add( hitList[i].collider.gameObject);
if(hitList[i].collider.gameObject.GetComponent<Barrier>()!=null)
{
if(_lasercolor ==_lasercollidList[i].GetComponent<Barrier>().barrierColor)
{
Debug.Log("BARRIER SAME COLOR");
_laserhitpoint = hitList[i+1].point;
_linerenderer.SetPosition(0, transform.position);
_linerenderer.SetPosition(1, _laserhitpoint);
// Physics2D.IgnoreCollision(_edgecollider,_collidBarrier._edgecollider,false);
}
else
{
Debug.Log("BARRIER COLOR NOO");
_laserhitpoint = hit.point;
_linerenderer.SetPosition(0, transform.position);
_linerenderer.SetPosition(1, _laserhitpoint);
}
}
}
}
// This if statement is the one i added for the workaround, i can delete it and use the LaserReceiver collision instead
else if(hit.collider.gameObject.tag == "LaserReceivers")
{
hit.collider.gameObject.GetComponent<Laserreciver>()._laserwall = this;
hit.collider.gameObject.GetComponent<Laserreciver>().OnLaserCollision();
_laserhitpoint = hit.point;
_linerenderer.SetPosition(0, transform.position);
_linerenderer.SetPosition(1, _laserhitpoint);
}
else // Any other object is hit so laser stops
{
//Debug.Log("SetLaserPoint");
_laserhitpoint = hit.point;
_linerenderer.SetPosition(0, transform.position);
_linerenderer.SetPosition(1, _laserhitpoint);
}
// Debug.Log("hitobj "+hit.collider.gameObject);
SetCollider();
Debug.DrawRay(transform.position,transform.up, Color.red, Mathf.Infinity,false);
}
I change the points after the raycast hits an object.
I used unity events system to avoid shooting rays continuously on fixed Update.
So any time an object is activated, for example a door is opened or closed, an event is triggered and the RayCastStart function is executed, then it checks the object type it collide with, then checks if the object can be pierced, then it sets the LineRenderer points, then it moves the collider points.
It works with well with the doors (Barriers in the code) but with the Laserreceiver it does not.
I