做一个简单的Flappy Bird小游戏

原创
2020/04/10 22:10
阅读数 39

1.打开unity,新建一个2D场景

2.将所有素材拖进去

3.添加两根导管 作为鸟穿过的通道,给导管添加Box Collider和声音,复制同样的两份,添加脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PipeCollision : MonoBehaviour {
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag=="Player")
        {
            this.GetComponent<AudioSource>().Play();
            SceneManager.LoadScene(0);
        }
    }
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
4.添加鸟的素材,设置rigidbody、Sphere Collider和鸟飞行时的声音,添加脚本控制鸟的运动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour {
    public float timer;
    public int frameCount = 0;
    public int frameNumber = 10;
    public float birdSpeed = 5.0f;

    // Use this for initialization
    void Start () {
        this.GetComponent<Rigidbody>().velocity = new Vector3(birdSpeed, 0, 0);
    }
    
    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime;
        if(timer>=1.0f/frameNumber)
        {
            frameCount = (frameCount + 1) % 3;
            timer = 0;
            this.GetComponent<Renderer>().material.SetTextureOffset("_MainTex", new Vector2(0.3333f * frameCount, 0));
        }
        if(Input.GetMouseButton(0))
        {
            Vector3 vel = this.GetComponent<Rigidbody>().velocity;
            this.GetComponent<Rigidbody>().velocity = new Vector3(3, 3, vel.z);
            this.GetComponent<AudioSource>().Play();
        }
    }
}
5.新建一个空对象,设置当鸟撞到导管时发生的事情,添加一个Box Collider,添加脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveTrigger : MonoBehaviour {
    public Transform currentBG;
    public Pipe p1;
    public Pipe p2;
    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag=="Player")
        {
            Transform firstBG = GameManager._instance.lastBG;
            currentBG.position = new Vector3(firstBG.position.x + 10, currentBG.position.y, currentBG.position.z);
            GameManager._instance.lastBG = currentBG;
            p1.RandomPosition();
            p2.RandomPosition();
        }
    }
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
6.相机跟着鸟运动,添加脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {
    public GameObject bird;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        Vector2 birdPosition = bird.transform.position;
        float posY = birdPosition.y;
        if(posY>2)
        {
            posY = 2;
        }
        else if(posY<-2)
        {
            posY = -2;
        }
        this.transform.position = new Vector3(birdPosition.x + 9f, birdPosition.y, transform.position.z);
    }
}
7.添加一个脚本统计分数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
    public Transform lastBG;
    public static GameManager _instance;
    public int score = 0;
    // Use this for initialization
    private void Awake()
    {
        _instance = this;
    }
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
 

 

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部