MovementController.cs 4.08 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
Merge    
Unknown committed
17
	[SerializeField] private float m_SecondsWaitToJump = 0.3f;
Unknown's avatar
Unknown committed
18
	[SerializeField] private int m_MaxJumpCount = 1;
19

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

Unknown's avatar
Unknown committed
28
	[Header ("Events")]
29
30
	[Space]

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

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

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

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

Unknown's avatar
Unknown committed
43
	private void Update () { }
44

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

Unknown's avatar
Unknown committed
49
		// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
50
		// This can be done using layers instead but Sample Assets will not overwrite your project settings.
51
52
		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
53
54
		for (int i = 0; i < colliders.Length; i++) {
			if (colliders[i].gameObject != gameObject) {
55
				m_Grounded = true;
Unknown's avatar
Unknown committed
56
57
58
				m_TempJumpCount = m_MaxJumpCount;
				if (!wasGrounded)
					OnLandEvent.Invoke ();
59
60
61
62
			}
		}
	}

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

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

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

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

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

86
		// If the player should jump...
Unknown's avatar
Unknown committed
87
		if (jump) {
Unknown's avatar
Merge    
Unknown committed
88
			if (((m_Grounded) || (m_TempJumpCount > 1)) && !m_Jumped) {
Unknown's avatar
Unknown committed
89
90
91
92
				// Add a vertical force to the player.
				m_Grounded = false;
				m_TempJumpCount--;
				m_Rigidbody2D.AddForce (new Vector2 (0f, m_JumpForce));
Unknown's avatar
Merge    
Unknown committed
93
				StartCoroutine(Wait(m_SecondsWaitToJump));
Unknown's avatar
Unknown committed
94
			}
95
		}
Unknown's avatar
Merge    
Unknown committed
96
	}
97

Unknown's avatar
Merge    
Unknown committed
98
99
100
101
102
	private IEnumerator Wait(float s)
	{
		m_Jumped = true;
		yield return new WaitForSeconds(s);
		m_Jumped = false;
Unknown's avatar
Unknown committed
103
	}
104

Unknown's avatar
Unknown committed
105
	private void Flip () {
106
107
108
109
110
111
112
113
114
		// 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
115
	void OnDrawGizmos () {
116
		Gizmos.color = Color.green;
117
		Gizmos.DrawWireSphere (m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y, 0), m_GroundedRadius);
Unknown's avatar
Unknown committed
118
	}
119

mthiele2's avatar
mthiele2 committed
120
}