效果如下
代码如下
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() { } }
简单记录一下,不喜勿喷,谢谢
原文:https://www.cnblogs.com/qq2351194611/p/14211417.html