using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Obstacle : PoolableMonoBehaviour { [SerializeField] private bool destroyOnHit = false; [SerializeField] private float damageOnHit = 0.1f; private AudioSource audioSource; [SerializeField] private float hitRecoilForce = 1.0f; private Dictionary originalPosition = new Dictionary(); private Dictionary originalRotation = new Dictionary(); public float DamageOnHit => damageOnHit; public bool DestroyOnHit => destroyOnHit; public float HitRecoilForce => hitRecoilForce; protected void Start() { audioSource = GetComponent(); foreach(Transform t in GetComponentsInChildren().Where(x => x.gameObject != this.gameObject)) { originalPosition[t.gameObject] = t.localPosition; originalRotation[t.gameObject] = t.localRotation; } } public void OnHit(GameObject other) { if(destroyOnHit) { foreach(Rigidbody r in GetComponentsInChildren().Where(x => x.gameObject != this.gameObject)) r.isKinematic = false; this.GetComponent().enabled = false; } if(audioSource != null) audioSource.Play(); } public override void OnReturn() { foreach(Rigidbody r in this.GetComponentsInChildren()) r.isKinematic = true; foreach(GameObject g in this.GetComponentsInChildren().Where(x => x.gameObject != this.gameObject).Select(x => x.gameObject)) { g.transform.localPosition = originalPosition[g]; g.transform.localRotation = originalRotation[g]; } this.GetComponent().enabled = true; } }