]> git.lizzy.rs Git - SuperMouseAdventure.git/blobdiff - 2DGame/Assets/Scripts/Mouse/MouseController.cs
improved movement v0.1
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Mouse / MouseController.cs
index 122ae8477929bec360e0d7e96ed4976cf2f2f26b..b672289596a302cd6ff74c20fedf80d175ae6710 100644 (file)
@@ -11,10 +11,13 @@ public class MouseController : MonoBehaviour
     public float jumpForce;
     private float jumpTimeCounter;
     public float jumptime;
-    private bool isJumping;
     private float moveInput;
+
     public float coyoteTime;
-    private bool jumpAllowed;
+    private float coyoteTimeCounter;
+
+    public float jumpBufferLength;
+    private float jumpBufferCounter;
 
     [HideInInspector]
     public bool isGrounded;
@@ -30,12 +33,13 @@ public class MouseController : MonoBehaviour
 
     [SerializeField]
     GameObject bullet;
+    
     [SerializeField]
     Transform bulletSpawnPos;
+
     [SerializeField]
     private float shootDelay = 0.5f;
 
-    // Start is called before the first frame update
     void Start()
     {
         //Hier wird der Rigidbody initialisiert
@@ -44,38 +48,39 @@ public class MouseController : MonoBehaviour
         powerUps = GetComponent<PowerUps>();
     }
 
-    // Update is called once per frame
     void Update()
     {
-        if (!isGrounded)
+        //manage coyote time
+        if (isGrounded)
         {
-            StartCoroutine(CoyoteTime());
-        }
-        else
+            coyoteTimeCounter = coyoteTime;
+        } else
         {
-            jumpAllowed = true;
+            coyoteTimeCounter -= Time.deltaTime;
         }
 
-               if (jumpAllowed && Input.GetButtonDown("Jump"))
+        //manage jump buffering
+        if (Input.GetButtonDown("Jump"))
         {
-            isJumping = true;
-            jumpTimeCounter = jumptime;
-            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
+            jumpBufferCounter = jumpBufferLength;
+        } else
+        {
+            jumpBufferCounter -= Time.deltaTime;
         }
 
-        if (Input.GetButton("Jump") && isJumping)
+        //jump
+        if (jumpBufferCounter >= 0 && coyoteTimeCounter > 0)
         {
-            if (jumpTimeCounter > 0)
-            {
-                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
-                jumpTimeCounter -= Time.deltaTime;
-                FindObjectOfType<AudioManager>().Play("sprung");
-            }
+            isGrounded = false;
+            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
+            jumpBufferCounter = 0;
+            coyoteTimeCounter = 0;
         }
-
-        if (Input.GetButtonUp("Jump"))
+        
+        //jumping with different height
+        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
         {
-            isJumping = false;
+            rb.velocity = new Vector2(rb.velocity.x, jumpForce * 0.5f);
         }
 
         if (rb.velocity.x < 0)
@@ -133,7 +138,7 @@ public class MouseController : MonoBehaviour
 
                if (moveInput != 0)
                {
-                       acceleration *= moveInput * 4;
+                       acceleration *= moveInput * 3;
                }
                else if (isGrounded)
                {
@@ -156,12 +161,6 @@ public class MouseController : MonoBehaviour
                }
     }
 
-    IEnumerator CoyoteTime()
-    {
-        yield return new WaitForSeconds(coyoteTime);
-        jumpAllowed = false;
-    }
-
     void ResetShoot()
     {
         isShooting = false;