I added unit.Pushed(targetUnit.GetGridPosition() - unit.GetGridPosition());
in the private void NextState()
for SwordAction.cs and that seems to work fine.
Then I added the public void Pushed(GridPosition direction)
in Unit.cs, but it isn’t able to get passed the second check. I put some debugs like so
GridPosition testGridPosition = GetGridPosition() + direction;
if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition)) return;
Debug.Log("Pass step 1");
if (LevelGrid.Instance.HasAnyUnitOnGridPosition(testGridPosition)) return;
Debug.Log("Pass step 2");
if (!Pathfinding.Instance.IsWalkableGridPosition(testGridPosition)) return;
Debug.Log("Pass step 3");
pushedGridPosition = testGridPosition;
isPushed = true;
Debug.Log("Pushed? " + isPushed);
It gets passed the first step but then ends and so nothing happens. I tried flipping around unit.Pushed(unit.GetGridPosition() - targetUnit.GetGridPosition());
but that just made the sword unit go backward.
For reference, this is the code I added to Update()
if (!isPushed)
{
return;
}
Debug.Log("Push activated");
Vector3 targetPosition = LevelGrid.Instance.GetWorldPosition(pushedGridPosition);
Vector3 moveDirection = (targetPosition - transform.position).normalized;
float rotateSpeed = 12f;
transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);
float stoppingDistance = .1f;
if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
{
float moveSpeed = 6f;
transform.position += moveDirection * moveSpeed * Time.deltaTime;
}
else
{
newGridPosition = LevelGrid.Instance.GetGridPosition(transform.position);
isPushed = false;
}