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 float moveInput;
16 public float coyoteTime;
17 private float coyoteTimeCounter;
19 public float jumpBufferLength;
20 private float jumpBufferCounter;
22 [HideInInspector] public bool isGrounded;
23 public Transform groundcheck;
24 public float checkRadius;
25 public LayerMask whatIsGround;
29 [HideInInspector] public bool isFacingLeft;
31 private bool isShooting;
33 [SerializeField] GameObject bullet;
35 [SerializeField] Transform bulletSpawnPos;
37 [SerializeField] private float shootDelay = 0.5f;
41 //Hier wird der Rigidbody initialisiert
42 rb = GetComponent<Rigidbody2D>();
44 powerUps = GetComponent<PowerUps>();
52 coyoteTimeCounter = coyoteTime;
55 coyoteTimeCounter -= Time.deltaTime;
58 //manage jump buffering
59 if (Input.GetButtonDown("Jump"))
61 jumpBufferCounter = jumpBufferLength;
64 jumpBufferCounter -= Time.deltaTime;
68 if (jumpBufferCounter >= 0 && coyoteTimeCounter > 0)
71 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
72 jumpBufferCounter = 0;
73 coyoteTimeCounter = 0;
76 //jumping with different height
77 if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
79 rb.velocity = new Vector2(rb.velocity.x, jumpForce * 0.5f);
82 if (rb.velocity.x < 0)
84 transform.localScale = new Vector3(-1, 1, 1);
87 else if (rb.velocity.x > 0)
89 transform.localScale = new Vector3(1, 1, 1);
93 if (powerUps.mouseIsGardener == true)
95 if (Input.GetButtonDown("Fire1") && ! isShooting)
99 GameObject b = Instantiate(bullet);
100 b.GetComponent<Scissors>().StartShoot(isFacingLeft, rb.velocity);
101 b.transform.position = bulletSpawnPos.transform.position;
103 Invoke("ResetShoot", shootDelay);
107 moveInput = Input.GetAxisRaw("Horizontal");
110 bool HasReachedTV(float acceleration)
112 if (acceleration < 0)
114 if (rb.velocity.x <= -speed)
117 else if (acceleration > 0)
119 if (rb.velocity.x >= speed)
128 //Hier wird ein Kreis unter der Maus erzeugt, der prueft, ob die Maus den Boden beruehrt
129 isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
131 //Wenn a und d oder Pfeiltaste links und rechts gedrueckt werden, ist der Wert von moveInput -1 oder 1;
133 float acceleration = speed;
137 acceleration *= moveInput * 3;
141 acceleration *= -Math.Sign(rb.velocity.x) * 16;
148 if (! HasReachedTV(acceleration))
150 int oldSign = Math.Sign(rb.velocity.x);
151 rb.velocity += new Vector2(acceleration, 0);
153 if (HasReachedTV(acceleration))
154 rb.velocity = new Vector2(Math.Sign(rb.velocity.x) * speed, rb.velocity.y);
155 if (oldSign == -Math.Sign(rb.velocity.x))
156 rb.velocity = new Vector2(0, rb.velocity.y);