From 5b1d030d3d022f3163444c87ced18eea0608fd6e Mon Sep 17 00:00:00 2001 From: Soulaha Balde Date: Sun, 23 Oct 2022 18:36:32 -0400 Subject: [PATCH] Shake obj before vanish --- Assets/Scripts/GrappleHook.cs | 4 ++++ Assets/Scripts/Vanisher.cs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/GrappleHook.cs b/Assets/Scripts/GrappleHook.cs index 4bb9a31..0f2cbcf 100644 --- a/Assets/Scripts/GrappleHook.cs +++ b/Assets/Scripts/GrappleHook.cs @@ -42,6 +42,8 @@ public class GrappleHook : MonoBehaviour private bool grappled = false, grappling = false, boosted = false, changingLength = false, isStunned = false; float changingLengthDir = 0; private Vector3 currGrappleEndPos; + private GameObject hookedTo; // The obj we are hooked to + public GameObject HookedTo{get => hookedTo; set => hookedTo = value;} #region private methods void Start() @@ -136,6 +138,7 @@ public class GrappleHook : MonoBehaviour if(hit.transform.tag.Equals("Vanishing")){ hit.transform.gameObject.GetComponent().Begin(this); } + hookedTo = hit.transform.gameObject; currGrappleEndPos = transform.position; } @@ -145,6 +148,7 @@ public class GrappleHook : MonoBehaviour lr.enabled = false; boosted = false; Destroy(joint); + hookedTo = this.gameObject; } private Vector2 WorldToUI(Vector3 point){ diff --git a/Assets/Scripts/Vanisher.cs b/Assets/Scripts/Vanisher.cs index 99dcf42..780ea5b 100644 --- a/Assets/Scripts/Vanisher.cs +++ b/Assets/Scripts/Vanisher.cs @@ -8,21 +8,41 @@ public class Vanisher : MonoBehaviour private float timeToVanish; [SerializeField] private float timeToAppear; + private float timer = 0; private GrappleHook grapple; private bool isVanishing = false; + private Vector3 originalPos; //TODO represent obj is breaking + + private void Start() { + originalPos = transform.localPosition; + } + private void Update() { + timer += Time.deltaTime; + if(isVanishing){//Shake + float speed = timer / timeToVanish; + float x = Random.Range(-.5f, .5f) * speed * .01f; + float y = Random.Range(-.5f, .5f) * speed * .01f; + transform.localPosition += new Vector3(x, y, originalPos.z); + } + + } public void Begin(GrappleHook grapple){ - if(isVanishing)return; this.grapple = grapple; + if(isVanishing)return; Invoke("Vanish", timeToVanish); isVanishing = true; + originalPos = transform.localPosition; + timer = 0; } private void Vanish(){ - grapple.Unhook(); + grapple?.Unhook(); gameObject.SetActive(false); Invoke("Appear", timeToAppear); isVanishing = false; + timer = 0; + transform.localPosition = originalPos; } private void Appear(){ @@ -34,4 +54,12 @@ public class Vanisher : MonoBehaviour Begin(other.gameObject.GetComponent()); } } + private void OnCollisionExit(Collision other) { + if(other.gameObject.tag.Equals("Player")){ + if(!grapple.HookedTo.Equals(this.gameObject)){ + grapple = null; + } + } + + } }