Unity經典游戲教程之:冒險島


版權聲明:

  • 本文原創發布於博客園"優夢創客"的博客空間(網址:http://www.cnblogs.com/raymondking123/)以及微信公眾號"優夢創客"(微信號:unitymaker)
  • 您可以自由轉載,但必須加入完整的版權聲明!

游戲簡介

是一款動作冒險類游戲,由HUDSON公司發售,是一款2D橫向卷軸游戲。游戲的主人公是當時游戲少年們所稱贊的高橋名人與冒險島中的名人很像。

image

場景搭建

1.將3張背景連接

image
image
image

前景層

1.將玩家,障礙,怪物,道具與勝利點放置在背景上
2.設置他們的Sorting Layer為ForeGround

image

3.按照他們的先后設置他們的Order in Layer 的大小

攝像機位置的限制

1.在玩家子節點下建立一個新的坐標點用來控制偏移量

···

public Transform player, boundLeft, boundRight;//創建公有的坐標點
Vector3 pos;
void Start () {
    pos = transform.position - player.position;
}
void Update () {
    Vector3 camPos = transform.position;//新建一個攝像機坐標點
    camPos.x =player.position.x+ pos.x;
    if(camPos.x>=boundRight.position.x)
    {
        return;
    }
    //camPos.x = Mathf.Clamp(player.position.x, boundLeft.position.x, boundRight.position.x);//限制攝像機的X軸在min 和max之間
    camPos.y = this.transform.position.y;
    camPos.z = this.transform.position.z;//-10
    this.transform.position = camPos;
}
2.在設置一個最右坐標點,防止攝像機跑到地圖外面去

image

給背景添加一個音樂播放器

image

子彈的發射

1.設置一個子彈預制體控制傷害以及碰撞判定消除

image

    public float damage = 10;

    public void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "boss")
        {
            collision.gameObject.GetComponent<Enemy>().Hit(damage);
        }
        if (collision.gameObject.tag == "kulou")
        {
            collision.gameObject.GetComponent<Enemy1>().Hit(damage);
        }
        if (collision.gameObject.tag != "Player")
        {
            Destroy(this.gameObject);
        }
    }
2.將子彈的Rigidbody 2D中的Gravity Scale設置為3,這樣可以保證子彈發出去以后會有一個下落
3.在人物身上添加一個子節點坐標點用來控制子彈的發射位置

image

4.給這個位置寫一個腳本用來控制子彈的速度以及顯示

image


    public float speed = 10f;
    public GameObject rocketPrefab;
    void Start()
    {
    
    }
    public void Shoot()
    {

            var rocket = Instantiate(rocketPrefab);
            rocket.transform.position = this.transform.position;
            if (this.transform.parent.localScale.x > 0)
            {
                rocket.transform.rotation = Quaternion.Euler(0, 0, 0);
                rocket.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
            }
            else
            {
                rocket.transform.rotation = Quaternion.Euler(0, 0, 180);
                rocket.GetComponent<Rigidbody2D>().velocity = new Vector2(-speed, 0);
            }
        }

人物控制

1.添加動畫控制器

image
image

2.給人物添加一個腳本用來更新角色的行動並將攻擊作為一個bool值,一開始為false,當吃到道具時再開啟

image

    public float moveSpeed = 10f;
    public float moveForce = 10f;
    private bool jump = false;//是否按了跳躍鍵並且可以跳躍
    public float jumpForce = 255f;
    public bool gongji = false;
    public static playerControl instance;//單件模式
    private Rigidbody2D rb;
    void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
    }
    public void Awake()
    {
        instance = this;
    }
    void Update()
    {
        RaycastHit2D hit = Physics2D.Raycast(this.transform.position, Vector2.down, 0.5f, 1 << LayerMask.NameToLayer("Ground"));

        Debug.DrawRay(this.transform.position, Vector2.down);

        if (Input.GetButtonDown("Jump") && hit)
        {
            jump = true;
        }
        if (Input.GetButtonDown("Fire1"))
        {
            if (gongji == true)
            {
                GetComponent<Animator>().SetTrigger("Fire");
            }
        }
    }
    void FixedUpdate()
    {
        if (PlayerHealth.instance.gameOver == 0)
        {
            float h = Input.GetAxis("Horizontal");
            Vector2 force = new Vector2(h * moveForce, 0);
            if (Mathf.Abs(rb.velocity.x) < moveSpeed)
            {
                GetComponent<Animator>().SetFloat("Speed", h);
                rb.AddForce(force);
            }
            if (Mathf.Abs(rb.velocity.x) >= moveSpeed)
            {
                rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * moveSpeed, rb.velocity.y);
            }

            if (h > 0)
            {
                var s = this.transform.localScale;
                s.x = Mathf.Abs(s.x);
                this.transform.localScale = s;
            }
            else if (h < 0)
            {
                var s = this.transform.localScale;
                s.x = -Mathf.Abs(s.x);
                this.transform.localScale = s;

            }
            if (jump)
            {

                GetComponent<Animator>().SetTrigger("Jump");
                rb.AddForce(Vector2.up * jumpForce);
                jump = false;
            }
        }
    }
3.在人物上添加一個腳本,用來控制玩家是否死亡以及碰撞

image

    public int gameOver;
    void Update()
    {
        TimeOut();
        if (gameOver ==1)
        {
            this.GetComponent<Animator>().SetTrigger("dead");
            if (Input.GetMouseButtonDown(0))
            {
                Application.LoadLevel(Application.loadedLevelName);
            }
        }
        if (gameOver == 2)
        {
            this.GetComponent<Animator>().SetTrigger("Firedead");
            if (Input.GetMouseButtonDown(0))
            {
                Application.LoadLevel(Application.loadedLevelName);
            }
        }
    }
    public void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag=="Wall")
        {
            this.GetComponent<Animator>().SetTrigger("sleep");
        }
        if(collision.gameObject.tag=="boss")
        {
            this.gameOver = 1;
        }
        if(collision.gameObject.tag=="Fire")
        {
            this.GetComponent<Animator>().SetTrigger("Firedead");
            this.gameOver = 2;
        }
    }
    private void TimeOut()
    {
        if (timeLeft > 0)
        {
            timeLeft = timeLeft - Time.deltaTime;
            timetext.text = "Time Left:" + (int)timeLeft;
        }
        if (timeLeft > 50)
        {
            timeLeft = 50;
        }
        if (timeLeft <= 0)
        {
            timeLeft = 0f;

            this.gameOver = 1;
        }
    }
    public void Playerdead()
    {
        gameOver = 1;
        Vector2 v = new Vector2(0, 5);
        this.GetComponent<Rigidbody2D>().velocity = v;
        this.GetComponent<BoxCollider2D>().isTrigger = true;
    }
    public void PlayerFireDead()
    {
        gameOver = 2;
        this.GetComponent<BoxCollider2D>().isTrigger = true;
    }
4.在這個腳本上控制道具的得分以及時間加成

    private int score = 0;
    public Text scoreText;
    public float timeLeft = 20;
    public Text timetext;
    public void Score()
    {
        if (this.gameOver == 1)
            return;
        score += 100;
        scoreText.text = "Score:" + score;
        timeLeft += 3;
    }
    public void Duyao()
    {
        if (this.gameOver == 1)
            return;
        timeLeft -=7;
    }
    public void Niunai()
    {
        if (this.gameOver == 1)
            return;
        score += 300;
        scoreText.text = "Score:" + score;
    }
    public void Timeup()
    {
        if (this.gameOver == 1)
            return;
        timeLeft += 10;
    }
    public void yaoshi()
    {
        if (this.gameOver == 1)
            return;
        score += 500;
        scoreText.text = "Score:" + score;
        timeLeft += 10;
    }

怪物的移動與碰撞

1.給怪物添加一個碰撞器

image

2.給牆添加一個碰撞器和一個標簽為Wall,使得怪物碰撞到Wall會進行一個反向移動的效果

image

    void FixedUpdate()
    {
        RaycastHit2D hit = Physics2D.Linecast(FrontCheck.position,this.transform.position);
        if(hit&&hit.transform.gameObject.tag=="Wall")
        {
            Vector3 s = this.transform.localScale;
            s.x = -s.x;
            this.transform.localScale = s;
        }       
       Rigidbody2D rb = GetComponent<Rigidbody2D>();
        Vector2 v = new Vector2(this.transform.localScale.x * speed, rb.velocity.y);
        rb.velocity = v;
    }
3.以怪物自身中心為起點在怪物的加點下面添加一個子節點為射線的終點

image

    private Transform FrontCheck;
    void Start () {
        FrontCheck = transform.Find("frontCheck");
	}
3.在怪物腳本里給怪物設置血量,每次碰到子彈減去相對的血量,當血量沒有時觸發死亡效果

image


    public float hp = 1;
    public float damage = 1;
    public void Hit(float damage)
    {
        hp -= damage;

        if (hp <= 0)
        {            
            if (dead != null)
            {
                this.GetComponent<SpriteRenderer>().sprite = dead;
                foreach (var c in GetComponents<Collider2D>())
                {
                    c.isTrigger = true;
                }
                GetComponent<Rigidbody2D>().freezeRotation = false;
            }
        }
    }

游戲勝利

1.將游戲勝利人物設置為一個觸發器IsTrigger

image

2.設置腳本,當人物觸發勝利人物時,在屏幕中央跳出游戲勝利的字幕

image

重新開始

在人物死亡與游戲勝利的時候,點擊鼠標左鍵,就可以重新開始
    public GameObject WinText;
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "player")
        {
            WinText.SetActive(true);
        }
        if (Input.GetMouseButtonDown(0))
        {
            Application.LoadLevel(Application.loadedLevelName);
        }
    }

游戲改進

1.關卡有點短,后期進行改進
2.后期會多做幾個關卡
3.添加其他道具以及特殊場景
4.爭取做到模仿的與原版游戲無特別大的差別


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM