跑酷综合案例
案例功能流程:游戏开始进入开始界面,输入账号密码,点击开始游戏进入游戏场景。进入游戏主角开始移动吃到金币加分,碰到障碍物血条掉血,中间通过放置碰撞器碰到指定标签的碰撞器开始在指定位置复制金币和场景,如果血条为0则暂停游戏,弹出隐藏画布(从新开始)(退出游戏)。退出游戏退出到开始界面。
游戏开始进入开始界面
点击开始按钮进入下一个游戏界面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ks : MonoBehaviour
{
public void startMenu()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
人物移动+加分掉血代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//加分
using UnityEngine.SceneManagement;//场景
public class rolemove : MonoBehaviour
{
//摄像机一直跟随主角,主角一直向前跑通过按AD键躲避障碍物W键跳跃
//
public GameObject role;//主角
public float movesp = 10f;
public float jumpsp = 10f;
float fenshu;//
public Text wenb;//场景加分显示
public float jiasusp =50f;//碰到加速带速度
public static int diaoxie;//
public Text dxl;//掉血文本显示
float tishi;//
public Text tsxl;//快掉血提示
// public GameObject targetScene; // 指定要消失的场景对象
public AudioSource audioSource;//射击音频源
public AudioClip bulletSound;//射击音频
public AudioSource yinyuan;//
public AudioClip yinxu;//开始音乐
public GameObject huabuui;//显示隐藏的画布
void Update()
{
key();
role.transform.Translate(new Vector3(0, 0, 1 * movesp * Time.deltaTime));//一直向前跑
}
private void Start()
{
diaoxie = 10000;
PlaySound1();
}
void PlaySound1()
{
yinyuan.clip = yinxu; //
yinyuan.Play(); // 播放音效
}
void key()//人物移动
{
float zy = Input.GetAxis("Horizontal");//左右移动代码
role.transform.Translate(new Vector3(zy *jumpsp * Time.deltaTime, 0, 0));
if (Input.GetKey(KeyCode.W))
{
jump();
}
}
void jump()//
{
Debug.Log("我开始跳跃了");
role.transform.Translate(new Vector3(0, 1 * jumpsp * Time.deltaTime, 0));
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))//如果碰到碰撞器标签是player的金币就让他消失并且加金币音效
{
jf();
PlaySound();
}
if (other.gameObject.CompareTag("Finish"))//如果碰到碰撞器标签是Finish的物体就减血
{
dx();
}
if (diaoxie<=0)//如果用户血量小于等于0则弹出隐藏画布同时暂停游戏
{
huabuui.SetActive(true);
Time.timeScale = 0;
}
if (other.gameObject.CompareTag("Respawn")) //如果碰到碰撞器标签是Respawn的就加速
{
role.transform.Translate(new Vector3(0, 0,7* jiasusp * Time.deltaTime));
}
//if (other.gameObject.tag == "Finish") // 判断是否是主角碰到了触发器
//{
// other.gameObject.SetActive(false); // 销毁指定场景对象
// }
void jf()
{
fenshu += 1;
Debug.Log(("当前分数:" + fenshu));
wenb.text = "" + fenshu;
other.gameObject.SetActive(false);
}
void dx()
{
diaoxie = diaoxie - 1000;
Debug.Log("当前血量:" + diaoxie);
dxl.text = "当前血量:" + diaoxie;
}
if (diaoxie <= 3000)//如果用户血量小于等于3000则显示快没血了
{
tx();
}
void tx()
{
tsxl.text = "快没血了!!!";
}
void PlaySound()//金币音效
{
audioSource.clip = bulletSound; // 设置音频片段为金币音效
audioSource.Play(); // 播放音效
}
}
}//end
滚动条掉血
代码给人物
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class xuetiao : MonoBehaviour
{
public Slider oneSlider;//滚动条
public static int diaoxie;//掉血量
void Start()
{
//初始化滑动条的最大数值为10000,这个是为了与另一个脚本的变量保持一致
oneSlider.maxValue = 10000;
oneSlider.value = 10000;//这个是滑动条当前的数值,随着这个数值的变化,滑动条自己会滑动
diaoxie = 10000;
}
// Update is called once per frame
private void OnTriggerEnter(Collider other)//如果人物碰到标签是Finish的障碍物时掉血
{
if (other.gameObject.CompareTag("Finish"))
{
diaoxie = diaoxie - 1000;
}
}
void Update()
{
do
{
if (oneSlider == null)
{
Debug.Log("滑动条为空");
break;
}
//将上面拿到血量变量实时赋值给滑动条的当前数值
oneSlider.value = diaoxie;
} while (false);
}
}
金币克隆
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class kljinbi : MonoBehaviour
{
public GameObject cjmb1;//场景模板
public GameObject cffq1;//克隆模板暂存
GameObject pzqd1;//碰撞器点
private void OnTriggerEnter(Collider other)//场景克隆代码
{
pzqd1 = other.transform.gameObject;//指出碰撞器点位置
Debug.Log("开始克隆障碍物");
Vector3 kld = new Vector3(pzqd1.transform.position.x+9, pzqd1.transform.position.y+1, pzqd1.transform.position.z+50);//指出场景克隆在碰撞点前方指定位置
GameObject.Instantiate(cjmb1, kld, Quaternion.Euler(0, 0, 0), cffq1.transform);//克隆代码+旋转
}
}
场景克隆
如果碰到了碰撞器就克隆场景在克隆点前方指定位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zaiwu : MonoBehaviour
{
public GameObject cjmb;//场景模板
public GameObject cffq;//克隆模板暂存
GameObject pzqd;//碰撞器点
// Start is called before the first frame update
private void OnTriggerEnter(Collider other)//场景克隆代码
{
pzqd = other.transform.gameObject;//指出碰撞器点位置
Debug.Log("开始克隆障碍物");
Vector3 kld = new Vector3(pzqd.transform.position.x -4 , pzqd.transform.position.y, pzqd.transform.position.z + 65);//指出场景克隆在碰撞点前方指定位置
GameObject.Instantiate(cjmb, kld, Quaternion.Euler(0, 90, 0), cffq.transform);//克隆代码+旋转
}
}