个人项目(6):其他
胜利
我们要为游戏添加胜利条件,即在终点处放置一个立方体的触发器,将其设置为不可见,当主角进入到触发器中则显示游戏胜利UI并使场景中所有其他角色暂停行动。
public class endgame : MonoBehaviour
{
public playeraction player;
bool isend=false;
Transform follow;
void Start()
{
follow=GameObject.FindWithTag("Player").transform;
}
private void OnTriggerStay(Collider other){
if(other.transform==follow){
isend=true;
}
}
private void OnTriggerExit(Collider other){
if(other.transform==follow){
isend=false;
}
}
void Update()
{
if(isend==true){
player.iswin=true;
}
}
}
if(iswin==true){//角色胜利
Animator.SetBool("ismoving",false);
gameend2();
}
void gameend2(){//游戏胜利
gamewin.alpha=1;
}
//敌人脚本
if(player.iswin==true){//主体角色胜利
as1.Stop();
musicplaying=true;
movement.Set(0.0f,0.0f,0.0f);
Animator.SetBool("isrunning",false);
Animator.SetBool("isattacking",false);
}
游戏胜利UI制作与游戏失败UI类似。
血包
我们可以在场景中添加血包,当主角靠近血包触发器时使主角血条回满,血包模型制作(用ProBuilder实现):
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pgikGySm-1661321100264)(https://gitee.com/kirove/blog/raw/master/img/20220824114804屏幕截图 2022-08-23 171101.png)]
制作完成后我们便将血包移动场景中相应位置,并添加材质使其颜色为红色,以下是血包功能的逻辑代码:
public class bloodpacks : MonoBehaviour
{
public playeraction player;
bool isinrange=false;
Transform follow;
void Start()
{
follow=GameObject.FindWithTag("Player").transform;
}
private void OnTriggerEnter(Collider other){
if(other.transform==follow){
isinrange=true;
}
GetComponent<Renderer>().enabled = false; //血包模型消失
}
private void OnTriggerExit(Collider other){
if(other.transform==follow){
isinrange=false;
}
}
void Update()
{
if(isinrange==true){
player.hp=player.hp1;//hp1是玩家总血量
player.isheal=true;
}
}
//主角脚本
if(isheal==true){//已拾取血瓶
image.fillAmount=hp/hp1;
}
游戏结束
为了方便游戏随时结束,可以设置按esc键使游戏退出:
if (Input.GetKeyDown(KeyCode.Escape))//esc键退出游戏
{
quit();
}
void quit(){//游戏退出
//UnityEditor.EditorApplication.isPlaying = false;
Application.Quit();
}
结语
此次项目的实现过程中我遇到了许多困难,但在最终预览到自己的实现效果时还是成就感满满的。就本次项目制作而言,我收获了许多知识和技能,但同时也存在着一些不足,例如摄像头跟随功能的扩充、子弹效果制作的不完善等等,这些都还需要我之后多加学习和实践。