]> git.lizzy.rs Git - SuperMouseAdventure.git/commitdiff
Komplexeres Physiksystem und Knockback
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 11 Aug 2021 15:44:58 +0000 (17:44 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 11 Aug 2021 15:44:58 +0000 (17:44 +0200)
2DGame/Assets/Scripts/Gegner/EnemyCollision.cs
2DGame/Assets/Scripts/Gegner/EnemyScript.cs
2DGame/Assets/Scripts/Maus/Health.cs
2DGame/Assets/Scripts/Maus/MouseController.cs

index a1df122c9a1f4970319c236011b6d9c6869c8578..16a5ac55b1b38242cf5b1026f3a346695aba6376 100644 (file)
@@ -18,7 +18,7 @@ public class EnemyCollision : MonoBehaviour
     EnemyScript eS;
 
     public float flashingTime = 0.3f;
-    
+
     private bool invulnerable = false;
 
     // Start is called before the first frame update
@@ -36,7 +36,17 @@ public class EnemyCollision : MonoBehaviour
         if (collision.gameObject.CompareTag("Player") && invulnerable == false)
         {
             powerUps.mouseIsGardener = false;
-            health.GetDamage(eS.enemyDamage);
+            health.DealDamage(eS.enemyDamage);
+
+            Vector2 mousePos = collision.gameObject.GetComponent<Transform>().position;
+            Vector2 enemyPos = GetComponent<Transform>().position;
+
+            Vector2 knockback = mousePos - enemyPos;
+            knockback.Normalize();
+            knockback *= eS.enemyDamage * 100;
+
+            collision.gameObject.GetComponent<Rigidbody2D>().velocity += knockback;
+
             FindObjectOfType<AudioManager>().Play("drsh");
             StartCoroutine ("GetInvincible");
         }
index 0f49d518acc90377b735f759e2f2af8dd2715b72..e34a8849e2d8a5cbcc3b3f25168fcd74263e16be 100644 (file)
@@ -10,15 +10,15 @@ public class EnemyScript : MonoBehaviour
     // Update is called once per frame
     void Update()
     {
-        if(enemyHealth == 0)
+        if (enemyHealth <= 0)
         {
-            gameObject.SetActive(false);        
+            Destroy(gameObject);
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
-        if(collision.gameObject.CompareTag("Bullet"))
+        if (collision.gameObject.CompareTag("Bullet"))
         {
             enemyHealth--;
             Destroy(collision.gameObject);
index 6ae8998f7a29e2aed8685fbdbc99b7ed49459002..4ac4fe26e6a954fc6e48178f94bc7bed21579aca 100644 (file)
@@ -12,15 +12,10 @@ public class Health : MonoBehaviour
     public Sprite fullHeart;
     public Sprite emptyHeart;
 
-    private void Start()
-    {
-
-    }
-
     // Update is called once per frame
     void Update()
     {
-        if(mouseHealth > numberOfHearts)
+        if (mouseHealth > numberOfHearts)
         {
             mouseHealth = numberOfHearts;
         }
@@ -28,7 +23,7 @@ public class Health : MonoBehaviour
         for (int i = 0; i < hearts.Length; i++)
         {
             //Wenn i kleiner als die Gesundheit, zeige ein volles Herz an, ansonsten ein leeres
-            if(i < mouseHealth)
+            if (i < mouseHealth)
             {
                 hearts[i].sprite = fullHeart;
             } else
@@ -36,19 +31,20 @@ public class Health : MonoBehaviour
                 hearts[i].sprite = emptyHeart;
             }
 
-            if(i < numberOfHearts)
+            if (i < numberOfHearts)
             {
                 hearts[i].enabled = true;
-            } else
+            }
+            else
             {
                 hearts[i].enabled = false;
             }
         }
     }
 
-    public void GetDamage(int enemyDamage)
+    public void DealDamage(int enemyDamage)
     {
-        //Bei Ber?hrung mit einem Gegner wird die Gesundheit um 1 verringert
-        mouseHealth-=enemyDamage;
+        //Bei Beruehrung mit einem Gegner wird die Gesundheit um 1 verringert
+        mouseHealth -= enemyDamage;
     }
 }
index e0a59f2994eb6f40a972967287022e68907a6a8d..b09c44fe978be5319d42ca71308a7ca1622d8e6f 100644 (file)
@@ -1,4 +1,5 @@
 //Mit diesem Script kann man die Maus steuern.
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
@@ -42,23 +43,6 @@ public class MouseController : MonoBehaviour
     // Update is called once per frame
     void Update()
     {
-        if (isGrounded == true && Input.GetButtonDown("Jump"))
-        {
-            isJumping = true;
-            jumpTimeCounter = jumptime;
-            rb.velocity = Vector2.up * jumpForce;
-        }
-
-        if (Input.GetButton("Jump") && isJumping == true)
-        {
-            if (jumpTimeCounter > 0)
-            {
-                rb.velocity = Vector2.up * jumpForce;
-                jumpTimeCounter -= Time.deltaTime;
-                FindObjectOfType<AudioManager>().Play("sprung");
-            }
-        }
-
         if (Input.GetButtonUp("Jump"))
         {
             isJumping = false;
@@ -94,13 +78,71 @@ public class MouseController : MonoBehaviour
         moveInput = Input.GetAxisRaw("Horizontal");
     }
 
+    bool HasReachedTV(float acceleration)
+    {
+               if (acceleration < 0)
+               {
+                       if (rb.velocity.x <= -speed)
+                               return true;
+               }
+               else if (acceleration > 0)
+               {
+                       if (rb.velocity.x >= speed)
+                               return true;
+               }
+
+               return false;
+       }
+
     void FixedUpdate()
     {
-        //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
+                //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
 
-        //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
-        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
+               //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
+
+               float acceleration = speed;
+
+               if (moveInput != 0)
+               {
+                       acceleration *= moveInput * 4;
+               }
+               else if (isGrounded)
+               {
+                       acceleration *= -Math.Sign(rb.velocity.x) * 16;
+               }
+               else
+               {
+                       acceleration = 0;
+               }
+
+               if (! HasReachedTV(acceleration))
+               {
+                       int oldSign = Math.Sign(rb.velocity.x);
+                       rb.velocity += new Vector2(acceleration * Time.fixedDeltaTime, 0);
+
+                       if (HasReachedTV(acceleration))
+                               rb.velocity = new Vector2(Math.Sign(rb.velocity.x) * speed, rb.velocity.y);
+                       if (oldSign == -Math.Sign(rb.velocity.x))
+                               rb.velocity = new Vector2(0, rb.velocity.y);
+               }
+
+        if (isGrounded == true && Input.GetButtonDown("Jump"))
+        {
+            isJumping = true;
+            jumpTimeCounter = jumptime;
+            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
+        }
+
+        if (Input.GetButton("Jump") && isJumping == true)
+        {
+            if (jumpTimeCounter > 0)
+            {
+                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
+                jumpTimeCounter -= Time.fixedDeltaTime;
+                FindObjectOfType<AudioManager>().Play("sprung");
+            }
+        }
     }
 
     void ResetShoot()