如何调整Unity 2D的跳跃速度?



我目前正在跟随Jason Weimann的YouTube教程,学习如何在Unity中制作2D flappy bird游戏。到目前为止,一切都很顺利,直到你编写代码让玩家跳跃并改变跳跃速度的部分。玩家可以跳得很好,但我不知道如何改变速度。我试着在视频下评论寻求帮助,但还没有得到回复。我是编程新手,对c#了解不多。下面是我到目前为止的代码:

公共类Player: MonoBehaviour{private Vector2 jumpVelocity;

// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
GetComponent<Rigidbody2D>().velocity = jumpVelocity;
}
}

}

任何帮助都是感激的!

你需要熟悉c#编程。

public class Player : MonoBehaviour { 
[SerializeField] // now you can set it from Inspector for Player script
private Vector2 jumpVelocity;

void Awake() {
jumpVelocity = new Vector2(0f, 1f); // or set the values for x n y axes in code
}
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
GetComponent<Rigidbody2D>().velocity = jumpVelocity;
}
}
}

2D中的速度是一个矢量单位,它有X和Y轴如果你给浮点变量赋值<没有定义>对于Vector2,它将导致错误。你需要将它分配给一个Vector2。这样的

GetComponent<Rigidbody2D>().velocity = new Vector2(0f, 1f);

或者像上面的答案一样声明一个Vector2 jumpVelocity,然后改变

new Vector2(0f, 1f)jumpVelocity

最新更新