我有一个箱子的橱柜。当我在箱子上看,然后按下鼠标按钮,我想打开/关闭它翻译。我想移动框,直到它是X坐标将是1.0(并且起始点是1.345)。但它移动的时间比这点长。如何在需要时停止翻译?
我试图用FixedUpdate,但它并没有帮助..
public LayerMask mask;
private bool shouldClose;
private bool changeXCoordinate;
private Transform objectToMove;
void Update()
{
if (changeXCoordinate)
OpenCloseBox();
else if(DoPlayerLookAtCupboardBox() && Input.GetMouseButtonDown(0))
changeXCoordinate = true;
}
bool DoPlayerLookAtCupboardBox()
{
RaycastHit _hit;
Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
bool isHit = Physics.Raycast(_ray, out _hit, 1.5f, mask.value);
if (isHit && !changeXCoordinate)
{
objectToMove = _hit.transform;
return true;
}
else
return false;
}
void OpenCloseBox()
{
if (shouldClose)
{
if(objectToMove.position.x != 1.345f) // It must stop at this point, but it don't
{
changeXCoordinate = false;
shouldClose = !shouldClose;
}
else
objectToMove.Translate(Vector3.right * Time.deltaTime);
}
else
{
if (objectToMove.position.x >= 0.1f) // The same problem here..
{
changeXCoordinate = false;
shouldClose = !shouldClose;
}
else
objectToMove.Translate(Vector3.left * Time.deltaTime);
}
}
1.345很可能是1.33599999,这是不一样的。所以它永远不会平等。也是迪马说'if(objectToMove.position.x> = 0.1f)//同样的问题在这里' –
是的,我编辑考虑这个问题。动画对象时,我不会推荐使用这个原则。 – Everts
我试图使用动画,但有一些问题。但现在它工作正常。无论如何感谢大家的帮助=) – dima