using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class EX01 : MonoBehaviour { float x, y; float tx, ty; float dx, dy; public GameObject pos; Text txtPos; // Use this for initialization void Start () { x = Random.Range(-1.4f, 1.4f); y = Random.Range(-2.5f, 2.5f); tx = Random.Range(-1.4f, 1.4f); ty = Random.Range(-2.5f, 2.5f); this.transform.position = new Vector3(x, y, 0); txtPos = pos.gameObject.GetComponent(); txtPos.text = "(" + x.ToString("0.00") + "," + y.ToString("0.00") + ")"; } // Update is called once per frame void Update () { dx = tx - x; if (dx > 0) { x += Time.deltaTime; } else { x -= Time.deltaTime; } this.transform.position = new Vector3(x, y, 0); if (Mathf.Abs(dx) < 0.1f) { tx = Random.Range(-1.4f, 1.4f); ty = Random.Range(-2.5f, 2.5f); } } }