MovementController.cs 3.84 KB
Newer Older
mthiele2's avatar
mthiele2 committed
1
using System.Collections;
2
3
4
5
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
	[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.
15
16
	[SerializeField] private float m_GroundedRadius = .2f;
	[SerializeField] private Vector2 m_GroundedOffset = Vector2.zero;
Unknown's avatar
Unknown committed
17
	[SerializeField] private int m_MaxJumpCount = 1;
18

Unknown's avatar
Unknown committed
19
20
	private bool m_Grounded; // Whether or not the player is grounded.
	private bool m_Airborn;
21
	private Rigidbody2D m_Rigidbody2D;
Unknown's avatar
Unknown committed
22
	private bool m_FacingRight = true; // For determining which way the player is currently facing.
23
	private Vector3 m_Velocity = Vector3.zero;
Unknown's avatar
Unknown committed
24
	private int m_TempJumpCount = 0;
25

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

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

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

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

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

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

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

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

Unknown's avatar
Unknown committed
62
	public void Move (Vector2 move, bool jump) {
63
64

		//only control the player if grounded or airControl is turned on
Unknown's avatar
Unknown committed
65
		if (m_Grounded || m_AirControl) {
66
67

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

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

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

85
		// If the player should jump...
Unknown's avatar
Unknown committed
86
87
88
89
90
91
92
		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));
			}
93
94
		}

Unknown's avatar
Unknown committed
95
	}
96

Unknown's avatar
Unknown committed
97
	private void Flip () {
98
99
100
101
102
103
104
105
106
		// 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
107
	void OnDrawGizmos () {
108
		Gizmos.color = Color.green;
109
		Gizmos.DrawWireSphere (m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y, 0), m_GroundedRadius);
Unknown's avatar
Unknown committed
110
	}
111

mthiele2's avatar
mthiele2 committed
112
}