private void OnTriggerEnter2D(Collider2D collision) { if ((collision.tag == "Player" && src == BulletSource.enemy) || (collision.tag == "Enemy" && src == BulletSource.player)) { if (effectPrefab != null) { Instantiate(effectPrefab, transform.position, Quaternion.LookRotation(rb.velocity.normalized, Vector3.forward)); } FightStatus fightStatus = collision.gameObject.GetComponent <FightStatus>(); if (fightStatus) { fightStatus.Hurt(damage); } foreach (SpriteRenderer sr in srs) { sr.enabled = false; } rb.constraints = RigidbodyConstraints2D.FreezeAll; Destroy(gameObject, 1); if (hitSE) { hitSE.Play(); } } }
public void FireMinigun(Vector2 position, Vector2 direction, float speed, int gunIndex, int power) { BulletType bulletType = BulletType.minigun; // Create bullet trail TrailRenderer trail = Instantiate(bulletTrails[(int)bulletType], position, Quaternion.identity); RaycastHit2D hit = Physics2D.Raycast(position, direction, 1000, bulletLayerMask); ParticleSystem hitEffect = backgunHitEffects[gunIndex]; if (!hit) { trail.AddPosition(position); trail.transform.position = position + direction * Random.Range(minigunTrailOffsetMin, minigunTrailOffsetMax) + direction * Random.Range(minigunTrailDistanceMin, minigunTrailDistanceMax); } else { float originDistance = Random.Range(minigunTrailHitOffsetMinRatio, minigunTrailHitOffsetMaxRatio) * hit.distance; float trailDistance = Random.Range(minigunTrailHitDistanceMinRatio, minigunTrailHitDistanceMaxRatio) * hit.distance; Vector2 origin = position + direction * originDistance; trail.AddPosition(origin); trail.transform.position = origin + direction * trailDistance; hitEffect.transform.position = hit.point; hitEffect.transform.rotation = Quaternion.LookRotation(direction, Vector3.forward); if (!hitEffect.isEmitting) { hitEffect.time = 0; hitEffect.Play(); } lastBackgunHitEffect[gunIndex] = Time.time; backgunHitSE[gunIndex].Play(); fightStatus = hit.collider.gameObject.GetComponent <FightStatus>(); if (fightStatus) { fightStatus.Hurt(power); } } }