Hello, I have a small error in my game im not sure what causing it. Basically I have a 2D sprite that flips X in SpriteRenederer & when it does a ray should grown in value until it collides with given layers. But for some reason when facing left (Flip X is True) & the ray collides with a gameobject the ray flips facing right. But for the right side the ray is working as it should.
public Transform viewpos;
public float viewdis;
public LayerMask OBJ;
public bool off;
// DETECTION
if (GetComponent<SpriteRenderer>().flipX)
{
RaycastHit2D viewl = Physics2D.Raycast(viewpos.position, -transform.right, viewdis, OBJ);
if (viewl.collider != null)
{
if (!off)
{
viewdis = viewl.collider.transform.parent.gameObject.transform.position.x - 0.54f;
off = true;
}
}
else
{
off = false;
viewdis += 0.05f;
}
Vector3 dirV = transform.TransformDirection(Vector3.left) * viewdis;
Debug.DrawRay(viewpos.position, dirV, Color.red);
}
else
{
RaycastHit2D viewr = Physics2D.Raycast(viewpos.position, transform.right, viewdis, OBJ);
if (viewr.collider != null)
{
if (!off)
{
viewdis = viewr.collider.transform.gameObject.transform.position.x - 0.54f;
off = true;
}
}
else
{
off = false;
viewdis += 0.05f;
}
Vector3 dirVr = transform.TransformDirection(Vector3.right) * viewdis;
Debug.DrawRay(viewpos.position, dirVr, Color.red);
}