public void StartMove(float height, float speed, float scale) { InitSelf(); pausing = false; this.height = height; this.speed = speed; this.scale = scale; if (null != _cor) { StopCoroutine(_cor); _cor = null; } running = true; transform.localScale = new Vector3(scale, scale, 1); Vector2[] scope = Vector2DUtils.GetScreenScope(); float xbound = render.bounds.size.x * .5f; float rightX = scope [1].x + xbound; float leftX = scope [0].x - xbound; float startX = speed < 0?rightX:leftX; float targetX = speed < 0?leftX:rightX; Vector2DUtils.ChangePositionX(transform, startX); gameObject.SetActive(true); _cor = StartCoroutine(DoUpdate(targetX)); }
protected override void Reset() { base.Reset(); animator.StopPlay(); if (null != _cor) { StopCoroutine(_cor); } int colorIndex = UnityEngine.Random.Range(0, colors.Length); render.color = colors[colorIndex]; Vector2[] scope = Vector2DUtils.GetScreenScope(); float xbound = render.bounds.size.x * .5f; float rightX = scope [1].x + xbound; float leftX = scope [0].x - xbound; float speed = UnityEngine.Random.Range(0.8f, Constants.speed_butterfly_move_max); float startX = speed < Mathf.Lerp(0.5f, Constants.speed_butterfly_move_max, 0.5f)?rightX:leftX; float startY = UnityEngine.Random.Range(scope[0].y, scope[1].y); transform.position = new Vector3(startX, startY, transform.position.z); Vector3 target1 = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0)); target1.z = transform.position.z; gameObject.SetActive(true); _cor = StartCoroutine(DoUpdate(speed, target1)); }
IEnumerator DoUpdate() { Vector2[] scope = Vector2DUtils.GetScreenScope(); float maxHeight = scope [1].y - scope [0].y; while (true) { if (!pausing) { int index = UnityEngine.Random.Range(0, clouds.Length); Cloud c = clouds [index]; if (!c.running) { float height = UnityEngine.Random.Range(0, maxHeight); float speed = UnityEngine.Random.Range(0.05f, Constants.speed_cloud_move_max); if (UnityEngine.Random.Range(0, 2) == 0) { speed *= -1; } float scale = UnityEngine.Random.Range(0.2f, 1.5f); c.StartMove(height, speed, scale); } } yield return(new WaitForSeconds(5f)); } }
IEnumerator DoUpdate(Vector3 center, Vector2 size, AnimationDoneCallBack callback, float animateTime) { Vector2[] scope = Vector2DUtils.GetScreenScope(); //to radius size = size * 0.5f; Vector3 lStart = new Vector3(scope[0].x - lr.bounds.size.x * 0.5f, center.y, center.z); Vector3 lEnd = lStart; lEnd.x = center.x - size.x - lr.bounds.size.x * 0.5f; Vector3 tStart = new Vector3(center.x, scope[1].y + tr.bounds.size.y * 0.5f, center.z); Vector3 tEnd = tStart; tEnd.y = center.y + size.y + tr.bounds.size.y * 0.5f; Vector3 rStart = new Vector3(scope[1].x + rr.bounds.size.x * 0.5f, center.y, center.z); Vector3 rEnd = rStart; rEnd.x = center.x + size.x + rr.bounds.size.x * 0.5f; Vector3 bStart = new Vector3(center.x, scope[0].y - br.bounds.size.y * 0.5f, center.z); Vector3 bEnd = bStart; bEnd.y = center.y - size.y - br.bounds.size.y * 0.5f; lr.transform.position = lStart; tr.transform.position = tStart; rr.transform.position = rStart; br.transform.position = bStart; if (Math.Abs(animateTime) > Mathf.Epsilon) { float lSpeed = ((lEnd - lStart) / animateTime).magnitude; float tSpeed = ((tEnd - tStart) / animateTime).magnitude; float rSpeed = ((rEnd - rStart) / animateTime).magnitude; float bSpeed = ((bEnd - bStart) / animateTime).magnitude; float timeUsed = 0; while (timeUsed < animateTime) { if (!pausing) { timeUsed += Time.deltaTime; Vector2DUtils.MoveToSmoothly(lr.transform, lEnd, lSpeed); Vector2DUtils.MoveToSmoothly(tr.transform, tEnd, tSpeed); Vector2DUtils.MoveToSmoothly(rr.transform, rEnd, rSpeed); Vector2DUtils.MoveToSmoothly(br.transform, bEnd, bSpeed); } yield return(null); } } lr.transform.position = lEnd; tr.transform.position = tEnd; rr.transform.position = rEnd; br.transform.position = bEnd; callback(center, size); }
void DrawOutLine() { if (EditorApplication.isCompiling || EditorApplication.isUpdating) { return; } Vector2[] scope = Vector2DUtils.GetScreenScope(); Vector3 _p1 = Vector3.zero; Vector3 _p2 = Vector3.zero; _p1.y = transform.position.y; _p1.x = scope[0].x; _p2.y = transform.position.y; _p2.x = scope[1].x; Gizmos.color = color; Gizmos.DrawLine(_p1, _p2); }
IEnumerator DoUpdate(float speed, Vector3 target) { animator.StartPlay(); while (true) { transform.eulerAngles = startAngulars; if (target.x < transform.position.x) { Vector2DUtils.Flip(transform); } while (transform.position != target) { // Log.Debug ("gooooo : {0},{1}",transform.position , target); if (Vector2DUtils.IsOutsideScreen(transform.position, Direction2D.up)) { // Log.Debug ("{0} outof screen.",gameObject.name); StopAct(); yield break; } if (!pausing) { Vector2DUtils.MoveToSmoothly(transform, target, speed); } yield return(null); } Vector2[] scope = Vector2DUtils.GetScreenScope(); float xbound = render.bounds.size.x * .5f; float rightX = scope [1].x + xbound + 10; float leftX = scope [0].x - xbound - 10; target.x = UnityEngine.Random.Range(leftX, rightX); target.y = UnityEngine.Random.Range(scope[0].y, scope[1].y); // Log.Debug ("move to new pos : {0}",target); } }
IEnumerator DoUpdate(float targetX) { while (true) { if (!pausing) { Vector2[] scope = Vector2DUtils.GetScreenScope(); float y = scope [0].y + height; Vector3 targetPos = Vector2.MoveTowards(transform.position, new Vector2(targetX, transform.position.y), Mathf.Abs(speed) * Time.deltaTime); targetPos.y = y; targetPos.z = transform.position.z; transform.position = targetPos; if (Math.Abs(transform.position.x - targetX) < Mathf.Epsilon) { running = false; gameObject.SetActive(false); break; } } yield return(null); } }