I am almost certain I followed this lecture to a T, but at the first test to make sure the movement action works fine I’m getting no animation, my unit will move to the correct square but has no walking animation. Code for MoveAction:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveAction : BaseAction
{
public event EventHandler OnStartMoving;
public event EventHandler OnStopMoving;
[SerializeField] private int maxMoveDistance = 4;
private Vector3 targetPosition;
protected override void Awake()
{
base.Awake();
targetPosition = transform.position;
}
private void Update()
{
if(!isActive)
{
return;
}
Vector3 moveDirection = (targetPosition - transform.position).normalized;
float stoppingDistance = .1f;
if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
{
float moveSpeed = 4f;
transform.position += moveDirection * moveSpeed * Time.deltaTime;
}
else
{
OnStopMoving?.Invoke(this, EventArgs.Empty);
ActionComplete();
}
float rotateSpeed = 10f;
transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);
}
public override void TakeAction(GridPosition gridPosition, Action onActionComplete)
{
ActionStart(onActionComplete);
this.targetPosition = LevelGrid.Instance.GetWorldPosition(gridPosition);
OnStartMoving?.Invoke(this, EventArgs.Empty);
}
public override List<GridPosition> GetValidActionGridPositionList()
{
List<GridPosition> validGridPositionList = new List<GridPosition>();
GridPosition unitGridPosition = unit.GetGridPosition();
for (int x = -maxMoveDistance; x <= maxMoveDistance; x++)
{
for (int z = -maxMoveDistance; z <= maxMoveDistance; z++)
{
GridPosition offsetGridPosition = new GridPosition(x, z);
GridPosition testGridPosition = unitGridPosition + offsetGridPosition;
if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
{
continue;
}
if (unitGridPosition == testGridPosition)
{
//same Grid Position where the unit is already at
continue;
}
if (LevelGrid.Instance.HasAnyUnitOnGridPosition(testGridPosition))
{
// Grid position already occupied with another unit
continue;
}
validGridPositionList.Add(testGridPosition);
}
}
return validGridPositionList;
}
public override string GetActionName()
{
return "Move";
}
}
Code for UnitAnimator:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitAnimator : MonoBehaviour
{
[SerializeField] private Animator animator;
private void Awake()
{
if (TryGetComponent<MoveAction>(out MoveAction moveAction))
{
moveAction.OnStartMoving += MoveAction_OnStartMoving;
moveAction.OnStopMoving += MoveAction_OnStopMoving;
}
}
private void MoveAction_OnStartMoving(object sender, EventArgs e)
{
animator.SetBool("Iswalking", true);
}
private void MoveAction_OnStopMoving(object sender, EventArgs e)
{
animator.SetBool("Iswalking", false);
}
}
Just let me know if I messed something up!