企業(yè)做網(wǎng)站有什么好處公眾號(hào)軟文范例100
項(xiàng)目成果?項(xiàng)目網(wǎng)盤
導(dǎo)入資源包 放入Assets文件Assets資源文件
游戲流程分析
攝像機(jī)size調(diào)小,讓圖片占滿屏幕
人跑本質(zhì),相對(duì)運(yùn)動(dòng),圖片無限向右滾動(dòng)
圖片720,縮小100倍第二個(gè)圖片x為7.2每unit?px100兩張圖片剛好挨著連貫
空對(duì)象BgControl,方便管理
reset
放兩張圖片
創(chuàng)腳本Bgcontrol,拖到該對(duì)象上
層級(jí)-10
腳本文件夾,c#腳本
地面對(duì)象
層級(jí)-5
碰撞組件box?collider?2d,編輯碰撞器。
掛腳本
三個(gè)地面都拖成預(yù)設(shè)體
前兩個(gè)地面固定,后一個(gè)隨機(jī)
Prefabs預(yù)設(shè)體文件夾
ground拖進(jìn)去
coin進(jìn)去
切片
多張
sprite?editor?
切片
應(yīng)用
對(duì)象聲音,掛腳本
單例
創(chuàng)建groundcontrol腳本
腳本掛到地面對(duì)象上
預(yù)設(shè)體相當(dāng)于類和對(duì)象
背景?-10
地面??-5地面預(yù)設(shè)體給標(biāo)簽ground?碰撞器
聲音?掛腳本
動(dòng)畫??窗口?跑?跳?死亡???過渡?設(shè)置參數(shù)
????????無退出,過渡0,isjump?true
主角??層級(jí)10??標(biāo)簽player?碰撞組件?剛體組件?凍結(jié)旋轉(zhuǎn)?重力縮放2,只提供重力加速度,加腳本(跳躍,地面上才能跳躍,播放聲音,動(dòng)畫切換),
button?UI?原圖像?原像素顯示,過渡?精靈切換?高亮圖像???點(diǎn)擊?玩家的跳躍?導(dǎo)航none
金幣?腳本?碰撞器?吃的觸發(fā)
桌子?放1號(hào)地面上?碰撞器?標(biāo)簽地面?預(yù)設(shè)體??
?????1地應(yīng)用所有
死亡??空對(duì)象die?碰撞器?編輯觸發(fā)器?標(biāo)簽die
???player腳本
敵人?圓形碰撞器?1地?標(biāo)簽enemy?1地應(yīng)用所有?預(yù)設(shè)體?觸發(fā)
層級(jí)
切片
?
腳本代碼
PlayerControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerControl : MonoBehaviour
{//血量public static int Hp=1;//剛體組件private Rigidbody2D rbody;//動(dòng)畫組件private Animator ani;private bool isGround;void Start(){//獲取剛體組件rbody = GetComponent<Rigidbody2D>();//獲取動(dòng)畫組件ani = GetComponent<Animator>();}void Update(){//如果按了空格鍵if (Input.GetKeyDown(KeyCode.Space)){//跳躍Jump();}}//跳躍public void Jump(){if (isGround == true){//給剛體一個(gè)向上的力rbody.AddForce(Vector2.up * 400);//播放跳躍聲音AudioManager.Instance.Play("跳");}}//發(fā)生碰撞private void OnCollisionEnter2D(Collision2D collision){//判斷如果是地面if(collision.collider.tag=="Ground"){isGround = true;//結(jié)束跳躍ani.SetBool("IsJump", false);}//如果是死亡邊界if(collision.collider.tag=="Die" && Hp > 0){//血量為0Hp = 0;////播放死亡聲音AudioManager.Instance.Play("Boss死了");//播放死亡動(dòng)畫ani.SetBool("IsDie", true);}}//結(jié)束碰撞private void OnCollisionExit2D(Collision2D collision){//判斷如果是地面if (collision.collider.tag == "Ground"){isGround = false;//開始跳躍ani.SetBool("IsJump", true);}}//如果碰到敵人private void OnTriggerEnter2D(Collider2D collision){if (collision.tag == "Enemy"){//血量為0Hp = 0;////播放死亡聲音AudioManager.Instance.Play("Boss死了");//播放死亡動(dòng)畫ani.SetBool("IsDie", true);}}
}
CoinControl?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CoinControl : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}//如果產(chǎn)生觸發(fā)private void OnTriggerEnter2D(Collider2D collision){//播放吃金幣的聲音AudioManager.Instance.Play("金幣");//銷毀自己Destroy(gameObject);}
}
AudioMnager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class AudioManager : MonoBehaviour
{//單例public static AudioManager Instance;//播放組件private AudioSource player;void Start(){//單例Instance = this;//獲取播放組件player = GetComponent<AudioSource>();}//播放音效public void Play(string name){//通過名稱獲取音頻片段AudioClip clip = Resources.Load<AudioClip>(name);//播放player.PlayOneShot(clip);}
}
BgControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class BgControl : MonoBehaviour
{//速度 每幀移動(dòng)0.2像素public float Speed = 0.2f;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update() //update是每一幀會(huì)調(diào)用一次{//如果玩家血量為0if(PlayerControl.Hp==0){return;}//遍歷背景,背景就是子物體foreach (Transform tran in transform){//獲取子物體的位置Vector3 pos = tran.position;//按照速度向左側(cè)移動(dòng)pos.x -= Speed * Time.deltaTime; //每秒向左側(cè)移動(dòng)0.2//判斷是否出了屏幕if (pos.x < -7.2f){//把圖片移動(dòng)到右邊pos.x += 7.2f * 2;}//位置賦給子物體tran.position = pos;}}
}
GroundControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GroundControl : MonoBehaviour
{//速度public float Speed = 2f;//要隨機(jī)的地面數(shù)組public GameObject[] GroundPrefabs;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update() //update是每一幀會(huì)調(diào)用一次{//如果玩家血量為0if (PlayerControl.Hp == 0){return;}//遍歷背景,背景就是子物體foreach (Transform tran in transform){//獲取子物體的位置Vector3 pos = tran.position;//按照速度向左側(cè)移動(dòng)pos.x -= Speed * Time.deltaTime; //每秒向左側(cè)移動(dòng)0.2//判斷是否出了屏幕if (pos.x < -7.2f){//創(chuàng)建新的地面Transform newTrans = Instantiate(GroundPrefabs[Random.Range(0, GroundPrefabs.Length)], transform).transform;
// ,后transform確定父子關(guān)系 .后transform拿到新地面的transform組件//獲取新地面的位置Vector2 newPos = newTrans.position;//設(shè)置新地面的位置newPos.x = pos.x + 7.2f * 2;//位置設(shè)置回去newTrans.position = newPos;//銷毀舊的地面(出了屏幕的地面)Destroy(tran.gameObject);}//位置賦給子物體tran.position = pos;}}
}