After the challenge to move the movement code, when I test it out I can select a unit but when I click on the Grid the unit does not move.
I have no compile errors at all
This is my UnitActionSystem.cs
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (TryHandleUnitSelection()) { return; }
selectedUnit.GetMoveAction().Move(MouseWorldPosition.GetPosition());
}
}
My MoveAction.cs
[SerializeField] private Animator unitAnimator;
private Vector3 targetPosition;
private void Awake()
{
targetPosition = transform.position;
}
// Update is called once per frame
void Update()
{
float stoppingDistance = 0.1f;
if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
{
Vector3 moveDirection = (targetPosition - transform.position).normalized;
float moveSpeed = 4f;
transform.position += moveDirection * Time.deltaTime * moveSpeed;
float rotateSpeed = 10f;
transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);
unitAnimator.SetBool("IsWalking", true);
}
else
{
unitAnimator.SetBool("IsWalking", false);
}
}
public void Move(Vector3 targetPosition)
{
this.targetPosition = targetPosition;
}
And the Unit.cs
public class Unit : MonoBehaviour
{
private GridPosition gridPosition;
private MoveAction moveAction;
private void Awake()
{
gridPosition = GetComponent<GridPosition>();
moveAction = GetComponent<MoveAction>();
}
private void Start()
{
gridPosition = LevelGrid.Instance.GetGridPosition(transform.position);
LevelGrid.Instance.SetUnitAtGridPosition(gridPosition, this);
}
private void Update()
{
GridPosition newGridPosition = LevelGrid.Instance.GetGridPosition(transform.position);
if(newGridPosition != gridPosition)
{
// Unit has changed it's gridPosition.
LevelGrid.Instance.UnitMovedGridPosition(this, gridPosition, newGridPosition);
gridPosition = newGridPosition;
}
}
public MoveAction GetMoveAction()
{
return moveAction;
}
}
Any pointers much appreciated