67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
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<GameObject, Vector3> originalPosition = new Dictionary<GameObject, Vector3>();
|
|
private Dictionary<GameObject, Quaternion> originalRotation = new Dictionary<GameObject, Quaternion>();
|
|
|
|
public float DamageOnHit => damageOnHit;
|
|
|
|
public bool DestroyOnHit => destroyOnHit;
|
|
|
|
public float HitRecoilForce => hitRecoilForce;
|
|
|
|
protected void Start()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
foreach(Transform t in GetComponentsInChildren<Transform>().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<Rigidbody>().Where(x => x.gameObject != this.gameObject))
|
|
r.isKinematic = false;
|
|
|
|
this.GetComponent<Collider>().enabled = false;
|
|
|
|
}
|
|
|
|
if(audioSource != null)
|
|
audioSource.Play();
|
|
}
|
|
|
|
public override void OnReturn()
|
|
{
|
|
foreach(Rigidbody r in this.GetComponentsInChildren<Rigidbody>())
|
|
r.isKinematic = true;
|
|
|
|
foreach(GameObject g in this.GetComponentsInChildren<Transform>().Where(x => x.gameObject != this.gameObject).Select(x => x.gameObject))
|
|
{
|
|
g.transform.localPosition = originalPosition[g];
|
|
g.transform.localRotation = originalRotation[g];
|
|
}
|
|
|
|
this.GetComponent<Collider>().enabled = true;
|
|
}
|
|
}
|