using System.Collections; using System.Collections.Generic; using UnityEngine; public class TambourineBehavior : MonoBehaviour { private Rigidbody2D rb; // private float timer; private Animator animator; private GameObject collidedObject; private float timeLerped = 0.0f; private float timeToLerp = 0.2f; private GameObject player; public bool pinned = false; public AudioSource tambourineHitSound; private bool returnToPlayer = false; void Awake() { this.gameObject.GetComponent().enabled = true; rb = this.gameObject.GetComponent(); animator = this.gameObject.GetComponent(); player = GameObject.FindGameObjectWithTag("Player"); } void Start() { // rb.AddForce(new Vector2(horizSpeed, vertSpeed), ForceMode2D.Impulse); StartCoroutine(CheckToDestroy()); } void Update() { // if (Input.GetKeyUp(KeyCode.K)) { // Destroy(this.gameObject); // } if (player == null) { DestroySelf(); } else { if (collidedObject != null && collidedObject.tag != "grappleSurface" && !returnToPlayer) { rb.constraints = RigidbodyConstraints2D.FreezeAll; // this.gameObject.transform.position = col.transform.position; timeLerped += Time.deltaTime; this.gameObject.transform.position = Vector2.Lerp(this.gameObject.transform.position, collidedObject.transform.position, timeLerped/timeToLerp); if (this.gameObject.transform.position.x == collidedObject.transform.position.x && this.gameObject.transform.position.y == collidedObject.transform.position.y && !pinned) { animator.SetBool("pinned", true); pinned = true; tambourineHitSound.Play(); } else { // print("pinned, but not same position: " + this.gameObject.transform.position + " / " + collidedObject.transform.position); } } else if (returnToPlayer) { Destroy(rb); Destroy(this.gameObject.GetComponent()); animator.SetBool("pinned", false); pinned = false; timeLerped += Time.deltaTime; this.gameObject.transform.position = Vector2.Lerp(this.gameObject.transform.position, player.transform.position, timeLerped/0.1f); if (this.gameObject.transform.position.x == player.transform.position.x && this.gameObject.transform.position.y == player.transform.position.y) { player.GetComponent().SetHasTambourine(true); Destroy(this.gameObject); } } } } void OnTriggerEnter2D(Collider2D col) { // print(col.tag); if (col.tag == "Enemy") { this.gameObject.GetComponent().enabled = false; collidedObject = col.gameObject; print("Pinning to enemy"); this.gameObject.GetComponent().enabled = false; collidedObject.GetComponent().pinned = true; collidedObject.GetComponent().TogglePin(true); } else if (col.tag == "Projectile") { this.gameObject.GetComponent().enabled = false; collidedObject = col.gameObject; print("pinned to projectile"); this.gameObject.GetComponent().enabled = false; collidedObject.GetComponent().Pin(); } } void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "wall") { DestroySelf(); } } IEnumerator CheckToDestroy() { yield return new WaitForSeconds(3f); // print("waited 5"); if (!player.GetComponent().grapplingRope.isGrappling) { DestroySelf(); } } public void DestroySelf() { if (player != null) { timeLerped = 0.0f; returnToPlayer = true; } if (collidedObject != null && collidedObject.tag == "Enemy") { collidedObject.GetComponent().pinned = false; collidedObject.GetComponent().TogglePin(false); } else if (collidedObject != null && collidedObject.tag == "Projectile") { collidedObject.GetComponent().Explode(); } if (player == null) { Destroy(this.gameObject); } else { player.GetComponent().grapplingGun.ReleaseGrapple(); } } }