MovementController.cs 5.35 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))]
Unknown's avatar
Unknown committed
7
8
public class MovementController : MonoBehaviour
{
Unknown's avatar
Unknown committed
9
10
	[SerializeField] private float m_Speed = 10f;
	[SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
Unknown's avatar
Unknown committed
11
	[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
Unknown's avatar
Unknown committed
12
13
14
	[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.
Unknown's avatar
Unknown committed
15
	[SerializeField] private float m_GroundCheckDistance = 1.5f;
16
17
	[SerializeField] private float m_GroundedRadius = .2f;
	[SerializeField] private Vector2 m_GroundedOffset = Vector2.zero;
Unknown's avatar
Unknown committed
18
	[SerializeField] private float m_GroundMaxSlopeAngle = 45f;
Unknown's avatar
Merge    
Unknown committed
19
	[SerializeField] private float m_SecondsWaitToJump = 0.3f;
Unknown's avatar
Unknown committed
20
	[SerializeField] private int m_MaxJumpCount = 1;
21

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

Unknown's avatar
Unknown committed
31
32
33
34
35
	private RaycastHit2D[] m_RaycastHits2D = new RaycastHit2D[16];
	private ContactFilter2D m_ContactFilter2D;
	private Vector3 m_CurrentGroundNormal;

	[Header("Events")] [Space]
36

Unknown's avatar
Unknown committed
37
	//Wird Aktiviert wenn Spieler Landet
38
39
	public UnityEvent OnLandEvent;

Unknown's avatar
Unknown committed
40
41
42
43
	public float GravityModifier
	{
		set => m_Rigidbody2D.gravityScale = value;
	}
44

Unknown's avatar
Unknown committed
45
46
47
48
49
50
51
52
53
54
55
	public void SetSwimming(bool value)
	{
		m_Swimming = value;
		m_TempJumpCount = m_MaxJumpCount;
	}

	private void Awake()
	{
		m_Rigidbody2D = GetComponent<Rigidbody2D>();

		m_ContactFilter2D.SetLayerMask(m_WhatIsGround);
56
57

		if (OnLandEvent == null)
Unknown's avatar
Unknown committed
58
			OnLandEvent = new UnityEvent();
59
60
	}

Unknown's avatar
Unknown committed
61
62
63
	private void Update()
	{
	}
64

Unknown's avatar
Unknown committed
65
66
	private void FixedUpdate()
	{
67
68
69
		bool wasGrounded = m_Grounded;
		m_Grounded = false;

Unknown's avatar
Unknown committed
70
		// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
71
		// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Unknown's avatar
Unknown committed
72
		/*Vector3 pos = m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y);
73
		Collider2D[] colliders = Physics2D.OverlapCircleAll (pos, m_GroundedRadius, m_WhatIsGround);
Unknown's avatar
Unknown committed
74
75
		for (int i = 0; i < colliders.Length; i++) {
			if (colliders[i].gameObject != gameObject) {
76
				m_Grounded = true;
Unknown's avatar
Unknown committed
77
78
79
				m_TempJumpCount = m_MaxJumpCount;
				if (!wasGrounded)
					OnLandEvent.Invoke ();
80
			}
Unknown's avatar
Unknown committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		}*/
		Vector3 pos = m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y, 0);
		var c = Physics2D.CircleCast(pos, m_GroundedRadius, Vector2.down, m_ContactFilter2D, m_RaycastHits2D,
			m_GroundCheckDistance);
		for (var i = 0; i < c; i++)
		{
			if (m_RaycastHits2D[i].transform.gameObject == gameObject) continue;

			m_Grounded = true;
			m_TempJumpCount = m_MaxJumpCount;
			m_CurrentGroundNormal = m_RaycastHits2D[i].normal;
			if (!wasGrounded)
			{
				OnLandEvent.Invoke();
			}
96
97
98
		}
	}

Unknown's avatar
Unknown committed
99
100
101
	public void Move(Vector2 move, bool jump)
	{
		Debug.Log(m_Swimming);
102
		//only control the player if grounded or airControl is turned on
Unknown's avatar
Unknown committed
103
104
		if (m_Grounded || m_AirControl || m_Swimming)
		{
105
			// Move the character by finding the target velocity
Unknown's avatar
Unknown committed
106
			Vector2 targetVelocity = new Vector2((move.x * m_Speed * Time.deltaTime) * 10f, m_Rigidbody2D.velocity.y);
Unknown's avatar
Unknown committed
107

108
			// And then smoothing it out and applying it to the character
Unknown's avatar
Unknown committed
109
110
			m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity,
				m_MovementSmoothing);
111
112

			// If the input is moving the player right and the player is facing left...
Unknown's avatar
Unknown committed
113
114
			if (move.x > 0 && !m_FacingRight)
			{
115
				// ... flip the player.
Unknown's avatar
Unknown committed
116
				Flip();
117
118
			}
			// Otherwise if the input is moving the player left and the player is facing right...
Unknown's avatar
Unknown committed
119
120
			else if (move.x < 0 && m_FacingRight)
			{
121
				// ... flip the player.
Unknown's avatar
Unknown committed
122
				Flip();
123
124
			}
		}
Unknown's avatar
Unknown committed
125

126
		// If the player should jump...
Unknown's avatar
Unknown committed
127
128
129
130
		if (jump)
		{
			if (((m_Grounded) || (m_TempJumpCount > 1) || m_Swimming) && !m_Jumped)
			{
Unknown's avatar
Unknown committed
131
132
133
				// Add a vertical force to the player.
				m_Grounded = false;
				m_TempJumpCount--;
Unknown's avatar
Unknown committed
134
				m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
Unknown's avatar
Merge    
Unknown committed
135
				StartCoroutine(Wait(m_SecondsWaitToJump));
Unknown's avatar
Unknown committed
136
			}
137
		}
Unknown's avatar
Merge    
Unknown committed
138
	}
139

Unknown's avatar
Merge    
Unknown committed
140
141
142
143
144
	private IEnumerator Wait(float s)
	{
		m_Jumped = true;
		yield return new WaitForSeconds(s);
		m_Jumped = false;
Unknown's avatar
Unknown committed
145
	}
146

Unknown's avatar
Unknown committed
147
148
	private void Flip()
	{
149
150
151
152
153
154
155
156
157
		// 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
158
159
160

	void OnDrawGizmos()
	{
161
		Gizmos.color = Color.green;
Unknown's avatar
Unknown committed
162
163
164
165
		Vector3 pos = m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y, 0);
		//Gizmos.DrawWireSphere (m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y, 0), m_GroundedRadius);
		Gizmos.DrawRay(pos, Vector3.down * m_GroundCheckDistance);
		Gizmos.DrawWireSphere(pos + Vector3.down * m_GroundCheckDistance, m_GroundedRadius);
Unknown's avatar
Unknown committed
166
	}
mthiele2's avatar
mthiele2 committed
167
}
Unknown's avatar
Unknown committed
168