/// <summary> 获得需要控制物体的Transform组件 </summary>
[Header("获得需要限制运动范围的物体")]
[SerializeField]
private Transform transform1;
/// <summary> X轴范围 </summary>
[Header("限制物体的X轴范围")]
[SerializeField]
private float limitX;
/// <summary> Z轴范围 </summary>
[Header("限制物体的Z轴范围")]
[SerializeField]
private float limitZ;
// Update is called once per frame
private void Update()
{
if (limitX < Mathf.Abs(transform1.position.x) || limitZ < Mathf.Abs(transform1.position.z))
{
Vector3 temp = transform1.position;
if (temp.z > limitZ) { temp.z = limitZ; }
if (temp.z < -limitZ) { temp.z = -limitZ; }
if (temp.x > limitX) { temp.x = limitX; }
if (temp.x < -limitX) { temp.x = -limitX; }
transform1.position = temp;
}
}
设置 x 和 y 的值来限制物体的运动