我发现在调试浮点数和Unity中的内容时,有时值会关闭。即我有一个称为currentSpeed的浮点数,当玩家移动时,这是一个值,而当他不在时,它是0。即1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1。我发现这种情况发生在多个值和向量上。奇怪的是,当我的帧率降低时,它不会发生(即,在检查员中选择播放器时,帧率从60下降到48)。我正在使用Unity 2018.3,不知道如何解决此问题。我希望有人能帮助我。
编辑(添加的代码):
void Move(Vector2 inputDir, bool running)
{
if (inputDir != Vector2.zero)
{
// When the player is not pushing we can create a moving direction
if (!playerIsPushing)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
}
}
float targetSpeed = ((running) ? runSpeed : movementSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
velocityY += Time.deltaTime * gravity;
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
// Here we cap the players velocity to a maximum speed. In this case the runSpeed. This means the player can never exceed the runSpeed.
if (velocity.x > runSpeed)
{
velocity.x = runSpeed;
}
if (velocity.x < -runSpeed)
{
velocity.x = -runSpeed;
}
if (velocity.z > runSpeed)
{
velocity.z = runSpeed;
}
if (velocity.z < -runSpeed)
{
velocity.z = -runSpeed;
}
controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
// Debugging the players velocity
Debug.Log(currentSpeed);
float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / movementSpeed * .5f);
编辑2:我测试了更多内容,发现我的推力不是独立于帧率的。在游戏中推入一个对象时,我在调试时看到了奇怪的值,即1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1 。我正在使用CharacterController,因此可以通过脚本为刚体添加力。
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;
}
这可能是奇怪值的问题吗?