private void Update() { if (_curPath != null && !Mathf.Approximately(_curPath.Elapsed(), 0f)) { var nextPoint = _curPath.PathGetPoint(Mathf.Min(_curPath.ElapsedPercentage() + 0.01f, 1f)); transform.rotation = Quaternion.LookRotation(transform.position - nextPoint, Vector3.up); transform.Rotate(Vector3.right, 90f); } }
private void StartMoveToTarget(Vector3 target) { if (isStunned) { return; } if (startGameManager.inSetup) { return; } start = transform.position; this.target = new Vector3(target.x, transform.position.y, target.z); startDist = Vector3.Distance(start, this.target); SetMoving(true); transform.LookAt(this.target); //TODO with sequence to lerp up then steady then down if (moveTween != null && moveTween.IsPlaying()) { moveTween.SetTarget(this.target); moveTween.ChangeValues(start, this.target, startDist / speed); } else { moveTween = transform.DOMove(this.target, startDist / speed); moveTween.OnUpdate(() => { var t = (Mathf.Sin(moveTween.ElapsedPercentage().Remap(0, 1, -Mathf.PI + Mathf.PI / 2, Mathf.PI + Mathf.PI / 2)) + 1) / 2.0f; animator.SetFloat("Speed", t); if (!isMoving || isStunned) { moveTween.Kill(); } }); moveTween.OnComplete(() => { SetMoving(false); }); moveTween.OnKill(() => { SetMoving(false); }); } }
//EXPERIMENTAL //called on every tween update for lerping rotation between waypoints private void OnWaypointRotation() { int lookPoint = currentPoint; lookPoint = Mathf.Clamp(pathContainer.GetWaypointIndex(currentPoint), 0, pathContainer.GetWaypointCount()); if (!tween.IsInitialized() || tween.IsComplete()) { ApplyWaypointRotation(pathContainer.GetWaypoint(lookPoint).rotation); return; } TweenerCore <Vector3, Path, PathOptions> tweenPath = tween as TweenerCore <Vector3, Path, PathOptions>; float currentDist = tweenPath.PathLength() * tweenPath.ElapsedPercentage(); float pathLength = 0f; float currentPerc = 0f; int targetPoint = currentPoint; if (moveToPath) { pathLength = tweenPath.changeValue.wpLengths[1]; currentPerc = currentDist / pathLength; ApplyWaypointRotation(Quaternion.Lerp(originRot, pathContainer.GetWaypoint(currentPoint).rotation, currentPerc)); return; } if (pathContainer is BezierPathManager) { BezierPathManager bPath = pathContainer as BezierPathManager; int curPoint = currentPoint; if (reverse) { targetPoint = bPath.GetWaypointCount() - 2 - (waypoints.Length - currentPoint - 1); curPoint = (bPath.bPoints.Count - 2) - targetPoint; } int prevPoints = (int)(curPoint * bPath.pathDetail * 10); if (bPath.customDetail) { prevPoints = 0; for (int i = 0; i < targetPoint; i++) { prevPoints += (int)(bPath.segmentDetail[i] * 10); } } if (reverse) { for (int i = 0; i <= curPoint * 10; i++) { currentDist -= tweenPath.changeValue.wpLengths[i]; } } else { for (int i = 0; i <= prevPoints; i++) { currentDist -= tweenPath.changeValue.wpLengths[i]; } } if (bPath.customDetail) { for (int i = prevPoints + 1; i <= prevPoints + bPath.segmentDetail[currentPoint] * 10; i++) { pathLength += tweenPath.changeValue.wpLengths[i]; } } else { for (int i = prevPoints + 1; i <= prevPoints + 10; i++) { pathLength += tweenPath.changeValue.wpLengths[i]; } } } else { if (reverse) { targetPoint = waypoints.Length - currentPoint - 1; } for (int i = 0; i <= targetPoint; i++) { currentDist -= tweenPath.changeValue.wpLengths[i]; } pathLength = tweenPath.changeValue.wpLengths[targetPoint + 1]; } currentPerc = currentDist / pathLength; if (pathContainer is BezierPathManager) { lookPoint = targetPoint; if (reverse) { lookPoint++; } } currentPerc = Mathf.Clamp01(currentPerc); ApplyWaypointRotation(Quaternion.Lerp(pathContainer.GetWaypoint(lookPoint).rotation, pathContainer.GetWaypoint(reverse ? lookPoint - 1 : lookPoint + 1).rotation, currentPerc)); }
//EXPERIMENTAL //called on every tween update for lerping rotation between waypoints private void OnWaypointRotation() { int lookPoint = currentPoint; lookPoint = Mathf.Clamp(pathContainer.GetWaypointIndex(currentPoint), 0, pathContainer.GetWaypointCount()); if (!tween.IsInitialized() || tween.IsComplete()) { ApplyWaypointRotation(pathContainer.GetWaypoint(lookPoint).rotation); return; } TweenerCore <Vector3, Path, PathOptions> tweenPath = tween as TweenerCore <Vector3, Path, PathOptions>; float currentDist = tweenPath.PathLength() * tweenPath.ElapsedPercentage(); float pathLength = 0f; float currentPerc = 0f; int targetPoint = currentPoint; if (moveToPath) { pathLength = tweenPath.changeValue.wpLengths[1]; currentPerc = currentDist / pathLength; ApplyWaypointRotation(Quaternion.Lerp(originRot, pathContainer.GetWaypoint(currentPoint).rotation, currentPerc)); return; } if (pathContainer is BezierPathManager) { BezierPathManager bPath = pathContainer as BezierPathManager; int curPoint = currentPoint; if (reverse) { targetPoint = bPath.GetWaypointCount() - 2 - (waypoints.Length - currentPoint - 1); curPoint = (bPath.bPoints.Count - 2) - targetPoint; } int prevPoints = (int)(curPoint * bPath.pathDetail * 10); if (bPath.customDetail) { prevPoints = 0; for (int i = 0; i < targetPoint; i++) { prevPoints += (int)(bPath.segmentDetail[i] * 10); } } if (reverse) { for (int i = 0; i <= curPoint * 10; i++) { currentDist -= tweenPath.changeValue.wpLengths[i]; } } else { for (int i = 0; i <= prevPoints; i++) { currentDist -= tweenPath.changeValue.wpLengths[i]; } } if (bPath.customDetail) { for (int i = prevPoints + 1; i <= prevPoints + bPath.segmentDetail[currentPoint] * 10; i++) { pathLength += tweenPath.changeValue.wpLengths[i]; } } else { for (int i = prevPoints + 1; i <= prevPoints + 10; i++) { pathLength += tweenPath.changeValue.wpLengths[i]; } } } else { if (reverse) { targetPoint = waypoints.Length - currentPoint - 1; } for (int i = 0; i <= targetPoint; i++) { currentDist -= tweenPath.changeValue.wpLengths[i]; } pathLength = tweenPath.changeValue.wpLengths[targetPoint + 1]; } currentPerc = currentDist / pathLength; if (pathContainer is BezierPathManager) { lookPoint = targetPoint; if (reverse) { lookPoint++; } } currentPerc = Mathf.Clamp01(currentPerc); //ApplyWaypointRotation(Quaternion.Lerp(pathContainer.GetWaypoint(lookPoint).rotation, pathContainer.GetWaypoint(reverse ? lookPoint - 1 : lookPoint + 1).rotation, currentPerc)); Vector3 nextPos = tween.PathGetPoint(tweenPath.ElapsedPercentage() + 0.01f); if (local) { nextPos = pathContainer.transform.TransformPoint(nextPos); } rotationTarget.LookAt(nextPos); Transform tfm = pathContainer.GetWaypoint(reverse ? lookPoint - 1 : lookPoint + 1); Quaternion q = Quaternion.FromToRotation(Vector3.forward, forwardDir); rotationTarget.rotation *= q; //q = Quaternion.Lerp(pathContainer.GetWaypoint(lookPoint).rotation, tfm.rotation, currentPerc); //rotationTarget.rotation = q * rotationTarget.rotation; q = Quaternion.Lerp(pathContainer.GetWaypoint(lookPoint).localRotation, tfm.localRotation, currentPerc); rotationTarget.localRotation *= q; rotationTarget.Rotate(eulerAngles); }