MovementController.cs 4.58 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;
Sebastian Frey's avatar
Sebastian Frey committed
19 20
	[SerializeField] private float m_fallMultiplier = 2.5f;
	[SerializeField] private float m_lowJumpMultiplier = 2.0f;
21

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

Unknown's avatar
Unknown committed
30
	[Header ("Events")]
31 32
	[Space]

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

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

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

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

Unknown's avatar
Unknown committed
45
	private void Update () { }
46

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

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

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

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

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

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

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

88
		// If the player should jump...
Unknown's avatar
Unknown committed
89
		if (jump) {
Unknown's avatar
Merge  
Unknown committed
90
			if (((m_Grounded) || (m_TempJumpCount > 1)) && !m_Jumped) {
Unknown's avatar
Unknown committed
91 92 93
				// Add a vertical force to the player.
				m_Grounded = false;
				m_TempJumpCount--;
Sebastian Frey's avatar
Sebastian Frey committed
94 95
				//m_Rigidbody2D.AddForce (new Vector2 (0f, m_JumpForce));
				m_Rigidbody2D.velocity = Vector2.up * m_JumpForce;
Unknown's avatar
Merge  
Unknown committed
96
				StartCoroutine(Wait(m_SecondsWaitToJump));
Unknown's avatar
Unknown committed
97
			}
98
		}
Sebastian Frey's avatar
Sebastian Frey committed
99 100 101 102 103 104 105 106

		if (m_Rigidbody2D.velocity.y < 0)
		{
			m_Rigidbody2D.velocity += Vector2.up * Physics2D.gravity.y * (m_fallMultiplier - 1) * Time.deltaTime;
		} else if (m_Rigidbody2D.velocity.y > 0 && !Input.GetButton("Jump"))
		{
			m_Rigidbody2D.velocity += Vector2.up * Physics2D.gravity.y * (m_lowJumpMultiplier - 1) * Time.deltaTime;
		}
Unknown's avatar
Merge  
Unknown committed
107
	}
108

Unknown's avatar
Merge  
Unknown committed
109 110 111 112 113
	private IEnumerator Wait(float s)
	{
		m_Jumped = true;
		yield return new WaitForSeconds(s);
		m_Jumped = false;
Unknown's avatar
Unknown committed
114
	}
115

Unknown's avatar
Unknown committed
116
	private void Flip () {
117 118 119 120 121 122 123 124 125
		// 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
126
	void OnDrawGizmos () {
127
		Gizmos.color = Color.green;
128
		Gizmos.DrawWireSphere (m_GroundCheck.position + new Vector3(m_GroundedOffset.x, m_GroundedOffset.y, 0), m_GroundedRadius);
Unknown's avatar
Unknown committed
129
	}
130

mthiele2's avatar
mthiele2 committed
131
}