I am trying to create a bullet with one raycast going away from the player and another going towards the player to get the exit point of the bullet when it hits an enemy. I’m using this code but I only get “backwards” in the console when I run the game and shoot an enemy.
RaycastHit2D Hit = Physics2D.Raycast(Gun.transform.position, AimDirection, MaxRange, ~Ignore);
if(Hit.collider != null && Hit.transform.tag == "Enemy")
{
Instantiate(HitPoint, Hit.point, Player.transform.rotation, Hit.transform);
Debug.Log("forwards");
}
RaycastHit2D BackwardCast = Physics2D.Raycast(AimTowards.transform.position, -AimDirection, MaxRange, ~Ignore);
if(BackwardCast.collider != null && BackwardCast.transform.tag == "Enemy")
{
Instantiate(ExitPoint, BackwardCast.point, Player.transform.rotation, BackwardCast.transform);
Debug.Log("backwards");
}
If I remove the if(Hit.transform.tag == “Enemy”) from the forwards raycast and then run the game and shoot an enemy, then I get both backwards and forwards in the console. Why would this be the case though, the backwards raycast has the exact same if statement?