首页 > 编程语言 > 详细

Unity 3D物体飞到UI上的效果

时间:2020-12-30 16:20:39      阅读:132      评论:0      收藏:0      [点我收藏+]

效果如下

 代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 脚本挂在物体上即可
/// </summary>
public class DianJIl : MonoBehaviour
{
    public Transform target;  //ui的transform
    public bool asds = false;
    Vector3 vectortar;
    Vector3 vectortar1;
    // Start is called before the first frame update
    void Start()
    {
        vectortar = Camera.main.WorldToScreenPoint(target.position);
    }
    private void OnMouseDown()
    {
       
        Debug.Log("点击了");
        asds = true;
        StartCoroutine("eniter");
    }
    IEnumerator eniter()
    {
        while (asds)
        {
            vectortar1 = Camera.main.WorldToScreenPoint(this.transform.position);
            if (Vector3.Distance(vectortar, vectortar1) > 60f)
            {
                vectortar1 = Vector3.MoveTowards(vectortar1, vectortar, 10);

                this.transform.position = Camera.main.ScreenToWorldPoint(vectortar1);
            }
            else
            {
                asds = false;
                StopCoroutine("eniter");
                Destroy(gameObject);
            }
            yield return null;
        }

    }
    // Update is called once per frame
    void Update()
    {
          
    }
}

 

简单记录一下,不喜勿喷,谢谢

 

Unity 3D物体飞到UI上的效果

原文:https://www.cnblogs.com/qq2351194611/p/14211417.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!