]> git.lizzy.rs Git - SuperMouseAdventure.git/blobdiff - 2DGame/Assets/Scripts/Level_Elements/Key.cs
key collecting v0.2
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Level_Elements / Key.cs
diff --git a/2DGame/Assets/Scripts/Level_Elements/Key.cs b/2DGame/Assets/Scripts/Level_Elements/Key.cs
new file mode 100644 (file)
index 0000000..621b9df
--- /dev/null
@@ -0,0 +1,42 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Key : MonoBehaviour
+{
+    private bool isFollowing;
+
+    public float followSpeed;
+
+    public Transform followTarget;
+
+    // Start is called before the first frame update
+    void Start()
+    {
+        
+    }
+
+    // Update is called once per frame
+    void Update()
+    {
+        if(isFollowing)
+        {
+            transform.position = Vector3.Lerp(transform.position, followTarget.position, followSpeed * Time.deltaTime);
+        }
+    }
+
+    private void OnTriggerEnter2D(Collider2D collision)
+    {
+        if(collision.tag == "Player")
+        {
+            if(!isFollowing)
+            {
+                KeyHolder keyHolder = FindObjectOfType<KeyHolder>();
+
+                isFollowing = true;
+
+                keyHolder.followingKey = this;
+            }
+        }
+    }
+}