❤❤Fungus新課程即將發布,快寫問卷拿優惠❤❤
顯示具有 collider 標籤的文章。 顯示所有文章
顯示具有 collider 標籤的文章。 顯示所有文章

2017年7月4日 星期二

Unity 打磚塊 2D 教學(下) 過關機制


下載完整可執行遊戲的專案 二合一(台幣約60元)

Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    [Header("水平移動速度")]
    public float speedX;
    Rigidbody2D playerRigidbody2D;

    void Start()
    {
        playerRigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        moveLeftOrRight();
    }

    float LeftOrRight()
    {
        return Input.GetAxis("Horizontal");
    }

    void moveLeftOrRight()
    {
        playerRigidbody2D.velocity = LeftOrRight() * new Vector2(speedX, 0);
    }
}
Ball.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Ball : MonoBehaviour
{
    public Text scoreText;
    int score;

    Rigidbody2D ballRigidbody2D;
    CircleCollider2D ballCircleCollider2D;

    [Header("水平速度")]
    public float speedX;

    [Header("垂直速度")]
    public float speedY;

    #region 教學理解用 可不寫
    [Header("實際水平速度")]
    public float velocityX;

    [Header("實際垂直速度")]
    public float velocityY;
    #endregion



    void Start()
    {
        ballRigidbody2D = GetComponent<Rigidbody2D>();
        ballCircleCollider2D = GetComponent<CircleCollider2D>();

        //切換成Kinematic模式
        //Uity 2018版以後不加這行的話 發球之前球會無法跟著球拍移動
        ballRigidbody2D.bodyType = RigidbodyType2D.Kinematic;

        scoreText.text = "目前分數:";
        Invoke("ballStart", 3);
    }

    void Update()
    {
        #region 教學理解用 可不寫
        velocityX = ballRigidbody2D.velocity.x;
        velocityY = ballRigidbody2D.velocity.y;
        #endregion

        if (Input.GetKey(KeyCode.Space))
        {
            ballStart();
        }
    }

    void ballStart()
    {
        if (isStop())
        {
            ballCircleCollider2D.enabled = true;
            transform.SetParent(null);
            ballRigidbody2D.velocity = new Vector2(speedX, speedY);
            
            //Unity 2018以後的版本需要加下面這行:
            //改回預設的Dynamic,使用Unity內建的物理運動規則
            ballRigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        }
    }

    bool isStop()
    {
        return ballRigidbody2D.velocity == Vector2.zero;
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        lockSpeed();
        if (other.gameObject.CompareTag(tags.磚塊.ToString()))
        {
            GameManager.brickCount--;
            Debug.Log("目前磚塊數量: "+GameManager.brickCount);
            GameManager.checLevelClearOrNot();
            other.gameObject.SetActive(false);
            score += 10;
            scoreText.text = "目前分數:" + score;
        }
    }

    void lockSpeed()
    {
        Vector2 lockSpeed = new Vector2(resetSpeedX(), resetSpeedY());
        ballRigidbody2D.velocity = lockSpeed;
    }

    float resetSpeedX()
    {
        float currentSpeedX = ballRigidbody2D.velocity.x;
        if (currentSpeedX < 0)
        {
            return -speedX;
        }
        else
        {
            return speedX;
        }
    }

    float resetSpeedY()
    {
        float currentSpeedY = ballRigidbody2D.velocity.y;
        if (currentSpeedY < 0)
        {
            return -speedY;
        }
        else
        {
            return speedY;
        }
    }
}
Trap.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Trap : MonoBehaviour {
    [Header("被撞到時候的位移")]
    public Vector3 offset;

    public int life;

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag(tags.球.ToString()))
        {
            life--;
            gameObject.transform.position += offset;
        }
        if (life<=0&&!GameManager.LevelClear)
        {
            GameManager.ReloadThisScene();
        }

    }

}
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    [Header("可打破的磚塊初始數量")]
    public static int brickCount;

    static GameObject nextLevelButton;

    public static void ReloadThisScene() {
        Scene current = SceneManager.GetActiveScene();
        SceneManager.LoadScene(current.name);
    }

    public static bool LevelClear {
        get {

            if (brickCount<=0)
            {
                return true;
            }
            return false;
        }
    }
    public static void checLevelClearOrNot() {
        if (LevelClear)
        {
            showNextLevelButton();
        }
    }
    public void GotoScene(string next) {
        SceneManager.LoadScene(next);
    }


    void Start()
    {
        nextLevelButton = GameObject.FindGameObjectWithTag(tags.下一關按鈕.ToString());
        nextLevelButton.SetActive(false);

        brickCount = GameObject.FindGameObjectsWithTag(tags.磚塊.ToString()).Length;
        Debug.Log("一開始有 "+brickCount+" 個可打破的磚塊");
    }

    static void showNextLevelButton() {
        nextLevelButton.SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {

    }
}

