【发布时间】:2023-03-14 09:28:01
【问题描述】:
或者我应该说,“有效地禁用对角线运动”。
网上有很多关于这个的Q/As,但我一直遇到同样的问题:水平移动时(例如向左),我可以覆盖当前方向并开始垂直移动(通过向上推),即我想要的是。但反过来就不行了!垂直移动不能覆盖水平移动。
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
ManageMovement(h, v);
}
void ManageMovement(float horizontal,float vertical) {
if (vertical != 0f) {
horizontal = 0f;
Vector3 movement = new Vector3 (horizontal, vertical, 0);
GetComponent<Rigidbody2D> ().velocity = movement * speed;
return;
}
if (horizontal != 0f) {
vertical = 0f;
Vector3 movement = new Vector3 (horizontal, vertical, 0);
GetComponent<Rigidbody2D> ().velocity = movement * speed;
return;
} else {
Vector3 noMovement = new Vector3 (0, 0, 0);
GetComponent<Rigidbody2D> ().velocity = noMovement;
}
}
如果我颠倒这些 if() 语句的顺序,它就会颠倒问题。所以,这是一个线索。但我不是一个伟大的侦探。我希望得到一些帮助!
【问题讨论】: