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 PlayerController : MonoBehaviour
{
Rigidbody2D rbody;//Rigidbody2D类型的变量
float axisH = 0.0f;//输入
public float speed = 3.0f;//移动速度

public float jump = 9.0f;//跳跃力
public LayerMask groundLayer;//可以落脚的层
bool goJump = false;//跳跃开始的旗标
bool onGround = false;//立于地面的旗标
Animator animator;//画师
public string stopAnime = "PlayerStop";
public string moveAnime = "PlayerMove";
public string jumpAnime = "PlayerJump";
public string goalAnime = "PlayerGoal";
public string deadAnime = "PlayerDead";
string nowAnime = "";
string oldAnime = "";

public static string gameState = "playing";//游戏的状态

// Start is called before the first frame update
void Start()
{
//取得Rugidbody2D
rbody = this.GetComponent<Rigidbody2D>();
gameState = "playing";//设为游戏中
//取得Animator
animator = GetComponent<Animator>();
nowAnime = stopAnime;
oldAnime = stopAnime;
}

// Update is called once per frame
void Update()
{
if (gameState != "playing") return;
//检查水平方向的输入
axisH = Input.GetAxisRaw("Horizontal");
//方向调整
if (axisH > 0.0f)
{
//向右移动
Debug.Log("向右移动");
transform.localScale = new Vector2(1, 1);
}
else if (axisH < 0.0f) {
//向左移动
Debug.Log("向左移动");
transform.localScale = new Vector2(-1, 1);//左右翻转
}
if (Input.GetButtonDown("Jump"))
{
Jump();//跳跃
}

}

void FixedUpdate()
{
if (gameState != "playing") return;
//地面判定
bool onGround = Physics2D.Linecast(transform.position, transform.position - (transform.up * 0.1f), groundLayer);
if (onGround || axisH != 0)
{
//在地面上或速度不为0
//更新速度
rbody.velocity = new Vector2(axisH * speed, rbody.velocity.y);
}
if (goJump)//if(onGround &&goJump)
{
//在地面上按下了跳跃键
//跳起来
Debug.Log("跳跃!");
Vector2 jumpPw = new Vector2(0, jump);//新建用于跳跃的向量
rbody.AddForce(jumpPw, ForceMode2D.Impulse);//施加瞬间的力
goJump = false;//取下跳跃旗标
}
if (axisH != 0 || axisH == 0)//if(onGround)
{
//在地面上
if (axisH == 0)
{
nowAnime = stopAnime;//停止
}
else
{
nowAnime = moveAnime;
}
}
else
{
//空中
nowAnime = jumpAnime;
}
if (nowAnime != oldAnime)
{
oldAnime = nowAnime;
animator.Play(nowAnime);//播放动画效果
}
}


//跳跃
public void Jump()
{
goJump = true;
Debug.Log("按下了跳跃键!");
}

//接触开始
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Goal"){
Goal();
}else if (collision.gameObject.tag == "Dead")
{
GameOver();
}
}
//到达终点
public void Goal()
{
animator.Play(goalAnime);
gameState = "gameclear";
GameStop();//游戏停止
}
//游戏失败
public void GameOver()
{
animator.Play(deadAnime);
gameState = "gameover";
GameStop();
GetComponent<CapsuleCollider2D>().enabled=false;
rbody.AddForce(new Vector2(0,5),ForceMode2D.Impulse);
}

//游戏停止
void GameStop()
{
Rigidbody2D rbody = GetComponent<Rigidbody2D>();
rbody.velocity = new Vector2(0, 0);
}

}