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

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年5月9日 星期二

Unity 教學 2D 遊戲角色移動 程式篇





影片中的專案下載(台幣約30元)
https://gumroad.com/l/iLyXq

Unity 2D遊戲角色移動控制 程式篇
用淺顯易懂的方式解說如何實作2D角色移動的控制 包含走路與跳躍
而且能用 Physics2D.Linecast 判定是否在地板上,是的話才能跳躍(也就是不能在空中連跳)
並且附上2D platform Effector 的簡易說明

延伸閱讀:
Unity 5.5 2D Effectors 力場教學:2D平台, 橫向捲軸, 風吹, 瀑布, 漂浮, 黑洞, 爆炸, 輸送帶
http://www.morningfungame.com/2017/01/unity-55-2d-effectors-2d.html

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

public class Player : MonoBehaviour
{
    Rigidbody2D playerRigidbody2D;

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

    [Header("目前的水平方向")]
    public float horizontalDirection;//數值會在 -1~1之間

    const string HORIZONTAL = "Horizontal";

    [Header("水平推力")]
    [Range(0, 150)]
    public float xForce;

    //目前垂直速度
    float speedY;

    [Header("最大水平速度")]
    public float maxSpeedX;

    [Header("垂直向上推力")]
    public float yForce;

    [Header("感應地板的距離")]
    [Range(0, 0.5f)]
    public float distance;

    [Header("偵測地板的射線起點")]
    public Transform groundCheck;

    [Header("地面圖層")]
    public LayerMask groundLayer;

    public bool grounded;

    public void ControlSpeed()
    {
        speedX = playerRigidbody2D.velocity.x;
        speedY = playerRigidbody2D.velocity.y;
        float newSpeedX = Mathf.Clamp(speedX, -maxSpeedX, maxSpeedX);
        playerRigidbody2D.velocity = new Vector2(newSpeedX, speedY);
    }

    public bool JumpKey
    {
        get
        {
            return Input.GetKeyDown(KeyCode.Space);
        }
    }

    void TryJump()
    {
        if (IsGround && JumpKey)
        {
            playerRigidbody2D.AddForce(Vector2.up * yForce);
        }
    }

    //在玩家的底部射一條很短的射線 如果射線有打到地板圖層的話 代表正在踩著地板
    bool IsGround
    {
        get
        {
            Vector2 start = groundCheck.position;
            Vector2 end = new Vector2(start.x, start.y - distance);

            Debug.DrawLine(start, end, Color.blue);
            grounded = Physics2D.Linecast(start, end, groundLayer);
            return grounded;
        }
    }

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

    /// <summary>水平移動</summary>
    void MovementX()
    {
        horizontalDirection = Input.GetAxis(HORIZONTAL);
        playerRigidbody2D.AddForce(new Vector2(xForce * horizontalDirection, 0));
    }

    void Update()
    {
        MovementX();
        ControlSpeed();
        TryJump();
        //speedX = playerRigidbody2D.velocity.x;
    }
}

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

2017年1月11日 星期三

Unity 2D Effectors 力場教學:2D平台, 橫向捲軸, 風吹, 瀑布, 漂浮, 黑洞, 爆炸, 輸送帶,等速移動 效果

Unity 5.5 2D Effectors 力場教學:2D平台, 橫向捲軸, 風吹, 瀑布, 漂浮, 黑洞, 爆炸, 輸送帶,等速移動 效果

影片內容介紹了以下 Effector (中文名稱皆為自己翻譯 並非通用譯名)

Constant Force 2D ( 常數推力 )
功用:讓物件等速移動



Area Effector 2D ( 區域力場 )
功用:類似風吹或瀑布有特定方向的力場




Buoyancy Effector 2D ( 漂浮力場 )
功用:水池的效果


Point Effector 2D ( 點狀力場 )
功用:黑洞或是爆炸般的效果


Surface Effector 2D ( 表面力場 )
功用:輸送帶或迴轉壽司轉盤的效果


Platform Effector 2D ( 平台力場  )
功用:可以設定跳上某平台以後就下不來,或是特定角度才跳得上去




影片中使用到的專案下載
https://goo.gl/fE1O4O

2016年5月27日 星期五

獨立遊戲網站推薦



http://gamejolt.com
遊戲多到漫出來啦!!!

這個網站有中文介面
首頁的最下方可以選擇語言

=======
https://itch.io/
很癢的網站...(疑?)

我好奇為什麼取名叫itch?itch不是很癢的意思嗎...

除了很多免費遊戲之外,還有遊戲素材可以使用,部分是免費的

========


https://indiegamestand.com/free-games/
這個網站也有很多免費的獨立遊戲