Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Physics物理引擎 #1

Open
tzutzu858 opened this issue Apr 9, 2020 · 7 comments
Open

Physics物理引擎 #1

tzutzu858 opened this issue Apr 9, 2020 · 7 comments

Comments

@tzutzu858
Copy link
Owner

讓物件做出物理性的動作
包含Rigidbody 和 Collider 兩種物件

@tzutzu858
Copy link
Owner Author

選擇物件/Inspector視窗/點選Add Component/Physics2D/Rigidbody2D or Box Collider2D

@tzutzu858
Copy link
Owner Author

Circle Collider2D : 圓形
Box Collider2D : 長方形
Edge Collider2D : 線型。用於局部衝突判斷
Polygon Collider2D : 多邊形。用於完全符合物件輪廓的衝突判斷

@tzutzu858
Copy link
Owner Author

Freeze Rotation用來防止指定軸心轉動
Rigidbody2D/Constraints/Freeze Rotation/Z

@tzutzu858
Copy link
Owner Author

tzutzu858 commented Apr 9, 2020

使用Physics來移動物件
並不是給予變更物件的座標,而是給予力量
如果直接變更物件座標,會影響衝突判斷的正確性
因此,只要給予角色一點力量,Physics就會自動運算之後的動作

暫記(4_9)不知道之前其他專案設地圖的collision邊線會失效是不是這個原因
導致Player可以走進去,然後collision又起作用,Player就出不來了

@tzutzu858
Copy link
Owner Author

tzutzu858 commented Apr 9, 2020

複習:元件(components)

void Start()
    {
        this.rigid2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // 跳躍
        if (Input.GetKeyDown(KeyCode.Space))
        {
            this.rigid2D.AddForce(transform.up * this.jumpForce);
        }

        // 左右移動
        int key = 0;
        if (Input.GetKey(KeyCode.RightArrow)) key = 1;
        if (Input.GetKey(KeyCode.LeftArrow)) key = -1;

        // 遊戲角色的速度
        float speedx = Mathf.Abs(this.rigid2D.velocity.x);

        // 速度限制
        if (speedx < this.maxWalkSpeed)
        {
            this.rigid2D.AddForce(transform.right * key * this.walkForce);
        }

        // 依照行進方向翻轉
        if(key != 0) {
            transform.localScale = new Vector3(key, 1, 1);
        }
    }
  • transform.up * jumpForce

    =(0, jumpForce , 0)
    
  • 速度限制:每個影格使用AddForce方法增加力量,可以使角色加速前進
    就像是踩著汽車油門,慢慢加速
    當角色移動速度大於指定的最高限速(maxWalkSpeed)時,需要減少力量來調整速度

  • 翻轉角色的圖片,要將圖片X方向的擴大率設為「-1」倍

@tzutzu858
Copy link
Owner Author

使用Physics做衝突判斷就不需要另外指定衝突判定的演算法
當帶有Collider元件彼此衝突時,Physics可以自動做衝突判定

Physics的衝突判定有Collision(衝突模式)和Trigger(穿透模式)兩種
Collision模式不只可以做判別衝突,也可以執行如往後跳等衝突回應
Trigger模式則只能做判斷,並無執行衝突回應(發生衝突會直接穿透過去)

Trigger模式回顧 : 角色與告示牌的碰撞事件
Trigger模式回顧 : Test專案地圖場景轉換,當Player碰到colllider,cameraChange x值-27.66
x值在Inspector視窗調

private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            cam.minPosition += cameraChange;
            cam.maxPosition += cameraChange;
            other.transform.position += playerChange;
            if(needText)
            {
                StartCoroutine(placeNameCo());
            }

        }
    }

@tzutzu858
Copy link
Owner Author

tzutzu858 commented Apr 10, 2020

狀態Collision模式Trigger模式
衝突發生瞬間OnCollisionEnter2DOnTriggerEnter2D
衝突發生期間OnCollisionStay2DOnTriggerStay2D
衝突結束瞬間OnCollisionExit2DOnTriggerExit2D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant