My projectile is passing through walls - which is something I don’t want.
The destroy game object works when it hits a enemy but when it touches a wall it goes through it i dont use onCollisionenter2d because it is a trigger can someone help
public class Projectile : MonoBehaviour
{
public int damage = 10;
public Vector2 moveSpeed = new Vector2(3f, 0);
public Vector2 knockback = new Vector2(0, 0);
Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
rb.velocity = new Vector2(moveSpeed.x * transform.localScale.x, moveSpeed.y);
}
private void OnTriggerEnter2D(Collider2D collision)
{
Damageable damageable = collision.GetComponent<Damageable>();
if (damageable != null)
{
Vector2 deliveredKnockback = transform.localScale.x > 0 ? knockback : new Vector2(-knockback.x, knockback.y);
bool gotHit = damageable.Hit(damage, deliveredKnockback);
if (gotHit)
Debug.Log(collision.name + " hit for " + damage);
Destroy(gameObject);
if (collision.CompareTag("Tilemap"))
{
Destroy(gameObject);
}
}
}
}
Can you try to make the walls bigger or the bullet slower? Maybe if the wall is really small or/and the bullet is really fast the collision won't be seen due to the movement amount per fixed update being higher than the wall size!
– Patrickmol