I am trying to make it so a game object flips if the player is not in it’s raycast but it does not seem to work. I know that the raycast works fine because I tested it by logging the name of the object the raycast hit, but my if statement with a boolean does not seem to work. Please help. Thanks!
public Transform viewPoint;
public Transform target;
public float speed = 3f;
public float viewDistance = 10f;
bool FacingPlayer;
void Update()
{
RaycastHit2D hitTarget = Physics2D.Raycast(viewPoint.position, viewPoint.right);
if (Vector3.Distance(transform.position, target.position) < viewDistance)
{
if (hitTarget)
{
FacingPlayer = true;
}
if (!FacingPlayer)
{
Flip();
}
}
}
void Flip()
{
transform.Rotate(0f, 180f, 0f);
}