- Home /
Question by
Agent27765 · Apr 16, 2019 at 02:05 AM ·
c#2d game2d-platformerraycasthitraycasthit2d
My Raycast2D is registering when facing one side but when going the other direction nothing is "hit."
I just did some bug fixing and found a bug that I cannot seem to figure out. My 2D character attacks and at the end of the animation it calls the function DealDamage(). The problem is that when my player is facing right and is attacking an enemy to its right it works perfectly fine. When it is facing left its as if the enemy is not there and there is "nothing" to hit. I checked and the Raycast returns null so it doesn't even register the enemy. I used DrawRaycast and the Raycast clearly hits the enemy. I don't know what to do.
public void DealDamage()
{
if (facingRight == true && weaponReach < 0)
{
weaponReach *= -1;
}
else if (facingRight == false && weaponReach > 0)
{
weaponReach *= -1;
}
RaycastHit2D hit = Physics2D.Raycast(origin.position, new Vector2(weaponReach, 0), weaponReach, whoIsEnemy);
if (hit.transform.gameObject.layer == 12)
{
EnemyHealth EnemHealth = hit.transform.gameObject.GetComponent<EnemyHealth>();
EnemHealth.TakeDamage(attackDamage);
}
}
When I make the Raycasthit2D the following code everything magically works fine. The problem is that the distance is unlimited and I definitely dont want the player to hit the enemy a mile away.
RaycastHit2D hit = Physics2D.Raycast(origin.position, new Vector2(weaponReach, 0), weaponReach, whoIsEnemy);
Comment