如何通过在UNITY 5.5中触摸模型来触发模型的动画

时间:2017-02-15 19:20:32

标签: animation unity3d touch

我是UNITY的新手。这是一个简单的问题,但我无法做到这一点...... 我在动画和动画师的场景中有模特。模型的默认状态是IDLE。当我在手机屏幕上触摸该模型时,我希望它转到移动状态。 当我触摸屏幕上的任何地方时,我能够为此设置动画...但我希望它能够播放"移动"当我在场景中触摸特定的3D对象/模型时动画。这是我的代码:

void Start () {
    ani = GetComponent<Animator>();
}
void Update() {
//FOR MOUSE
//if (Input.GetMouseButtonDown (0)) {
//      Debug.Log ("mouse button was pressed");
//      ani.Play ("moving",-1,0f);
//FOR MOBILE
  if (Input.touchCount == 1) {
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Began) {
        ani.Play ("moving", -1, 0f);        
    }

我在Model上附上了这个脚本。我尝试了不同的方法,但没有任何工作:

 //NOT WORKING
    //if(Input.GetMouseButton(0))
    //      {
    //          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    //          RaycastHit hit;
    //
    //          if(Physics.Raycast(ray, out hit, 100))
    //          {
    //              ani = hit.collider.GetComponent<Animator>();
    //              ani.Play("moving",-1,0f);
    //          }

// NOT WORKING
//if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
//      {
//          RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
//          if(hit.collider != null)
//          {
//              Debug.Log ("Touched it");
//              ani.Play ("moving", -1, 0f);
//          }
//  }

我知道这是小代码...请告诉我哪里错了?

0 个答案:

没有答案