-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController2.cs
More file actions
149 lines (110 loc) · 3.96 KB
/
PlayerController2.cs
File metadata and controls
149 lines (110 loc) · 3.96 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2 : MonoBehaviour {
public float moveSpeed;
public float moveSpeedMax;
private float moveSpeedStore;
public float speedMultiplier;
public float speedIncreaseMilestonemultiplier;
public float speedMargin;
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 GameManagaer1 theGameManager;
public AudioSource jumpSound;
public AudioSource deathSound;
// Use this for initialization
void Start () {
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")
{
theGameManager.ResartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreaseMilestone = speedIncreaseMiletoneStore;
deathSound.Play();
}
}
}