最吸引人的廣告圖片seo教學(xué)免費課程霸屏
腳本1 觸發(fā)器腳本?
這個腳本是主角身上的腳本,用于檢測是否碰到其他觸發(fā)器,并做出對應(yīng)的行為?
?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColliidisonTrigger : MonoBehaviour
{
? ? //觸發(fā)檢測 ?1.碰到金幣克隆區(qū),克隆金幣 2.恐克隆怪物 ?3.碰撞金幣 吃掉它 加分 4.碰到怪物,掉血 5碰到加速帶加速、減速
? ? //該觸發(fā)檢測腳本掛到 主角身上
? ? //PlayerCont onePlayer = new PlayerCont();
??
? ? GameObject BeiPengObj;
? ? public GameObject MosterMuban;
? ? public GameObject CloneStruFather;? ? public GameObject JInbiMUban;
? ? public GameObject JinbiFather;
? ??
? ? private void OnTriggerEnter(Collider other)
? ? {
? ? ? ? ??
? ? ? ? ? ?BeiPengObj = other.transform.gameObject;
? ? ? ? //如果碰到觸發(fā)器,執(zhí)行一次?
? ? ? ? Debug.Log("碰到了別人");
? ? ? ? if (BeiPengObj.tag == "MonsterTrigger")//碰到了怪物克隆區(qū)觸發(fā)器
? ? ? ? {
? ? ? ? ? ? CloneMonster();? ? ? ? }
? ? ? ? if (BeiPengObj.tag == "rCoinTrigger")//碰到了金幣克隆區(qū)觸發(fā)器
? ? ? ? {
? ? ? ? ? ? Debug.Log("kelongjinbi");
? ? ? ? ? ? CloneCoin();
? ? ? ? }
? ? ? ? if (BeiPengObj.tag == "TriggerSpeedUp")//碰到了加速區(qū)觸發(fā)器
? ? ? ? {
? ? ? ? ? ? Debug.Log("開始加速");
? ? ? ? ? ? Speedup();
? ? ? ? }? ? ? ? if (BeiPengObj.tag == "Coin")//碰到了金幣觸發(fā)器
? ? ? ? {
? ? ? ? ? ? Debug.Log("吃掉,消失金幣,加分");
? ? ? ? ? ? Destroy(BeiPengObj);
? ? ? ? ? ? addScore();
? ? ? ? }? ? ? ? if (BeiPengObj.tag == "Monster")//碰到了怪物障礙物
? ? ? ? {
? ? ? ? ? ? Debug.Log("掉血");
? ? ? ? ? ? ScoreManager.SubBlood(); //跨類訪問并執(zhí)行分數(shù)類里面的掉血函數(shù)? ??
? ? ? ? }
? ? }? ? void CloneMonster()
? ? {
? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? {
? ? ? ? ? ? Vector3 ONETEMPPOS = new Vector3(156, 0, BeiPengObj.transform.position.z + 50f+i*3);
? ? ? ? ? ? Debug.Log("克隆怪物去");
? ? ? ? ? ? GameObject.Instantiate(MosterMuban, ONETEMPPOS, Quaternion.Euler(0, 0, 0), CloneStruFather.transform);
? ? ? ? }
? ? }
? ? void CloneCoin()
? ? {
? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? {
? ? ? ? ? ? Vector3 ONETEMPPOS = new Vector3(156, 0, BeiPengObj.transform.position.z + 50f + i * 3);
? ? ? ? ? ? Debug.Log("克隆怪物去");
? ? ? ? ? ? GameObject.Instantiate(JInbiMUban, ONETEMPPOS, Quaternion.Euler(0, 0, 0), JinbiFather.transform);
? ? ? ? }
? ? }
? ? void Speedup()
? ? {
? ? ? ? PlayerCont.RunSpeed = 20f;
? ? }? ? void addScore()
? ? {
? ? ? ? ScoreManager.ScoreAdd();//跨類訪問并執(zhí)行分數(shù)類里面的加分函數(shù)
? ? }? ?
}//end class?
腳本2 分數(shù)管理腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ScoreManager : MonoBehaviour
{
? ? //分數(shù)管理 血量管理
? ? // 如果撞擊到金幣就加分,如果撞擊到障礙物就掉血? ? public static ?int CurrentScore=0;
? ? public static int CurrentBlood = 10000;
? ? public static void ScoreAdd()
? ? {
? ? ? ? Debug.Log("加分函數(shù)開始執(zhí)行");
? ? ? ? CurrentScore += 10;
? ? ? ? Debug.Log("分:"+CurrentScore);
? ? }
? ? public static void SubBlood()
? ? {
? ? ? ? Debug.Log("掉血函數(shù)開始執(zhí)行");
? ? ? ? CurrentBlood -= 100;
? ? ? ? Debug.Log("血:" + CurrentBlood);
? ? }
? ? private void OnGUI()
? ? {
? ? ? ? Rect oneLableRec = new Rect(100, 100, 50, 50);
? ? ? ? GUILayout.Box(CurrentBlood.ToString(), GUILayout.Width(200), GUILayout.Height(50), GUILayout.ExpandWidth(false));?
? ? ? ? // 創(chuàng)建另一個矩形框,背景色為紅色,邊框?qū)挾葹?像素
? ? ? ? GUILayout.Box(CurrentScore.ToString(), GUILayout.Width(200), GUILayout.Height(70), GUILayout.ExpandWidth(false));
? ? ? ? GUILayout.TextField("", GUILayout.Width(200), GUILayout.Height(50), GUILayout.ExpandWidth(false));
? ? }}
?
腳本3 空物體移動的腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerCont : MonoBehaviour
{
? ? //Horizontal
? ? //1.控制玩家移動,input.getaix
? ? //2.A、D控制向左、右移動
? ? //3.W控制跳躍
? ? public GameObject Player;
? ? public float LRSPEED = 10F;
? ? public float JUMPSPEED = 5;
? ?
? ? public static float RunSpeed;? ? private void Start()
? ? {
? ? ? ? RunSpeed = 10f;
? ? }
? ? private void Update()
? ? {
? ? ? ? UserInput();
?
? ? }
? ? //*=========================
? public ?void UserInput()
? ? {
? ? ? ? //Debug.Log("檢測用戶按下的鍵盤A D W");
? ? ? ? float MoveZuoY = Input.GetAxis("Horizontal");
? ? ? ? ControlMove(MoveZuoY);
? ? ? ??
? ? ? ? if (Input.GetKey(KeyCode.W))
? ? ? ? {
? ? ? ? ? ? Jump();
? ? ? ? }
? ? }
? ? void ControlMove(float ZuoyouMove)
? ? {
? ? ? ? Player.transform.Translate(new Vector3(ZuoyouMove * LRSPEED * Time.deltaTime, 0, 0));? ? }
? ?
? ? void Jump()
? ? {
? ? ? ? Player.transform.Translate(new Vector3(0, JUMPSPEED * Time.deltaTime, 0));
? ? ? ? //Debug.Log("角色開始移動了");
? ? }
? ? private void FixedUpdate()
? ? {
? ? ? ? AwalysRun();
? ? }
?void AwalysRun()
? ? {
? ? ? ? Player.transform.Translate(new Vector3(0, 0, 1 * RunSpeed * Time.deltaTime));
? ? }
}
??