【发布时间】:2022-12-05 22:16:25
【问题描述】:
我在 Unity (2D) 中实现了以下教程,试图创建一个绳索摇摆平台游戏:https://gamedevelopment.tutsplus.com/tutorials/swinging-physics-for-player-movement-as-seen-in-spider-man-2-and-energy-hook--gamedev-8782
void FixedUpdate()
{
Vector2 testPosition = playerRigidbody.position + playerRigidbody.velocity * Time.deltaTime;
Hooked(testPosition);
}
private void Hooked(Vector2 testPosition)
{
Vector2 t = new Vector2(tetherPoint.position.x, tetherPoint.position.y);
Debug.DrawLine(tetherPoint.position, playerRigidbody.position);
float currentLength = (testPosition - t).magnitude;
if (currentLength < tetherLength)
{
currentLength = (playerRigidbody.position - t).magnitude * Time.deltaTime;
}
else
currentLength = tetherLength;
if ((testPosition - t).magnitude > tetherLength)
{
Vector2 x = (testPosition - t).normalized;
testPosition = new Vector2(x.x * currentLength, x.y * currentLength);
playerRigidbody.velocity = (testPosition - playerRigidbody.position) * Time.deltaTime;
playerRigidbody.position = testPosition;
}
}
它似乎在向下摆动时功能正常,但当玩家开始向上移动时,他们会卡在空中漂浮并且不会掉到弧线的中间。即使从高处掉落,秋千也不会在另一侧将它们推得很高。
当前的半工作解决方案将速度变化乘以 deltaTime,而教程说要除以,但是将“*”更改为“/”只会导致播放器不受控制地弹跳。
我试过检查游戏过程中变量的变化,但我就是想不通为什么它不能正常工作。我认为问题出在我对 C# 的原始伪代码的解释中。
之前已经问过关于同一教程的另一个问题,但不幸的是,用户的实现与我的非常不同:Game rope swing physics acting weird
编辑:自发布以来,我已经更新了代码以使用 AddForce 和 MovePosition,但它仍然是一样的。
playerRigidbody.AddForce((testPosition - playerRigidbody.position) * Time.deltaTime);
playerRigidbody.MovePosition(testPosition);
【问题讨论】:
标签: unity3d game-physics game-development