-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroundDetect.cs
73 lines (62 loc) · 1.66 KB
/
GroundDetect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundDetect : MonoBehaviour {
private PlayerController controller;
private int playerControllerNumber;
public int normal;
public int dropThroughFloor;
public MyAudio.AUDIOCLIPS landAudio;
public float minSpeedForSoundEffect;
private float previousYValue;
void Awake()
{
controller = transform.parent.GetComponent<PlayerController>();
}
void Start()
{
playerControllerNumber = controller.playerControlNumber;
}
public void OnTriggerStay2D(Collider2D other)
{
if(other.tag == Tags.floor)
{
controller.Land();
}
}
public void OnTriggerExit2D(Collider2D other)
{
if(other.tag == Tags.floor)
{
controller.LeftGround();
}
}
void Update()
{
float y = MyInputs.GetAxisRaw(playerControllerNumber, Tags.vertical);
if(!falling && y < -0.85 && previousYValue>-0.75)
{
StartCoroutine(FallThroughFloor());
}
if(MyInputs.GetButtonDown(playerControllerNumber, Tags.jump))
{
}
previousYValue = y;
}
bool falling;
IEnumerator FallThroughFloor()
{
falling = true;
gameObject.layer = dropThroughFloor;
yield return new WaitForSeconds(0.2f);
gameObject.layer = normal;
falling = false;
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.relativeVelocity.magnitude > minSpeedForSoundEffect)
{
MyAudio.current.PlayClip(landAudio);
}
}
}