public IEnumerator MoveToCoroutine(Vector3 destPos, TweenDefinition moveToDefinition) { return TweenToCoroutine(new TweenTransform(destPos), moveToDefinition); }
public IEnumerator MoveToCoroutine(Vector3 destPos, TweenDefinition moveToDefinition) { return(TweenToCoroutine(new TweenTransform(destPos), moveToDefinition)); }
/** Change position, rotation and scale. */ public IEnumerator TweenToCoroutine(TweenTransform tweenTransform, TweenDefinition definition) { int movingId = tweenTransform.ChangePosition && tweenTransform.Position != transform.position ? ++_movingId : -1; int rotatingId = tweenTransform.ChangeRotation && tweenTransform.Rotation != transform.rotation ? ++_rotatingId : -1; int scalingId = tweenTransform.ChangeScale && !IsLocalScaleMulti(tweenTransform.LocalScaleMulti) ? ++_scalingId : -1; if (movingId == -1 && rotatingId == -1 && scalingId == -1) yield break; Debug.Log("TweenTo " + tweenTransform.Position + " gameObject " + gameObject); // position nosmooth Vector3 deltaPos = (tweenTransform.Position - transform.position); Vector3 deltaNormalized = deltaPos; deltaNormalized.Normalize(); Vector3 velocity; float factor; // starting values Vector3 startScale = transform.localScale; Vector3 newScale = _startingScale * tweenTransform.LocalScaleMulti; Quaternion startRot = transform.rotation; Vector3 startPos = transform.position; // precompute duration float duration = 1; if (movingId != -1) duration = GetMoveTime(transform.position, tweenTransform.Position, definition.StartSpeed, definition.Acceleration); else if (scalingId != -1) duration = (newScale - startScale).sqrMagnitude; else duration = Quaternion.Angle(tweenTransform.Rotation, startRot); // smoothdamp float smoothSpeed = 0; float timeFactor = 1 / duration; float t = 0; float smoothT = 0; // for time // float timeEnd = Time.time + definition.ForTime; float timeStart = Time.time; _transformingCnt++; while (true) { /* debug if (_movingId != -1) transform.position = tweenTransform.Position; if (_rotatingId != -1) transform.rotation = tweenTransform.Rotation; if (_scalingId != -1) transform.localScale = newScale; break; */ // if (definition.ForTime != 0 && Time.time >= timeEnd) // break; t = t + Time.deltaTime * timeFactor; if (definition.Type == TweenDefinition.TransformType.NoSmooth) { if (movingId == _movingId) { // TODO: some bug here (from minion on table to opponent hero it takes longer time than from opponent hero to minion on table) velocity = Vector3.Lerp(Vector3.zero, deltaNormalized, Time.deltaTime); factor = definition.StartSpeed; if (definition.Acceleration != 0) factor = factor + definition.Acceleration * (Time.time - timeStart); // Debug.Log("MOVE base velocity " + velocity + " factor " + factor + " timeDelta " + Time.deltaTime); velocity = velocity * factor; transform.position += velocity; if (t >= 1) { transform.position = tweenTransform.Position; // Debug.Log("MOVE END from " + transform.position + " to " + tweenTransform.Position); movingId = -1; } } if (rotatingId == _rotatingId) transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, t); if (scalingId == _scalingId) transform.localScale = Vector3.Lerp(startScale, newScale, t); if (t >= 1) break; } else if (definition.Type == TweenDefinition.TransformType.SmoothOut) { smoothT = Mathf.SmoothDamp(smoothT, 1f, ref smoothSpeed, duration); if (smoothT > 0.999f) smoothT = 1; if (movingId == _movingId) transform.position = Vector3.Lerp(startPos, tweenTransform.Position, smoothT); if (rotatingId == _rotatingId) transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, smoothT); if (scalingId == _scalingId) transform.localScale = Vector3.Lerp(startScale, newScale, smoothT); if (smoothT >= 1) break; } // TODO: GS opposite to smooth out else if (definition.Type == TweenDefinition.TransformType.SmoothIn) { smoothT = Mathf.SmoothDamp(smoothT, 1f, ref smoothSpeed, duration); if (smoothT > 0.999f) smoothT = 1; if (movingId == _movingId) transform.position = Vector3.Lerp(startPos, tweenTransform.Position, smoothT); if (rotatingId == _rotatingId) transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, smoothT); if (scalingId == _scalingId) transform.localScale = Vector3.Lerp(startScale, newScale, smoothT); if (smoothT >= 1) break; } else if (definition.Type == TweenDefinition.TransformType.SmoothInOut) { smoothT = Mathf.SmoothStep(0f, 1f, t); if (movingId == _movingId) transform.position = Vector3.Lerp(startPos, tweenTransform.Position, smoothT); if (rotatingId == _rotatingId) transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, smoothT); if (scalingId == _scalingId) transform.localScale = Vector3.Lerp(startScale, newScale, smoothT); if (smoothT >= 1) break; } yield return null; } _transformingCnt--; Debug.Log("TweenTo ENDED " + tweenTransform == null ? "null" : tweenTransform.Position + " gameObject " + gameObject); }
/** Change position, rotation and scale. */ public IEnumerator TweenToCoroutine(TweenTransform tweenTransform, TweenDefinition definition) { int movingId = tweenTransform.ChangePosition && tweenTransform.Position != transform.position ? ++_movingId : -1; int rotatingId = tweenTransform.ChangeRotation && tweenTransform.Rotation != transform.rotation ? ++_rotatingId : -1; int scalingId = tweenTransform.ChangeScale && !IsLocalScaleMulti(tweenTransform.LocalScaleMulti) ? ++_scalingId : -1; if (movingId == -1 && rotatingId == -1 && scalingId == -1) { yield break; } Debug.Log("TweenTo " + tweenTransform.Position + " gameObject " + gameObject); // position nosmooth Vector3 deltaPos = (tweenTransform.Position - transform.position); Vector3 deltaNormalized = deltaPos; deltaNormalized.Normalize(); Vector3 velocity; float factor; // starting values Vector3 startScale = transform.localScale; Vector3 newScale = _startingScale * tweenTransform.LocalScaleMulti; Quaternion startRot = transform.rotation; Vector3 startPos = transform.position; // precompute duration float duration = 1; if (movingId != -1) { duration = GetMoveTime(transform.position, tweenTransform.Position, definition.StartSpeed, definition.Acceleration); } else if (scalingId != -1) { duration = (newScale - startScale).sqrMagnitude; } else { duration = Quaternion.Angle(tweenTransform.Rotation, startRot); } // smoothdamp float smoothSpeed = 0; float timeFactor = 1 / duration; float t = 0; float smoothT = 0; // for time // float timeEnd = Time.time + definition.ForTime; float timeStart = Time.time; _transformingCnt++; while (true) { /* debug * if (_movingId != -1) * transform.position = tweenTransform.Position; * if (_rotatingId != -1) * transform.rotation = tweenTransform.Rotation; * if (_scalingId != -1) * transform.localScale = newScale; * break; */ // if (definition.ForTime != 0 && Time.time >= timeEnd) // break; t = t + Time.deltaTime * timeFactor; if (definition.Type == TweenDefinition.TransformType.NoSmooth) { if (movingId == _movingId) { // TODO: some bug here (from minion on table to opponent hero it takes longer time than from opponent hero to minion on table) velocity = Vector3.Lerp(Vector3.zero, deltaNormalized, Time.deltaTime); factor = definition.StartSpeed; if (definition.Acceleration != 0) { factor = factor + definition.Acceleration * (Time.time - timeStart); } // Debug.Log("MOVE base velocity " + velocity + " factor " + factor + " timeDelta " + Time.deltaTime); velocity = velocity * factor; transform.position += velocity; if (t >= 1) { transform.position = tweenTransform.Position; // Debug.Log("MOVE END from " + transform.position + " to " + tweenTransform.Position); movingId = -1; } } if (rotatingId == _rotatingId) { transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, t); } if (scalingId == _scalingId) { transform.localScale = Vector3.Lerp(startScale, newScale, t); } if (t >= 1) { break; } } else if (definition.Type == TweenDefinition.TransformType.SmoothOut) { smoothT = Mathf.SmoothDamp(smoothT, 1f, ref smoothSpeed, duration); if (smoothT > 0.999f) { smoothT = 1; } if (movingId == _movingId) { transform.position = Vector3.Lerp(startPos, tweenTransform.Position, smoothT); } if (rotatingId == _rotatingId) { transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, smoothT); } if (scalingId == _scalingId) { transform.localScale = Vector3.Lerp(startScale, newScale, smoothT); } if (smoothT >= 1) { break; } } // TODO: GS opposite to smooth out else if (definition.Type == TweenDefinition.TransformType.SmoothIn) { smoothT = Mathf.SmoothDamp(smoothT, 1f, ref smoothSpeed, duration); if (smoothT > 0.999f) { smoothT = 1; } if (movingId == _movingId) { transform.position = Vector3.Lerp(startPos, tweenTransform.Position, smoothT); } if (rotatingId == _rotatingId) { transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, smoothT); } if (scalingId == _scalingId) { transform.localScale = Vector3.Lerp(startScale, newScale, smoothT); } if (smoothT >= 1) { break; } } else if (definition.Type == TweenDefinition.TransformType.SmoothInOut) { smoothT = Mathf.SmoothStep(0f, 1f, t); if (movingId == _movingId) { transform.position = Vector3.Lerp(startPos, tweenTransform.Position, smoothT); } if (rotatingId == _rotatingId) { transform.rotation = Quaternion.Slerp(startRot, tweenTransform.Rotation, smoothT); } if (scalingId == _scalingId) { transform.localScale = Vector3.Lerp(startScale, newScale, smoothT); } if (smoothT >= 1) { break; } } yield return(null); } _transformingCnt--; Debug.Log("TweenTo ENDED " + tweenTransform == null ? "null" : tweenTransform.Position + " gameObject " + gameObject); }