]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Maus/MouseController.cs
Man kann von Gegnern abspringen
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Maus / MouseController.cs
1 //Mit diesem Script kann man die Maus steuern.
2 using System.Collections;
3 using System.Collections.Generic;
4 using UnityEngine;
5 public class MouseController : MonoBehaviour
6 {
7     Rigidbody2D rb;
8     public float speed;
9     public float jumpForce;
10     private float jumpTimeCounter;
11     public float jumptime;
12     private bool isJumping;
13     private float moveInput;
14
15     private bool isGrounded;
16     public Transform groundcheck;
17     public float checkRadius;
18     public LayerMask whatIsGround;
19
20     PowerUps powerUps;
21
22     public bool isFacingLeft;
23
24     private bool isShooting;
25
26     [SerializeField]
27     GameObject bullet;
28     [SerializeField]
29     Transform bulletSpawnPos;
30     [SerializeField]
31     private float shootDelay = 0.5f;
32
33     // Start is called before the first frame update
34     void Start()
35     {
36         //Hier wird der Rigidbody initialisiert
37         rb = GetComponent<Rigidbody2D>();
38
39         powerUps = GetComponent<PowerUps>();
40     }
41
42     // Update is called once per frame
43     void Update()
44     {
45         if (isGrounded == true && Input.GetButtonDown("Jump"))
46         {
47             isJumping = true;
48             jumpTimeCounter = jumptime;
49             rb.velocity = Vector2.up * jumpForce;
50         }
51
52         if (Input.GetButton("Jump") && isJumping == true)
53         {
54             if (jumpTimeCounter > 0)
55             {
56                 rb.velocity = Vector2.up * jumpForce;
57                 jumpTimeCounter -= Time.deltaTime;
58                 FindObjectOfType<AudioManager>().Play("sprung");
59             }
60         }
61
62         if (Input.GetButtonUp("Jump"))
63         {
64             isJumping = false;
65         }
66
67         if (rb.velocity.x < 0)
68         {
69             transform.localScale = new Vector3(-1, 1, 1);
70             isFacingLeft = true;
71         }
72         else if (rb.velocity.x > 0)
73         {
74             transform.localScale = new Vector3(1, 1, 1);
75             isFacingLeft = false;
76         }
77
78         if (powerUps.mouseIsGardener == true)
79         {
80             if (Input.GetButtonDown("Fire1"))
81             {
82                 if (isShooting) return;
83
84                 isShooting = true;
85
86                 GameObject b = Instantiate(bullet);
87                 b.GetComponent<Scissors>().StartShoot(isFacingLeft);
88                 b.transform.position = bulletSpawnPos.transform.position;
89
90                 Invoke("ResetShoot", shootDelay);
91             }
92         }
93
94         moveInput = Input.GetAxisRaw("Horizontal");
95     }
96
97     void FixedUpdate()
98     {
99         //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
100         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
101
102         //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
103         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
104     }
105
106     void ResetShoot()
107     {
108         isShooting = false;
109     }
110 }