顯示具有 Rigibody 2D 標籤的文章。 顯示所有文章
顯示具有 Rigibody 2D 標籤的文章。 顯示所有文章

2019年6月22日 星期六

Unity 學習筆記 - 根據觸碰點施加不同方向的力


直接上程式碼

private Rigidbody2D m_rigidbody2D;

// Use this for initialization
    void Start () {
        m_rigidbody2D = GetComponent<Rigidbody2D>();
    }

private void FixedUpdate()
    {
        if (Input.touchCount > 0 && flag)
        {
            // 獲取觸碰點的座標(此為螢幕座標)
            float x_touch_position = Input.GetTouch(0).position.x;
            float y_touch_position = Input.GetTouch(0).position.y;

            // 世界座標轉換為螢幕座標 ()
            Vector3 tempPosition = Camera.main.WorldToScreenPoint(m_rigidbody2D.transform.position);

            // 欲施加的 X 方向向量
            float vx = x_touch_position - tempPosition.x;

            // 欲施加的 Y 方向向量
            float vy = y_touch_position - tempPosition.y;

            if (vx >=0 && vy>=0)
            {
                myAngle = Vector2.Angle(new Vector2(0, 1), new Vector2(vx, vy));
                m_rigidbody2D.rotation = 90f - myAngle;
                m_rigidbody2D.AddRelativeForce(Vector2.right * speed);
                flag = false;
            }
            else if (vx < 0 && vy >= 0)
            {
                myAngle = Vector2.Angle(new Vector2(0, 1), new Vector2(vx, vy));
                m_rigidbody2D.rotation = 90f + myAngle;
                m_rigidbody2D.AddRelativeForce(Vector2.right * speed);
                flag = false;
            }
        }
    }

2019年6月16日 星期日

Unity 學習筆記 - 設置手機螢幕邊框的透明圍牆


1. 新建立GameObject ,並重新命名

2. 結構如下,位置擺在座標(0,0,0)即可,將會用腳本另外控制所在位置

3. WallManager下掛在新建之腳本WallManager.cs

4. 腳本內容
public class WallManager : MonoBehaviour {

    private BoxCollider2D rightWall;
    private BoxCollider2D leftWall;
    private BoxCollider2D upWall;
    private BoxCollider2D downWall;

    // Use this for initialization
    void Start () {
        ResetWall();
    }
    
     // Update is called once per frame
     void Update () {
         
     }

    void ResetWall()
    {
        // 定義螢幕外的圍牆位置
        rightWall = transform.Find("rightWall").GetComponent<BoxCollider2D>();
        leftWall = transform.Find("leftWall").GetComponent<BoxCollider2D>();
        upWall = transform.Find("upWall").GetComponent<BoxCollider2D>();
        downWall = transform.Find("downWall").GetComponent<BoxCollider2D>();

        Vector3 tempPosition = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
        upWall.transform.position = new Vector3(0, tempPosition.y + 0.5f, 0);
        upWall.size = new Vector2(tempPosition.x * 2, 1);
        downWall.transform.position = new Vector3(0, -tempPosition.y - 0.5f, 0);
        downWall.size = new Vector2(tempPosition.x * 2, 1);
        leftWall.transform.position = new Vector3(-tempPosition.x - 0.5f, 0, 0);
        leftWall.size = new Vector2(1, tempPosition.y * 2);
        rightWall.transform.position = new Vector3(tempPosition.x + 0.5f, 0, 0);
        rightWall.size = new Vector2(1, tempPosition.y * 2);
    }
}

2019年6月15日 星期六

Unity 學習筆記 - 彈球屬性設置(2D)


欲設置一顆會不斷反彈的彈球,屬性設置如下:

1. 添加 Rigibody 2D Circle Collider 2D 組件

-- Rigibody 2D組件中,將Gravity Scale (重力)調整為 0 ,如此才不會受到重力影響

-- Circle Collider 2D組件中,添加 Material 材質




[Android][App] Pin Poke Challenge 隱私權保護政策

非常歡迎您使用「 Pin Poke Challenge 」,為了讓您能夠安心使用此 App 的各項服務與資訊,特此向您說明 Pin Poke Challenge的隱私權保護政策,以保障您的權益,請您詳閱下列內容: 一、隱私權保護政策的適用範圍 隱私權保護政策內容,包括...