【发布时间】:2020-07-22 10:41:07
【问题描述】:
我试图让我的播放器在按下空格键时跳跃。我的角色确实会跳跃,但是当我保持空间时,它会一直在空中上升。我确实知道我的代码有什么问题,只是我不知道如何解决它。
这是我的代码:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 500f;
public float sidewaysForce = 500f;
public float jumpForce = 200f;
// FixedUpdate is called once per frame
void FixedUpdate()
{
// Let the player have the ability to move around using wasd keys and jump using the space key
if (Input.GetKey("w"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -forwardForce * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(0, jumpForce * Time.deltaTime, 0);
}
}
}
任何建议都会很有帮助。
【问题讨论】:
-
“我知道我的代码有什么问题”——你知道吗?如果是这样,那么你应该在你的帖子中解释你已经知道代码有什么问题。这将帮助其他人以专门针对您需要帮助的部分的方式提供专门针对您正在处理的问题的答案。如果没有这些信息,问题就会变得非常广泛。