using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { public float speed = 20f; public int damage = 40; public Rigidbody2D rb; // Start is called before the first frame update void Start() { rb.velocity = transform.right * speed; Destroy(gameObject, 1); } void OnTriggerEnter2D(Collider2D hitInfo) { Enemy enemy = hitInfo.GetComponent<Enemy>(); if (enemy != null) { enemy.TakeDamage(damage); } if (hitInfo.gameObject.tag != "Enemy") { return; } Destroy(gameObject); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter