-
Notifications
You must be signed in to change notification settings - Fork 0
/
BirdBehaviour.cs
102 lines (91 loc) · 2.44 KB
/
BirdBehaviour.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BirdBehaviour : MonoBehaviour
{
// gravity bring are ugly bird down
public float gravity;
public float clickImpluse;
public Sprite flapUpSprite, flapDownSprite, flapMidSprite;
// track the speed of moving up and down
float ySpeed;
//variables to track our position
float posX, posY, posZ;
int animCount = 0;
int score = 0;
public AudioSource flapSound;
public AudioSource hitSound;
public AudioSource scoreSound;
public Text scoreText;
void Start()
{
// get our position and put into variables
posX = transform.position.x;
posY = transform.position.y;
posZ = transform.position.z;
// yspeed to zero
ySpeed = 0.0f;
}
void Update()
{
if(Input.GetMouseButtonUp(0))
{
flap(); // animation
}
animCount = animCount + 1;
if(ySpeed <= 0)
{
animCount = 2;
}
if (animCount == 4)
{
animCount = 0;
}
if (animCount == 1)
{
GetComponent<SpriteRenderer>().sprite = flapUpSprite;
}
else if (animCount == 3)
{
GetComponent<SpriteRenderer>().sprite = flapDownSprite;
}
else
{
GetComponent<SpriteRenderer>().sprite = flapMidSprite;
}
float angle = 9.0f * ySpeed;
if (angle > 30.0f)
angle = 30.0f;
if (angle < -30.0f)
angle = -30.0f;
transform.rotation = Quaternion.Euler(0, 0, angle);
scoreText.text = score.ToString();
}
void FixedUpdate()
{
ySpeed -= gravity * Time.fixedDeltaTime;
posY += ySpeed * Time.fixedDeltaTime;
transform.localPosition = new Vector3(posX, posY, posZ);
}
void flap()
{
ySpeed = clickImpluse;
flapSound.Play();
}
void OnTriggerEnter2D(Collider2D coll)
{
if(coll.gameObject.tag == "score")
{
scoreSound.Play();
score += 1;
}
else
{
transform.parent.GetComponent<SideScroller>().xSpeed = 0;
transform.parent.Find("gameover").gameObject.SetActive(true);
hitSound.Play();
this.gameObject.SetActive(false);
}
}
}