enum tags
{
    磚塊, 背景, 球拍, 球, 下一關按鈕
}
上集的程式碼與教學
http://www.morningfungame.com/2017/01/Unity-Arkanoid-breakout-tutorial-2D.html

2017年1月25日 星期三

Unity 打磚塊 2D 教學 (上)


下載完整可執行遊戲的專案 二合一(台幣約60元)

Player.cs

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

public class Player : MonoBehaviour {

    [Header("水平移動速度")]
    public float speedX;
    Rigidbody2D playerRigidbody2D;

    void Start () {
        playerRigidbody2D = GetComponent<Rigidbody2D> ( );
 }
 
 void Update () {
        moveLeftOrRight ( );
    }

    float LeftOrRight ( ) {
        return Input.GetAxis ( "Horizontal");
        }

    void moveLeftOrRight ( ) {
        playerRigidbody2D.velocity = LeftOrRight() * new Vector2 (speedX,0 );
        }
}


Ball.cs

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

public class Ball : MonoBehaviour
    {
    public Text scoreText;
    int score;

    Rigidbody2D ballRigidbody2D;
    CircleCollider2D ballCircleCollider2D;

    [ Header ( "水平速度" )]
    public float speedX;

    [Header ( "垂直速度" )]
    public float speedY;

    #region 教學理解用 可不寫
    [Header ( "實際水平速度" )]
    public float velocityX;

    [Header ( "實際垂直速度" )]
    public float velocityY;
    #endregion

    enum tags
        {
        磚塊,
        背景
        }

    void Start ( )
        {
        ballRigidbody2D = GetComponent<Rigidbody2D> ( );
        ballCircleCollider2D = GetComponent<CircleCollider2D> ( );

        //切換成Kinematic模式
        //Uity 2018版以後不加這行的話 發球之前球會無法跟著球拍移動
        ballRigidbody2D.bodyType = RigidbodyType2D.Kinematic;

        scoreText.text = "目前分數:";
        Invoke ( "ballStart",3 );
        }

    void Update ( )
        {
        #region 教學理解用 可不寫
        velocityX = ballRigidbody2D.velocity.x;
        velocityY = ballRigidbody2D.velocity.y;
        #endregion

        if ( Input.GetKey(KeyCode.Space) )
            {
            ballStart ( );
            }
        }

    void ballStart ( ) {
        if ( isStop ( ) )
            {
            ballCircleCollider2D.enabled = true;
            transform.SetParent ( null );
            ballRigidbody2D.velocity = new Vector2 ( speedX , speedY );
            
            //Unity 2018以後的版本需要加下面這行:
            //改回預設的Dynamic,使用Unity內建的物理運動規則
            ballRigidbody2D.bodyType = RigidbodyType2D.Dynamic;
            }
        }

    bool isStop ( ) {
        return ballRigidbody2D.velocity == Vector2.zero;
        }

    void OnCollisionEnter2D ( Collision2D other )
        {
        lockSpeed ( );
        if ( other.gameObject.CompareTag ( tags.磚塊.ToString ( ) ) )
            {
            other.gameObject.SetActive ( false );
            score += 10;
            scoreText.text = "目前分數:" + score;
            }
        }
    
    void lockSpeed ( )
        {
        Vector2 lockSpeed = new Vector2 ( resetSpeedX ( ) , resetSpeedY ( ) );
        ballRigidbody2D.velocity = lockSpeed;
        }

    float resetSpeedX ( )
        {
        float currentSpeedX = ballRigidbody2D.velocity.x;
        if ( currentSpeedX < 0 )
            {
            return -speedX;
            }
        else
            {
            return speedX;
            }
        }

    float resetSpeedY ( )
        {
        float currentSpeedY = ballRigidbody2D.velocity.y;
        if ( currentSpeedY < 0 )
            {
            return -speedY;
            }
        else
            {
            return speedY;
            }
        }
    }
下集的程式碼:
http://www.morningfungame.com/2017/07/Unity-Arkanoid-breakout-tutorial-2D-2.html