using UnityEngine; using Random = UnityEngine.Random; public class ProjectileScript : MonoBehaviour { public float damage; public GameObject[] brokenGlass; private void OnCollisionEnter(Collision c) { if (c.collider.CompareTag("Player")) { Debug.Log("Player Hit"); PlayerHealth playerHealth = c.gameObject.GetComponent<PlayerHealth>(); playerHealth.TakeDamage(damage); } else if (c.collider.CompareTag("Enemy")) { Debug.Log("Enemy Hit"); EnemyAi enemyAi = c.gameObject.GetComponent<EnemyAi>(); enemyAi.TakeDamage(damage); } else if (c.collider.CompareTag("Glass")) { Debug.Log("Glass Hit"); c.gameObject.SetActive(false); GameObject spawnedGlass = Instantiate(brokenGlass[Random.Range(0, brokenGlass.Length)]); spawnedGlass.transform.position = c.gameObject.transform.position; Destroy(spawnedGlass, 5f); } else { Debug.Log("Hit"); } 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