1 //Mit diesem Script kann man die Maus steuern.
3 using System.Collections;
4 using System.Collections.Generic;
7 public class MouseController : MonoBehaviour
11 public float jumpForce;
12 private float jumpTimeCounter;
13 public float jumptime;
14 private bool isJumping;
15 private float moveInput;
16 public float coyoteTime;
17 private bool jumpAllowed;
20 public bool isGrounded;
21 public Transform groundcheck;
22 public float checkRadius;
23 public LayerMask whatIsGround;
27 public bool isFacingLeft;
29 private bool isShooting;
34 Transform bulletSpawnPos;
36 private float shootDelay = 0.5f;
38 // Start is called before the first frame update
41 //Hier wird der Rigidbody initialisiert
42 rb = GetComponent<Rigidbody2D>();
44 powerUps = GetComponent<PowerUps>();
47 // Update is called once per frame
52 StartCoroutine(CoyoteTime());
59 if (jumpAllowed && Input.GetButtonDown("Jump"))
62 jumpTimeCounter = jumptime;
63 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
66 if (Input.GetButton("Jump") && isJumping)
68 if (jumpTimeCounter > 0)
70 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
71 jumpTimeCounter -= Time.deltaTime;
72 FindObjectOfType<AudioManager>().Play("sprung");
76 if (Input.GetButtonUp("Jump"))
81 if (rb.velocity.x < 0)
83 transform.localScale = new Vector3(-1, 1, 1);
86 else if (rb.velocity.x > 0)
88 transform.localScale = new Vector3(1, 1, 1);
92 if (powerUps.mouseIsGardener == true)
94 if (Input.GetButtonDown("Fire1") && ! isShooting)
98 GameObject b = Instantiate(bullet);
99 b.GetComponent<Scissors>().StartShoot(isFacingLeft, rb.velocity);
100 b.transform.position = bulletSpawnPos.transform.position;
102 Invoke("ResetShoot", shootDelay);
106 moveInput = Input.GetAxisRaw("Horizontal");
109 bool HasReachedTV(float acceleration)
111 if (acceleration < 0)
113 if (rb.velocity.x <= -speed)
116 else if (acceleration > 0)
118 if (rb.velocity.x >= speed)
127 //Hier wird ein Kreis unter der Maus erzeugt, der prueft, ob die Maus den Boden beruehrt
128 isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
130 //Wenn a und d oder Pfeiltaste links und rechts gedrueckt werden, ist der Wert von moveInput -1 oder 1;
132 float acceleration = speed;
136 acceleration *= moveInput * 4;
140 acceleration *= -Math.Sign(rb.velocity.x) * 16;
147 if (! HasReachedTV(acceleration))
149 int oldSign = Math.Sign(rb.velocity.x);
150 rb.velocity += new Vector2(acceleration * Time.fixedDeltaTime, 0);
152 if (HasReachedTV(acceleration))
153 rb.velocity = new Vector2(Math.Sign(rb.velocity.x) * speed, rb.velocity.y);
154 if (oldSign == -Math.Sign(rb.velocity.x))
155 rb.velocity = new Vector2(0, rb.velocity.y);
159 IEnumerator CoyoteTime()
161 yield return new WaitForSeconds(coyoteTime);