-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
176 lines (139 loc) · 4.38 KB
/
PlayerController.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
// SerializeField: Eleman private olsa bile unity arayüzünde görülebilir.
[SerializeField] private Rigidbody2D playerRb;
[SerializeField] private Animator anim;
[SerializeField] private LayerMask ground;
[SerializeField] private int cherries=0;
[SerializeField] private Text scoreText;
[SerializeField] public float hurtDamage=5f;
[SerializeField] private AudioSource cherrySound;
[SerializeField] private AudioSource stepSound;
private enum State {idle, running, jumping,falling,hurt}
private State state = State.idle;
private Collider2D isGround;
public float moveHorizontal;
private float speed=6.5f;
private float jumpforce=8.5f;
void Start()
{
playerRb= GetComponent<Rigidbody2D>();
anim= GetComponent<Animator>();
isGround = GetComponent<Collider2D>();
scoreText = GameObject.Find("Score").GetComponent<Text>();
}
void Update()
{
//Hareket etmek için gereken fonksion frame başı çağırılıyor.
Movement();
//Animasyonlar için oluşturulan fonksiyon
AnimationState();
//Animasyonun ardından stabil hale dönüş
anim.SetInteger("state", (int)state );
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Collectable")
{
cherrySound.Play();
Destroy(collision.gameObject);
cherries++;
scoreText.text = cherries.ToString();
}
}
// Düşmanla etkileşime girince yaılacaklar
private void OnCollisionEnter2D (Collision2D other)
{
if(other.gameObject.tag=="Enemy")
{
EnemyBasics frog= other.gameObject.GetComponent<EnemyBasics>();
//Sadece yukardan gelirsek yok edilecek düşman
if(state==State.falling)
{
frog.JumpedOn();
Jump();
}
else
{
state = State.hurt;
if(other.gameObject.transform.position.x > transform.position.x)
{
// düşman sağda, hasar alacam ve sola gidecek
playerRb.velocity= new Vector2(-hurtDamage,playerRb.velocity.y);
}
else
{
//düşmaan solda hasar alacam ve sağa gidecek
playerRb.velocity= new Vector2(hurtDamage,playerRb.velocity.y);
}
}
}
}
private void Movement()
{
moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
// Saga hareket
if(moveHorizontal<0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime*moveHorizontal);
transform.localScale = new Vector2 (-1,1); // animasyın icin
}
// Sola hareket
else if(moveHorizontal>0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime*moveHorizontal);
transform.localScale = new Vector2 (1,1); // animasyın icin
}
// Zıplama
if (Input.GetKeyDown(KeyCode.Space) && isGround.IsTouchingLayers(ground) )
{
Jump();
}
}
private void Jump()
{
playerRb.AddForce(new Vector2(0,1f)*jumpforce,ForceMode2D.Impulse);
state= State.jumping;
}
private void AnimationState()
{
if (state == State.jumping)
{
if (playerRb.velocity.y < 0.1f)
{
state = State.falling;
}
}
else if (state ==State.falling)
{
if(isGround.IsTouchingLayers(ground))
{
state = State.idle;
}
}
else if (state == State.hurt)
{
if(Mathf.Abs(playerRb.velocity.x) < 0.1f)
{
state= State.idle;
}
}
else if (Mathf.Abs (moveHorizontal) > 0.2f)
{
state = State.running;
}
else
{
state = State.idle;
}
}
private void FootStep()
{
stepSound.Play(); //ses kaydını çalıyor
}
}