c# - 如何防止二维运动中的滑动
问题描述
嗨,我想移动我的角色,我环顾四周,但找不到我的代码的解决方案,当我的角色移动并且我没有按下前进按钮然后它停止但不是立即它只是在我未按下 d 按钮后立即停止这是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public Rigidbody2D rb2d;
bool ismoving;
public float speed = 5f;
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
if (Input.GetButton("Horizontal"))
{
ismoving = true;
}
else { ismoving = false; }
if (ismoving)
{
rb2d.AddForce(new Vector2(horizontal, 0) * speed * Time.fixedDeltaTime *10);
}
else
{
while((rb2d.velocity) != (rb2d.velocity = Vector2.zero))
{
rb2d.AddForce(new Vector2(horizontal, 0) * Time.fixedDeltaTime *-1);
}
}
}
}
解决方案
您可以将减速度的乘数更改为更大的值
while((rb2d.velocity) != (rb2d.velocity = Vector2.zero))
{
rb2d.AddForce(new Vector2(horizontal, 0) * Time.fixedDeltaTime * -10);
}
现在它应该比以前减速 10 倍
推荐阅读
- python - 如何在函数运行期间忽略所有用户输入?
- python - 在运行时动态设置 Python 解释器优化
- java - 带有局部变量捕获的 Java lambda 表达式
- c - if-else 结构有什么问题?
- notimplementedexception - 是否有可能解决线程“主”scala.NotImplementedError 中的异常:缺少实现?
- json - 如何从json转换字符串?
- reactjs - 为什么 React 需要另一个渲染来缓解状态更新?
- regex - 如何使用 Python3 删除括号和所有数据
- javascript - 根据谷歌表格中的 URL 查找并选择一个单元格
- javascript - 如何在 nodeJS/reactJS 中使用 database.js 文件