c# - 统一流畅的运动
问题描述
我开始学习团结,我面临着一个我无法摆脱的问题,那就是:但是我试图让动作变得流畅,它不像通常的视频游戏,无论 FPS 有多高或有多少不同我尝试实现逻辑的方式。
我尝试使用固定更新和固定增量时间,但似乎没有什么不同。
void Update()
{
movement = Input.GetAxis("Horizontal");
if ((movement > 0 && lookingLeft) || (movement < 0 && !lookingLeft))
{
lookingLeft = !lookingLeft;
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
if (noOfJumps > 0 || jumping)
jumpInput = Input.GetAxis("Jump");
if (jumpInput > 0 && !jumping)
{
pressTime = 0;
jumping = true;
noOfJumps--;
}
else if (jumpInput == 0)
jumping = false;
else
pressTime += Time.deltaTime;
if (pressTime > 0.15f)
jumpInput = 0;
}
private void FixedUpdate()
{
rd.velocity = Vector2.Lerp(rd.velocity, new Vector2(movement != 0 ? speed *
movement * Time.fixedDeltaTime : rd.velocity.x * 0.95f, (jumpInput != 0) ? jumpInput * jumpSpeed * Time.fixedDeltaTime : -1), 0.9f);
}
解决方案
直接分配给速度可以覆盖每帧的某些计算。最好用于AddForce
避免由于重力和摩擦导致的压倒性变化。
让Input.GetAxis
投入movement
的平滑为你做平滑。只需将该值乘以速度即可获得新的水平速度。
此外,您正在更改速度,因此您不需要将速度场乘以Time.fixedDeltaTime
。
private void FixedUpdate()
{
float newVerticalVelocity = rd.velocity.y + jumpInput * jumpSpeed;
Vector2 velocityChange = newVerticalVelocity * Vector2.up
+ movement * speed * Vector2.right
- rd.velocity;
rd.AddForce(velocityChange, ForceMode.VelocityChange);
}
推荐阅读
- php - 在 iframe 中运行 Laravel Web App 时出现错误 419 Session Expired
- svelte - Sveltstrap 中的表单验证
- go - Go 中的预处理器指令与 C 中的一样
- python - 如何左对齐数据框
- powershell - 如何使用正确解析的参数从 powershell 启动新的 powershell 窗口
- java - 为什么我不断收到输入不匹配异常错误
- reactjs - 如何在 React js 中获取实时年份日期?
- python - 尝试在 Jupyter Notebook 上将 pdf 转换为 csv 时出现类型错误
- python - 如何根据基本公式获取值字符串?
- javascript - 从节点 js 服务器向客户端发送文件时不允许加载本地资源