c# - 我的推动机制取决于帧速率
问题描述
我的 CharacterController 与刚体交互时遇到问题。它有点工作,但只有 60 fps 并且当我打开垂直同步或帧速率低/非常高时会出现奇怪的行为。我有一些用于 CharacterController 和刚体之间交互的代码,并且我创建了一个名为 PushStates 的函数,它被加载到 Update 函数中。
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if (hit.moveDirection.y < -.3f)
return;
Vector3 pushDirection = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushForce * pushDirection * Time.deltaTime;
}
在这里,我有一些与我的推送机制相关的代码。另一件尚未奏效的事情是当玩家停止移动时从刚体中移除所有力。这会导致 pushableObject 仍然远离玩家几厘米,我也不确定它为什么不起作用。
public void PushStates() {
// Creating the raycast origin Vector3's
Vector3 forward = transform.TransformDirection(Vector3.forward) * distanceForPush;
Vector3 middle = controller.transform.position - new Vector3(0, -controller.height / 2, 0);
// Inspector bool
if (pushRay)
{
Debug.DrawRay(middle, forward, Color.cyan);
}
// Force the pushForce and movementSpeed to normal when the player is not pushing
pushForce = 0f;
movementSpeed = walkSpeed;
// Draws a raycast in front of the player to check if the object in front of the player is a pushable object
if (Physics.Raycast(middle, forward, out hit, distanceForPush))
{
if (InputManager.BButton() && playerIsInPushingTrigger)
{
PushableInfo();
playerIsPushing = true;
anim.SetBool("isPushing", true);
if (hit.collider.tag == "PushableLight")
{
pushForce = playerPushForceLight;
movementSpeed = pushSpeedLight;
}
else if (hit.collider.tag == "PushableHeavy")
{
pushForce = playerPushForceHeavy;
movementSpeed = pushSpeedHeavy;
}
// Checks the players speed now instead off movement. This is neccesary when the player is pushing a pushable into a collider.
// The player and pushable never stop moving because of force.
if (currentSpeed < 0.15f)
{
//Removes all remaining velocity, when the player stops pushing
pushableObjectRB.velocity = Vector3.zero;
pushableObjectRB.angularVelocity = Vector3.zero;
anim.SetFloat("pushSpeedAnim", 0f);
}
else
{
// Calls a rotation method
PushingRot();
if (hit.collider.tag == "PushableLight")
{
anim.SetFloat("pushSpeedAnim", pushSpeedAnimLight);
}
else if (hit.collider.tag == "PushableHeavy")
{
anim.SetFloat("pushSpeedAnim", pushSpeedAnimHeavy);
}
}
}
else
{
anim.SetBool("isPushing", false);
pushForce = 0f;
movementSpeed = walkSpeed;
playerIsPushing = false;
}
}
else
{
anim.SetBool("isPushing", false);
playerIsPushing = false;
}
// Setting the time it takes to rotate when pushing
AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
if (stateInfo.fullPathHash == pushStateHash)
{
turnSmoothTime = maxTurnSmoothTimePushing;
}
else
{
turnSmoothTime = 0.1f;
}
}
解决方案
从刚体的速度中删除 Time.deltaTime 并更改检查器中的一些设置,即(pushForce,pushSpeed)解决了我的问题。
推荐阅读
- python - 使用 os.path.exists 和 assert 检查除了定义的列表之外是否存在其他文件夹
- javascript - 如何倒计时?| X天,X小时过去了
- c# - 字符串中可空枚举的扩展方法
- apache-flink - Kinesis Data Analytics Flink:不断增加的检查点大小
- reactjs - 上下文中的 useState 挂钩重置未聚焦的输入框
- python - 使用 %s 的 MySQL 连接器/python 困难
- kubernetes - 如何将 Kubernetes 注释写入底层 YAML 文件?
- ms-access - 无法将 Access DB 导入 SQL Server Migration Assistant for Access(32 位)
- r - 如何在 dplyr %>% 运算符之后使用自定义函数?
- ruby-on-rails - “确保提高 ActiveRecord::Rollback”到底是做什么的?