2016-09-19 428 views
3

最近,我正在制作一个国际象棋游戏,其中国际象棋移动时有动画。我使用了两种方法,RotateTowards和Translate()。 RotateTowards()只能在Update()函数中运行。这里是我的代码:如何在Update()函数完成后停止方法? - C# - Unity3D

using UnityEngine; 
using System.Collections; 

public class OnTouch : MonoBehaviour { 

    public GameObject cube; 
    public GameObject sphere; 
    int clicked; 
    Quaternion lastRot; 

    // Use this for initialization 
    void Start() { 
     clicked = 0; 
    } 

    // Update is called once per frame 
    void Update() { 
    if(clicked == 1) 
     { 
      var rotation = Quaternion.LookRotation(cube.transform.position - transform.position); 
      print(rotation); 
      rotation.x = 0; 
      rotation.z = 0; 
      cube.transform.rotation = Quaternion.RotateTowards(cube.transform.rotation, rotation, 100 * Time.deltaTime); 
      if(cube.transform.rotation = ???? <- No Idea){ 

       clicked = 0; 
      } 
     } 
    } 

    void OnMouseDown() 
    { 
     print("clicked"); 
     clicked = 1; 
    } 
} 

我将该代码附加到所有国际象棋瓷砖。所以,立方体停止旋转后,我试图点击另一个瓷砖。但是,之前的RotateTowards()方法仍在运行,所以它同时运行。我试图做IF逻辑,但我不知道

if(cube.transform.rotation == ??? <-- No idea){ 

clicked = 0; 

} 

任何解决方案?谢谢!

+0

您可以考虑尝试如LeanTween它是Asset Store上可用的免费资产。这是简单而强大的工具。有了这个assset,从点到点的翻译​​变得更容易,因为不需要在Update()中调用它。 –

+0

@marecki BTw,谢谢。我会在稍后找到它。 – Sena

回答

1
if (cube.transform.rotation == rotation) 
+0

我试过了。但它没有奏效。我试图在update()函数中打印(点击),但它保持打印1,1和1 ...它没有改变,谢谢。 – Sena

1

它永远不会达到你最终的旋转,它会变得非常接近,虽然这样你就可以检查你的目的地旋转和当前旋转的角度大于很小的程度较小,那么你可以说达到了。

float DestAngle = 1.0f; //here we take 1 degree as the near enough angle 
    float angle = Quaternion.Angle(transform.rotation, target.rotation);//we calculate the angle between current rotaion and destination 
    if (Mathf.Abs (angle) <= DestAngle) { //we reached } 
+0

真的非常感谢。我会稍后再尝试。那么,我是一个学生,有一点时间;) – Sena

相关问题