-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
選擇物件/Inspector視窗/點選Add Component/Physics2D/Rigidbody2D or Box Collider2D |
Circle Collider2D : 圓形 |
Freeze Rotation用來防止指定軸心轉動 |
使用Physics來移動物件 暫記(4_9)不知道之前其他專案設地圖的collision邊線會失效是不是這個原因 |
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);
}
}
|
使用Physics做衝突判斷就不需要另外指定衝突判定的演算法 Physics的衝突判定有Collision(衝突模式)和Trigger(穿透模式)兩種 Trigger模式回顧 : 角色與告示牌的碰撞事件 private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
cam.minPosition += cameraChange;
cam.maxPosition += cameraChange;
other.transform.position += playerChange;
if(needText)
{
StartCoroutine(placeNameCo());
}
}
} |
|
讓物件做出物理性的動作
包含Rigidbody 和 Collider 兩種物件
The text was updated successfully, but these errors were encountered: