70 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts.InteractableObjects
{
public abstract class InteractableObject : MonoBehaviour
{
public AnimationClip clip;
private bool particlePlayed;
private bool animationPlayed;
public bool keepAlive;
// Start is called before the first frame update
void Start()
{
particlePlayed = false;
animationPlayed = false;
keepAlive = false;
}
// Update is called once per frame
void Update()
{
destroyObject();
}
public void OnInteraction(GameObject player)
{
StartCoroutine(playParticle(player));
StartCoroutine(playAnimation(player));
handleInteraction(player);
}
public abstract void handleInteraction(GameObject player);
IEnumerator playParticle(GameObject player)
{
if (GetComponent<ParticleSystem>() != null)
{
Vector3 playerPos = player.transform.position;
playerPos.y = transform.position.y;
Quaternion newRotation = Quaternion.LookRotation(playerPos - transform.position, transform.TransformDirection(Vector3.up));
ParticleSystem particleSystem = GetComponent<ParticleSystem>();
ParticleSystem.ShapeModule shape = particleSystem.shape;
shape.rotation = newRotation.eulerAngles;
particleSystem.Play();
yield return new WaitUntil(() => !particleSystem.isPlaying);
}
particlePlayed = true;
}
IEnumerator playAnimation(GameObject player)
{
if (clip != null)
{
player.GetComponent<Animator>().Play(clip.name);
yield return new WaitForSeconds(clip.length);
}
animationPlayed = true;
}
public void destroyObject()
{
if (particlePlayed && animationPlayed && !keepAlive)
{
Destroy(gameObject);
}
}
}
}