Comments (7)
選擇物件/Inspector視窗/點選Add Component/Physics2D/Rigidbody2D or Box Collider2D
from jump.
Circle Collider2D : 圓形
Box Collider2D : 長方形
Edge Collider2D : 線型。用於局部衝突判斷
Polygon Collider2D : 多邊形。用於完全符合物件輪廓的衝突判斷
from jump.
Freeze Rotation用來防止指定軸心轉動
Rigidbody2D/Constraints/Freeze Rotation/Z
from jump.
使用Physics來移動物件
並不是給予變更物件的座標,而是給予力量
如果直接變更物件座標,會影響衝突判斷的正確性
因此,只要給予角色一點力量,Physics就會自動運算之後的動作
暫記(4_9)不知道之前其他專案設地圖的collision邊線會失效是不是這個原因
導致Player可以走進去,然後collision又起作用,Player就出不來了
from jump.
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」倍
from jump.
使用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());
}
}
}
from jump.
狀態 | Collision模式 | Trigger模式 |
---|---|---|
衝突發生瞬間 | OnCollisionEnter2D | OnTriggerEnter2D |
衝突發生期間 | OnCollisionStay2D | OnTriggerStay2D |
衝突結束瞬間 | OnCollisionExit2D | OnTriggerExit2D |
from jump.
Related Issues (4)
- Mecanim HOT 6
- 場景切換比較 HOT 2
- Input.acceleration的判讀
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jump.