MovementController.cs 3.66 KB
Newer Older
1
2
3
4
5
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

Unknown's avatar
Unknown committed
6
[RequireComponent(typeof(Rigidbody2D), typeof(BoxCollider2D))]
7
8
public class MovementController : MonoBehaviour {

Unknown's avatar
Unknown committed
9
10
11
12
13
14
15
	[SerializeField] private float m_Speed = 10f;
	[SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
	[Range (0, .3f)][SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
	[SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
	[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
	[SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
	[SerializeField] private int m_MaxJumpCount = 1;
16
17

	const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
Unknown's avatar
Unknown committed
18
19
	private bool m_Grounded; // Whether or not the player is grounded.
	private bool m_Airborn;
20
	private Rigidbody2D m_Rigidbody2D;
Unknown's avatar
Unknown committed
21
	private bool m_FacingRight = true; // For determining which way the player is currently facing.
22
	private Vector3 m_Velocity = Vector3.zero;
Unknown's avatar
Unknown committed
23
	private int m_TempJumpCount = 0;
24

Unknown's avatar
Unknown committed
25
	[Header ("Events")]
26
27
	[Space]

Unknown's avatar
Unknown committed
28
	//Wird Aktiviert wenn Spieler Landet
29
30
	public UnityEvent OnLandEvent;

Unknown's avatar
Unknown committed
31
	public float GravityModifier { set => m_Rigidbody2D.gravityScale = value; }
32

Unknown's avatar
Unknown committed
33
34
	private void Awake () {
		m_Rigidbody2D = GetComponent<Rigidbody2D> ();
35
36

		if (OnLandEvent == null)
Unknown's avatar
Unknown committed
37
			OnLandEvent = new UnityEvent ();
38
39
	}

Unknown's avatar
Unknown committed
40
	private void Update () { }
41

Unknown's avatar
Unknown committed
42
	private void FixedUpdate () {
43
44
45
		bool wasGrounded = m_Grounded;
		m_Grounded = false;

Unknown's avatar
Unknown committed
46
		// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
47
		// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Unknown's avatar
Unknown committed
48
49
50
		Collider2D[] colliders = Physics2D.OverlapCircleAll (m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
		for (int i = 0; i < colliders.Length; i++) {
			if (colliders[i].gameObject != gameObject) {
51
				m_Grounded = true;
Unknown's avatar
Unknown committed
52
53
54
				m_TempJumpCount = m_MaxJumpCount;
				if (!wasGrounded)
					OnLandEvent.Invoke ();
55
56
57
58
			}
		}
	}

Unknown's avatar
Unknown committed
59
	public void Move (Vector2 move, bool jump) {
60
61

		//only control the player if grounded or airControl is turned on
Unknown's avatar
Unknown committed
62
		if (m_Grounded || m_AirControl) {
63
64

			// Move the character by finding the target velocity
Unknown's avatar
Unknown committed
65
66
			Vector2 targetVelocity = new Vector2 ((move.x * m_Speed * Time.deltaTime) * 10f, m_Rigidbody2D.velocity.y);

67
			// And then smoothing it out and applying it to the character
Unknown's avatar
Unknown committed
68
			m_Rigidbody2D.velocity = Vector3.SmoothDamp (m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
69
70

			// If the input is moving the player right and the player is facing left...
Unknown's avatar
Unknown committed
71
			if (move.x > 0 && !m_FacingRight) {
72
				// ... flip the player.
Unknown's avatar
Unknown committed
73
				Flip ();
74
75
			}
			// Otherwise if the input is moving the player left and the player is facing right...
Unknown's avatar
Unknown committed
76
			else if (move.x < 0 && m_FacingRight) {
77
				// ... flip the player.
Unknown's avatar
Unknown committed
78
				Flip ();
79
80
			}
		}
Unknown's avatar
Unknown committed
81

82
		// If the player should jump...
Unknown's avatar
Unknown committed
83
84
85
86
87
88
89
		if (jump) {
			if ((m_Grounded) || (m_TempJumpCount > 1)) {
				// Add a vertical force to the player.
				m_Grounded = false;
				m_TempJumpCount--;
				m_Rigidbody2D.AddForce (new Vector2 (0f, m_JumpForce));
			}
90
91
		}

Unknown's avatar
Unknown committed
92
	}
93

Unknown's avatar
Unknown committed
94
	private void Flip () {
95
96
97
98
99
100
101
102
103
		// Switch the way the player is labelled as facing.
		m_FacingRight = !m_FacingRight;

		// Multiply the player's x local scale by -1.
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}

Unknown's avatar
Unknown committed
104
	void OnDrawGizmos () {
105
		Gizmos.color = Color.green;
Unknown's avatar
Unknown committed
106
107
		Gizmos.DrawWireSphere (m_GroundCheck.position, k_GroundedRadius);
	}
108
109

}