CollisionState.cs 2.87 KB
Newer Older
1
using UnityEngine;
2 3 4 5
using System.Collections;

public class CollisionState : MonoBehaviour {

6 7 8 9 10 11 12
	public LayerMask collisionLayer;
	public LayerMask enemyLayer;
	public bool onGround, onWall, onWallRight, onWallLeft;
	public Vector2 bottomPosition = Vector2.zero;
	public Vector2 rightPosition = Vector2.zero;
	public Vector2 leftPosition = Vector2.zero;
	public float collisionRadius = 1f;
13

14
	[HideInInspector]public Collider2D enemyHit;
15

16
	private InputState inputState;
17
	private Rigidbody2D body;
18
	private Animator animator;
19

20 21
	void Awake() {
		inputState = GetComponent<InputState>();
22
		body = GetComponent<Rigidbody2D>();
23 24 25 26 27 28
		animator = GetComponentInChildren<Animator>();
	}

	void Update() {
		animator.SetBool("on ground", onGround);
		animator.SetBool("on wall", onWall);
29
	}
30

31
	void FixedUpdate() {
32

Tanner Grehawick's avatar
Tanner Grehawick committed
33

34
		enemyHit = null;
Tanner Grehawick's avatar
Tanner Grehawick committed
35
		// onGround = Physics2D.OverlapCircle(body.position + bottomPosition, collisionRadius, collisionLayer);
36

Tanner Grehawick's avatar
Tanner Grehawick committed
37 38 39
		// onWallLeft = Physics2D.OverlapCircle(body.position + leftPosition, collisionRadius, collisionLayer);
		// onWallRight = Physics2D.OverlapCircle(body.position + rightPosition, collisionRadius, collisionLayer);
		// onWall = onWallRight || onWallLeft;
40
		enemyHit = Physics2D.OverlapCircle(body.position + bottomPosition, collisionRadius, enemyLayer);
41
	}
42

43 44
	// void OnDrawGizmosSelected() {
	// 	Gizmos.color = Color.red;
45

46 47 48
	// 	Vector2 pos = bottomPosition;
	// 	pos.x += transform.position.x;
	// 	pos.y += transform.position.y;
49

50
	// 	Gizmos.DrawWireSphere(pos, collisionRadius);
51

52 53 54
	// 	pos = rightPosition;
	// 	pos.x += transform.position.x;
	// 	pos.y += transform.position.y;
55

56
	// 	Gizmos.DrawWireSphere(pos, collisionRadius);
57

58 59 60
	// 	pos = leftPosition;
	// 	pos.x += transform.position.x;
	// 	pos.y += transform.position.y;
61

62 63 64 65
	// 	Gizmos.DrawWireSphere(pos, collisionRadius);
	// }

	float lastFixedTime;
Tanner Grehawick's avatar
Tanner Grehawick committed
66 67

	void OnCollisionStay2D(Collision2D collision) {
68
		if (Time.fixedTime != lastFixedTime) {
69 70
			// only reset flags at the start of each frame
			// (OnCollisionStay2D is called once for every collider this body is in contact with)
71 72 73 74 75
			onGround = false;
			onWallLeft = false;
			onWallRight = false;
			lastFixedTime = Time.fixedTime;
		}
Tanner Grehawick's avatar
Tanner Grehawick committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		foreach (ContactPoint2D contact in collision.contacts) {
			Color color = Color.white;
			Vector2 normal = contact.normal;
			if (normal == Vector2.right) {
				color = Color.red;
				onWallLeft = true;
			}
			if (normal == -Vector2.right) {
				color = Color.green;
				onWallRight = true;
			}
			if (normal == Vector2.up) {
				color = Color.blue;
				onGround = true;
			}
			onWall = onWallLeft || onWallRight;
			Debug.DrawRay(contact.point, contact.normal, color);
		}
	}

	void OnCollisionExit2D() {
97
		// reset flags when we arent colliding with anything at all
Tanner Grehawick's avatar
Tanner Grehawick committed
98 99 100 101
		onGround = false;
		onWallLeft = false;
		onWallRight = false;
		onWall = false;
102
	}
103
}