软件编程
位置:首页>> 软件编程>> C#编程>> Unity实现鼠标或者手指点击模型播放动画

Unity实现鼠标或者手指点击模型播放动画

作者:liang_704959721  发布时间:2023-11-30 23:06:01 

标签:unity,鼠标,播放

本文实例为大家分享了unity鼠标或者手指点击模型播放动的具体代码,供大家参考,具体内容如下


using UnityEngine;
using System.Collections;

public class ClickPlayAnimation : MonoBehaviour {
 /// <summary>
 /// 实现功能为点击模型播放动画
 /// 使用方法,给模型添加碰撞,添加脚本
 /// </summary>

bool isPlayAnim = false;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
   //animation.Play();
   if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
   {
      foreach (Touch touch in Input.touches)
     {
       if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved)
       {
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
         RaycastHit hit;

if (Physics.Raycast(ray, out hit))
         {
           // Debug.DrawLine(ray.origin, hit.point);
           if (hit.collider.gameObject.name == gameObject.name)
           {
             isPlayAnim = true;
             print("123");
           }
         }
       }
     }
   }
   else
   {
     if (Input.GetMouseButtonDown(0))
     {
       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       RaycastHit hit;

if (Physics.Raycast(ray, out hit))
       {
         Debug.DrawLine(ray.origin, hit.point);
         //print(hit.collider.gameObject.name);

//curObject = hit.collider.gameObject;
         if (hit.collider.gameObject.name == gameObject.name)
         {
           isPlayAnim = true;
           print("123");
         }
         // 显示当前选中对象的名称
        // print(hit.collider.gameObject);
       }

}
   }

if(isPlayAnim)
   {
     animation.Play();
     isPlayAnim = false;
   }

}

}

Unity实现鼠标或者手指点击模型播放动画

来源:https://blog.csdn.net/liang_704959721/article/details/45830953

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com