我有一個箱子的櫥櫃。當我在箱子上看,然後按下鼠標按鈕,我想打開/關閉它翻譯。我想移動框,直到它是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