1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 【RequireComponent(typeof(Controller2D))】
6 public class Player : MonoBehaviour {
7 public float jumpHeight = 55;
8 public float timeToJumpApex = 22f / 60f;
9 public float moveSpeed = 60 2;
10 public float maxFallSpeed = 2402;
11 public GameObject dashShadow,dashPuf,dashBlast,wallJumpSpark;
12 int moveDirX, moveDirY, faceDir = 1;
13 int freezeCount, climbWallDir, dashCount;
14 int jumpDownCount;
15 bool isWallTurn;
16 bool tryToJump,jumpKeyHold,onWall;
17 bool tryToDash, DashKeyHold,onDash;
18 int onPlatform;
19
20 float gravity;
21 float jumpVelocity;
22 Vector3 velocity;
23
24 Controller2D controller;
25 Animator animator;
26 new SpriteRenderer renderer;
27 BoxCollider2D boxCollider;
28
29 // Start is called before the first frame update
30 void Start() {
31 controller = GetComponent();
32 animator = GetComponent();
33 renderer = GetComponent();
34 boxCollider = GetComponent();
35
36 gravity = -(2 jumpHeight / timeToJumpApex / timeToJumpApex);
37 jumpVelocity = -gravity timeToJumpApex;
38 }
39
40 private void Update() {
41 //since these states refresh each "real" frame....
42 GetMoveInput();
43 }
44
45 void GetMoveInput() {
46 if (Input.GetButtonDown("Left")) moveDirX = -1;
47 if (Input.GetButtonDown("Right")) moveDirX = 1;
48 if (Input.GetButtonDown("Up")) moveDirY = 1;
49 if (Input.GetButtonDown("Down")) moveDirY = -1;
50
51 if (!Input.GetButton("Left")) moveDirX = Input.GetButton("Right") ? 1 : 0;
52 if (!Input.GetButton("Right")) moveDirX = Input.GetButton("Left") ? -1 : 0;
53 if (!Input.GetButton("Up")) moveDirY = Input.GetButton("Down") ? -1 : 0;
54 if (!Input.GetButton("Down")) moveDirY = Input.GetButton("Up") ? 1 : 0;
55
56 if (Input.GetButtonDown("Jump")) tryToJump = true;
57 jumpKeyHold = Input.GetButton("Jump");
58
59 if (Input.GetButtonDown("Dash")) tryToDash = true;
60 DashKeyHold = Input.GetButton("Dash");
61 //DashKeyHold = true;
62
63 //moveDirX = 1;
64 if (moveDirX != 0 && freezeCount == 0) faceDir = moveDirX;
65 //print("Running!");
66 }
67
68 private void FixedUpdate() {
69 if (freezeCount == 0) {
70 if (transform.localScale.x != faceDir && (
71 animator.GetCurrentAnimatorClipInfo(0)【0】.clip.name.Equals("wallJumpLoop")
72 || animator.GetCurrentAnimatorClipInfo(0)【0】.clip.name.Equals("wallJumpPre"))) {
73 transform.localScale = new Vector3(faceDir, 1, 1);
74 animator.Play("wallJumpTurn");
75 }
76 else if ((transform.localScale.x != moveDirX) && (
77 animator.GetCurrentAnimatorClipInfo(0)【0】.clip.name.Equals("walldashjumploop")
78 || animator.GetCurrentAnimatorClipInfo(0)【0】.clip.name.Equals("walldashjumppre"))) {
79 //代码效果参考:http://hnjlyzjd.com/hw/wz_24609.html
transform.localScale = new Vector3(faceDir, 1, 1);80 animator.Play("walldashjumpstop");
81 }
82 else transform.localScale = new Vector3(faceDir, 1, 1);
83 }
84
85 if (controller.collisions.below && velocity.y <= 0) {
86 velocity.y = 0;
87 freezeCount = 0;
88 if (onWall) OffWall(0);
89 }
90 if (controller.collisions.above && velocity.y >= 0) {
91 velocity.y = 0;
92 freezeCount = 0;
93 if (onWall) OffWall(0);
94 }
95
96 if (velocity.y > 0 && jumpKeyHold == false) {
97 //release jump key during rising
98 velocity.y = 0;
99 freezeCount = 0;
100 }
101
102 controller.collisions.below |= animator.GetBool("IsGround");
103
104 if (tryToJump) {
105 if (moveDirY == -1) {
106 StartCoroutine(JumpDown());
107 controller.collisions.below = controller.CheckGround();
108 //Off platform
109 }
110 if (controller.collisions.below) {
111 velocity.y = jumpVelocity;
112 animator.SetTrigger("jump");
113 // normal jump
114 }
115 else{
116 int wallDir = controller.CheckWall(faceDir);
117 if (wallDir != 0) {
118 if (onWall) OffWall(isWallTurn ? 2 : 1);
119 faceDir = wallDir;
120 if (DashKeyHold) {
121 OnDash();
122 faceDir = -1;
123 }
124 freezeCount = 7;
125 velocity.x = wallDir moveSpeed -1;
126 velocity.y = jumpVelocity;
127 transform.localScale = new Vector3(faceDir, 1, 1);
128 animator.SetTrigger("Walljump");
129
130 SoundManager.PlayZeroWallJump();
131
132 //generate spark
133 if (controller.CheckFoot(wallDir)) {
134 GameObject instance = Instantiate(wallJumpSpark, transform.position + Vector3.right ((Random.value > 0.5) ? 16 : 12) wallDir + Vector3.down 17, transform.rotation);
135 }
136 }
137 }
138 }
139 tryToJump = false;
140
141 if (tryToDash&& controller.collisions.below) {
142 OnDash();
143 InDashShape();
144 SoundManager.PlayZeroDash();
145 }
146 tryToDash = false;
147
148
149 if (onDash) {
150 dashCount++;
151 if (DashKeyHold) {
152 if (controller.collisions.below) moveDirX = faceDir;
153 else BackShape();
154
155 if (controller.collisions.below) {
156 if (dashCount >= 30) {
157 OffDash();
158 BackShape();
159 }
160 }
161 }
162 else {
163 if (controller.collisions.below) {
164 OffDash();
165 BackShape();
166 }
167 else {
168 BackShape();
169 }
170 }
171 GameObject instance =<span style="color: