unity3d - 如何为统一 2D 角色控制器添加跳跃延迟?
问题描述
这是我的字符控制器代码。
public float speed;
float Velocity;
public float jump;
void Update ()
{
Velocity = 0;
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
{
Velocity = -speed;
}
if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
{
Velocity = speed;
}
if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey("space") || Input.GetKey (KeyCode.W))
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x,
jump);
}
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D>
().velocity.y);
}
}
这是一个非常简单的运动代码,这是我从 Brackeys 获得的凸轮跟随代码 我的代码存在无限跳转问题,我不知道如何修复它我不知道正确的语法来阻止它。我正在使用 Unity 2019.4.2f1 和 VScode 2020。我不确定添加我使用的版本是否会有所帮助,但我总是收到编译器错误,说当前上下文中不存在这些东西,但我不知道其他任何东西确实存在于将起作用的当前上下文。
void Update()
{
transform.position = playerPos.position + offset;
}
解决方案
要修复无限跳跃,请使用Input.GetKeyDown(KeyCode.UpArrow)
代替Input.GetKey(KeyCode.UpArrow)
. GetKey 函数用于检查何时按下某个键。GetKeyDown 函数用于获取第一次按下键的时间。
推荐阅读
- sql - 在 SQL Oracle 中计算百分比
- javascript - Next.js docker image set env 在运行时而不是在构建
- python - 硒日志隐藏在python中
- excel - VBA中的刷新查询自动移动到查询表所在的工作表
- unity3d - DllNotFoundException:agoraSdkCWrapper 在 macOS 上的 Unity 中使用 Agora
- python - 未能实现客户端流式传输到服务器 Python 代码
- multithreading - 在不同线程内的两个 tokio 运行时之间的消息传递
- python - mysql 连接器无法处理参数(错误)
- php - 为什么 C:\ 和 C:\xampp 中有 2 个 php 目录
- xcode - 如何在 menuPickerStyle 中将 CoreData 中的名称列表与 Picker 一起使用