我為 2d 角色制作了一個控制器,我修復(fù)了玩家在墻上抖動的問題,但我無法修復(fù)玩家在地面上抖動的問題。從多個教程和調(diào)整內(nèi)容中獲取了此代碼。如果您有任何提示,我們將不勝感激,我是這方面的新手,正在開發(fā)我的第一個游戲(因為我放置了太多代碼而不得不閑逛),謝謝。{ public float speed, height; Rigidbody2D rb; private bool horizontalRight = false; private bool horizontalLeft = false; private bool verticalMove = false; private void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { if (Input.GetAxisRaw("Horizontal") > 0) { horizontalRight = true; } else if (Input.GetAxisRaw("Horizontal") < 0) { horizontalLeft = true; } if (Input.GetButton("Jump") && rb.velocity.y == 0) { verticalMove = true; } } private void FixedUpdate() { if (horizontalRight == true) { transform.Translate(Vector2.right * Time.deltaTime * speed); horizontalRight = false; } else if (horizontalLeft == true) { transform.Translate(Vector2.left * Time.deltaTime * speed); horizontalLeft = false; } if (verticalMove == true) { rb.velocity = new Vector2(0, height); verticalMove = false; } }}
2 回答
斯蒂芬大帝
TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊
典型的統(tǒng)一跳躍如下所示:
void Jump()
{
if (_isGrounded == true)
{
rb.AddForce(Vector2.up * _jumpSpeed);
_isGrounded = false;
}
}
并且在碰撞事件中
void OnCollisionEnter (Collision hit)
{
_isGrounded = true;
}
這會限制你何時可以使用跳躍。
- 2 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報
0/150
提交
取消
