我正在 Unity 中制作 SpaceShooter 游戏。
这是功能。
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Player.GetComponent<Rigidbody>().velocity=movement*speed;
Player.GetComponent<Rigidbody> ().rotation = Quaternion.Euler(0.0f,0.0f,Player.GetComponent<Rigidbody> ().velocity.x * -tilt);
}
在上面的代码中,当我添加 Clamp 函数以将 Ship 限制在边界内时,Ship 的大小从侧面减小。这是减小船体尺寸的夹紧功能代码。
Player.GetComponent<Rigidbody> ().position=new Vector3
(
Mathf.Clamp(Player.GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(Player.GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
我已经在 Clamping 函数中准确地给出了屏幕边界值。