-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController3.cs
More file actions
171 lines (130 loc) · 4.71 KB
/
PlayerController3.cs
File metadata and controls
171 lines (130 loc) · 4.71 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController3 : MonoBehaviour {
public float moveSpeed;
public float moveSpeedMax;
private float moveSpeedStore;
public float speedMultiplier;
public float life;
public float speedIncreaseMilestonemultiplier;
public float speedMargin;
public Transform platformGenerator;
private Vector3 platformStartPoint;
private Vector3 playerStartPoint;
private platformDeleter[] platformList;
public float speedIncreaseMilestone;
public float speedIncreaseMiletoneStore;
private float speedMilestoneCount;
private float speedMilestoneCountStore;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
private bool stopedJumping;
private bool canDoubleJump;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
public Transform groundCheck;
public float groundCheckRadius;
private Collider2D myCollider;
private Animator myAnimator;
public GameManager3 theGameManager;
public AudioSource jumpSound;
public AudioSource deathSound;
// Use this for initialization
void Start () {
playerStartPoint = this.transform.position;
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
jumpTimeCounter=jumpTime;
speedMilestoneCount = speedIncreaseMilestone;
moveSpeedStore = moveSpeed;
speedMilestoneCountStore = speedMilestoneCount;
speedIncreaseMiletoneStore = speedIncreaseMilestone;
stopedJumping = true;
}
// Update is called once per frame
void Update () {
// grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if(transform.position.x > speedMilestoneCount)
{
speedMilestoneCount += speedIncreaseMilestone;
speedIncreaseMilestone = speedIncreaseMilestone * speedIncreaseMilestonemultiplier;
moveSpeed = moveSpeed * speedMultiplier;
}
if (speedMargin <= moveSpeed)
{
speedMultiplier = 1;
}
if (moveSpeedMax <= moveSpeed)
{
moveSpeed = moveSpeedMax;
}
myRigidbody.velocity = new Vector2(moveSpeed,myRigidbody.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stopedJumping = false;
jumpSound.Play();
}
if (!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stopedJumping = false;
canDoubleJump = false;
jumpSound.Play();
}
}
if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stopedJumping)
{
if (jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if(Input.GetKeyUp(KeyCode.Space)|| Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stopedJumping = true;
}
if (grounded)
{
jumpTimeCounter = jumpTime;
canDoubleJump = true;
}
myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
myAnimator.SetBool("Ground",grounded);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "KillBox")
{
if (life == 0)
{
theGameManager.ResartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreaseMilestone = speedIncreaseMiletoneStore;
deathSound.Play();
}
if (life > 0)
{
life = life - 1;
this.transform.position = playerStartPoint;
platformGenerator.position = platformStartPoint;
platformList = FindObjectsOfType<platformDeleter>();
for (int i = 0; i < platformList.Length; i++)
{
Destroy(platformList[i].gameObject);
}
}
}
}
}