Unity 2D游戏制作
調試背景
2D游戲的制作最重要的是層的設置
先設置兩個層①Layers②Edit Layers③Sorting Layers添加兩個層BackGround和Front
調試大雁
創建2D object改名BackGround設置背景圖片修改為BackGround 0層
創建2D object添加大雁圖片Ctrl+6,保存,把大雁動態模型全選,插入。調整時間可以看起來舒服,調整所在層為BackGround 1層
添加腳本使大雁從屏幕右邊開始飛入左邊飛出
publicclassSwanFly : MonoBehaviour {
??? //天鵝起飛的位置
??? privateVector3 startPosition;
??? publicfloat speed;
???????? // Use this for initialization
???????? void Start () {
??????? Debug.Log("屏幕的寬度" + Screen.width);
??????? Debug.Log("屏幕的高度" + Screen.height);
??????? Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
??????? float swanExtentsX = transform.GetComponent<Renderer>().bounds.extents.x;
??????? startPosition = newVector3(screenSize.x + transform.position.y, transform.position.z);
??????? transform.position = startPosition;
???????? }??????
???????? // Update is called once per frame
???????? void Update () {
??????? if (transform.position.x <-startPosition.x)
??????? {
??????????? transform.position = startPosition;
??????? }
??? ????transform.Translate(Vector3.right * -1 * speed * Time.deltaTime);
???????? }
}
調整草地
設置空物體,在其中創建草地,兩邊超過相機邊框。
在空物體上掛載BoxCollider2D組件,點擊EditCollider 會有一個綠色方框調整位置
如圖所示即可
?
?
游戲對象設置
設置球添加Front0層,掛載Circle Collider2D和Rigibody2D組件
創建一個2D空物體掛載腳本,隨機產生球
publicclassCreatBall : MonoBehaviour {
??? //保齡球預制體
??? publicGameObject ballPrefab;
??? //產生的范圍
??? privatefloat createRange;
??? //產生的時間
??? publicfloat rate;
???????? // Use this for initialization
???????? void Start () {
??????? Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
??????? float ballExtentsX = ballPrefab.transform.GetComponent<Renderer>().bounds.extents.x;
??????? createRange = screenSize.x -ballExtentsX;
??? }
???????? // Update is called once per frame
???????? void Update () {
??????? rate -= Time.deltaTime;
??????? if (rate<0)
??????? {
??????????? Vector3 position = newVector3(Random.Range(-createRange, createRange),transform.position.y, transform.position.z);
???????? ???GameObject ball = Instantiate(ballPrefab, position, Quaternion.identity) asGameObject;
??????????? //下一個保齡球產生的時間
??????????? rate = Random.Range(1.5f, 2.5f);
??????????? Destroy(ball, 3f);
??????? }
???????? }
}
調試接物體的物體
創建兩個2D object分別添加帽子的內沿和外沿,外沿為內沿的子物體
內沿層Front 0層? 外延層Front 1層
分別掛載Edge Collider 2D組件,內沿勾選Is Trigger,發生碰撞
設置Edit Collider
外沿? 內沿
掛載腳本接球然后銷毀產生粒子效果
publicclassHatController : MonoBehaviour {
??? //帽子移動的范圍
??? privatefloat HatMoveRange;
??? //粒子產生的位置
??? publicGameObject particleSystemPoint;
??? publicGameObject particle;
???????? // Use this for initialization
???????? void Start () {
??????? Vector3 screenSize = Camera.main.ScreenToWorldPoint(newVector3(Screen.width, Screen.height, 0));
??????? float hatExtentsX = transform.GetComponent<Renderer>().bounds.extents.x;
??????? HatMoveRange = screenSize.x -hatExtentsX;
??? }
???????? // Update is called once per frame
???????? void Update () {
??????? Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
??????? float x = Mathf.Clamp(mousePosition.x,-HatMoveRange, HatMoveRange);
??????? transform.position = newVector3(x, transform.position.y, transform.position.z);
???????? }
??? voidOnTriggerEnter2D(Collider2D col)
??? {
??????? //粒子產生
??????? GameObject tempParticle = Instantiate(particle,particleSystemPoint.transform.position, Quaternion.identity)asGameObject;
??????? Destroy(col.gameObject);
??????? Destroy(tempParticle, 2f);
??? }
}
設置粒子效果
設置預制體的Renderer中 Sorting Layer為Front 2層
離子產生的位置設置一個2D空物體成為帽子內沿的子物體,調整位置
總結
以上是生活随笔為你收集整理的Unity 2D游戏制作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红楼梦-传文
- 下一篇: 【java】饲养员喂养动物程序设计