IEnumerator <float> WalkTo(Vector3 Position, float StartingSpeedPercentage, float MaxSpeed, System.Action Callback) { Position = new Vector3(Position.x, Position.y, 1); PlayerAnimationHelper.StopPlayer(); //RequestPath.GetPath(rb.position, Position, path, Resolution.High); RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col); //IgnoreAllCollisionExcept(true, 31, 9, 10); col.isTrigger = true; float minSqrDist = 0; Vector3 pathFindingCenter = Vector3.zero; if (currentTarget != null) { minSqrDist = Mathf.Pow(currentTarget.interactionOptions.InteractionRange, 2); pathFindingCenter = currentTarget.gameObject.transform.position + (Vector3)currentTarget.interactionOptions.InteractionCenterOffset; } yield return(0f); if (path.Count != 0) { anim.SetBool("Walking", true); } MaxSpeed *= Stats.MoveSpeed; float _spd = StartingSpeedPercentage * MaxSpeed; int index = 0; while (this) { if (index >= path.Count) { break; } if (!CanWalkAt(path[index])) { yield return(0f); //RequestPath.GetPath(rb.position, Position, path, Resolution.High); RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col); index = 0; continue; } Vector3 currentPos = rb.position; Vector3 targetPos = grid.PosFromNode(path[index]); var offset = new Vector3(targetPos.x - currentPos.x, targetPos.y - currentPos.y, 1); if (_spd != MaxSpeed) { _spd = Mathf.MoveTowards(_spd, MaxSpeed, 10 * MaxSpeed * GameTime.Time.fixedDeltaTime); } float currentSpeed = _spd * GameTime.Time.fixedDeltaTime; var posAfterMovement = Vector3.MoveTowards(currentPos, targetPos, currentSpeed); // Update the index and increase the movement point without losing a frame of movement. if (posAfterMovement == targetPos) { index++; if (index >= path.Count) { } else { targetPos = new Vector3(grid.PosFromNode(path[index]).x, grid.PosFromNode(path[index]).y, 1); offset = new Vector3(targetPos.x - currentPos.x, targetPos.y - currentPos.y, 1); posAfterMovement = Vector3.MoveTowards(currentPos, targetPos, currentSpeed); } } /// Check if we've reached the <see cref="Objects.Functionality.InteractionOptions.InteractionRange"/>. if (currentTarget != null) { float sqrDistanceToTarget = Vector2.SqrMagnitude(posAfterMovement - pathFindingCenter); if (sqrDistanceToTarget <= minSqrDist) { break; } } rb.MovePosition(posAfterMovement); if (index >= path.Count) { break; } CalculateFacing(offset); yield return(0f); } if (!this) { yield break; } anim.SetBool("Walking", false); yield return(0f); col.isTrigger = false; PlayerAnimationHelper.ResetPlayer(); currentHandle = null; currentTarget = null; if (Callback != null) { Callback(); } }
IEnumerator <float> WalkTo(Vector3 Position, float StartingSpeedPercentage, float MaxSpeed, System.Action Callback) { Position = new Vector3(Position.x, Position.y, 1); PlayerAnimationHelper.StopPlayer(); //RequestPath.GetPath(rb.position, Position, path, Resolution.High); RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col); //IgnoreAllCollisionExcept(true, 31, 9, 10); col.isTrigger = true; yield return(0f); if (path.Count != 0) { anim.SetBool("Walking", true); } MaxSpeed *= Stats.MoveSpeed; float _spd = StartingSpeedPercentage * MaxSpeed; int index = 0; while (this) { if (index >= path.Count) { break; } if (!CanWalkAt(path[index])) { yield return(0f); //RequestPath.GetPath(rb.position, Position, path, Resolution.High); RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col); index = 0; continue; } Vector3 targetPos = grid.PosFromNode(path[index]); Vector3 offset = new Vector3(targetPos.x - rb.position.x, targetPos.y - rb.position.y, 1); if (_spd != MaxSpeed) { _spd = Mathf.MoveTowards(_spd, MaxSpeed, 10 * MaxSpeed * Time.fixedDeltaTime); } float currentSpeed = _spd * Time.fixedDeltaTime; var posAfterMovement = Vector3.MoveTowards(rb.position, targetPos, currentSpeed); if (posAfterMovement == targetPos) { index++; if (index >= path.Count) { } else { targetPos = new Vector3(grid.PosFromNode(path[index]).x, grid.PosFromNode(path[index]).y, 1); offset = new Vector3(targetPos.x - rb.position.x, targetPos.y - rb.position.y, 1); posAfterMovement = Vector3.MoveTowards(rb.position, targetPos, currentSpeed); } } rb.MovePosition(posAfterMovement); if (index >= path.Count) { break; } CalculateFacing(offset); yield return(0f); } if (!this) { yield break; } anim.SetBool("Walking", false); yield return(0f); GameLibOfMethods.player.GetComponent <Collider2D>().isTrigger = false; PlayerAnimationHelper.ResetPlayer(); currentHandle = null; if (Callback != null) { Callback(); } }