private void Update() { if (!isLocked) { if (Time.time > randomMoveTimer + randomMoveInterval) { randomMoveTimer = Time.time; Vector2 tarPoint = PlayerCharacter.instance.GetPosition() + Mathf2.SelectRandomPoint(randomPointRange); Vector2 dir = tarPoint - uComponent.GetPosition(); uComponent.MoveTo(dir); } } else { if (Time.time > folllowUpdateInterval + followUpdateTimer) { followUpdateTimer = Time.time; randomMoveTimer = Time.time; Vector2 tarPoint = PlayerCharacter.instance.GetPosition(); Vector2 dir = tarPoint - uComponent.GetPosition(); uComponent.MoveTo(dir); } } }
public void Init() { instance = this; tr = transform; radius = GetComponent <CircleCollider2D>().bounds.size.x / 2; for (int i = 0; i < starCount; i++) { Vector2 randomPoint = Mathf2.SelectRandomPoint(radius); Vector3 randomPos = new Vector3(randomPoint.x + PlayerCharacter.instance.tr.position.x, randomPoint.y + PlayerCharacter.instance.tr.position.y, UnityEngine.Random.Range(10, 30)); GameObject star = GameObject.Instantiate(starObj, randomPos, Quaternion.identity); star.transform.SetParent(starRoot.transform); star.GetComponent <SpriteRenderer>().color = Random.ColorHSV(); } }
IEnumerator InstantiateStarsOverTime() { for (int i = 0; i < starRoot.transform.childCount; i++) { if (!PlayerCharacter.instance) { break; } Vector2 randomPoint = Mathf2.SelectRandomPoint(radius); Vector3 randomPos = new Vector3(randomPoint.x + PlayerCharacter.instance.tr.position.x, randomPoint.y + PlayerCharacter.instance.tr.position.y, UnityEngine.Random.Range(10, 30)); starRoot.transform.GetChild(i).transform.position = randomPos; if (i % 20 == 0) { yield return(new WaitForEndOfFrame()); } } }