我的游戏场景中有一个 Cube(Player)。我编写了一个 C# 脚本来约束Mathf.Clamp()
Cube 的移动(使用),使其不会离开屏幕。
以下是我FixedUpdate()
的脚本方法
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3(
Mathf.Clamp (rb.position.x, x_min, x_max),
0.5f,
Mathf.Clamp(rb.position.z, z_min, z_max)
);
}
x_min
, x_max
, z_min
,的值z_max
分别是通过统一检查器输入的 -3, 3, -2, 8。
问题
脚本运行良好,但我的播放器(立方体)最多可以负向移动 -3.1 个单位X-AXIS
(如果我一直按向左箭头按钮),负向移动多 0.1 个单位X-AXIS
(这种行为也适用于其他轴)。当我停止按下按钮时,它显然会夹住 -3.1 到 -3。
- 为什么会这样?为什么 it(
Mathf.Clamp()
) 首先不将 Cube 限制为 -3 个单位? - 为什么我会得到额外的 0.1 个单位?
- 为什么这个值只有 0.1 个单位?为什么不多或少?