unity3d - Unity:玩家离墙太近“无视”重力 [视频]
问题描述
我有一个小平台游戏,玩家可以在其中跳跃。跳跃效果很好,玩家在空中跳得很高,然后根据我设置的重力跌落下来。然而,一旦玩家越过他可能跌倒的边缘,他的物理似乎完全卡住了。他没有倒下,而是慢慢地倒下,好像他是用树叶做成的。
我发现一旦玩家撞到侧墙,也会发生这种情况。看起来一旦你越过边缘,玩家就会触摸墙壁的一侧一秒钟,这会破坏它的物理特性。
我在这里记录了这个问题:
这些是我的播放器设置:
我还尝试将 2D 物理添加到墙壁和 blayer,并将所有内容设置为 0。这确实改变了一些效果,但远未解决它......
另外,我的固定更新,我跳的地方(从更新中捕获布尔,在固定中做物理:)
void FixedUpdate()
{
currentPlayerSpeed = rb.velocity.x;
if (moveLeft)
{
rb.AddForce((Vector2.left * movementSpeed * Time.deltaTime) - rb.velocity, ForceMode2D.Force);
}
if (moveRight)
{
rb.AddForce((Vector2.right * movementSpeed * Time.deltaTime) - rb.velocity, ForceMode2D.Force);
}
if (jump)
{
if (isGrounded)
{
isGrounded = false;
rb.AddForce(Vector2.up * (jumpHeight * counterForJumpHeight) * Time.deltaTime, ForceMode2D.Impulse);
jump = false;
anim.SetBool("bool_anim_isJumping", true);
}
if (timer != null)
timer.Stop();
counterForJumpHeight = jumpMulitMin;
jumpAlreadCharging = false;
}
if (!moveLeft && !moveRight) // if no movement input is happening
{
if (isGrounded)
{
StopVelocity();
}
}
}
解决方案
好的,我想我知道当你跌倒时,你的变量moveLeft
和moveRight
变量都是假的,所以你正在调用StopVelocity
并且这个 if 语句被执行。
if (!moveLeft && !moveRight) // if no movement input is happening
{
if (isGrounded)
{
StopVelocity();
}
}
这会导致这种奇怪的行为。因为你没有跳但只有跌倒isGrounded
也是真的:)
推荐阅读
- typescript - 我可以从联合类型派生值以创建新的联合类型吗?
- python - 如何将 TensorFlow 模型导出为 .tflite 文件?
- java - 如何在活动开始时以最短时间显示大图像?
- amazon-web-services - AWS 服务器和 Lambda 中的网络可用性 SLA
- r - R:从向量的增加部分中提取频率序列(列联表)
- java - 按字母顺序正确排序数组的问题
- c# - 开发 Unity 增强现实 vuforia 应用并与原生 android 应用集成
- c# - 如何在端口号 53135 中使用 IIS Express 运行 HTTPS
- java - 在 Java 中使用 ArcGIS 要素查询服务
- oracle - PROC DATASETS 中的索引,用于 SAS 和 ORACLE 库的 PROC SQL