private void FixedUpdate()
    {
        currentPosition = heroTransform.position;

        if (Mathf.Ceil(currentPosition.x) != Mathf.Ceil(targetPosition.x) ||
            Mathf.Ceil(currentPosition.y) != Mathf.Ceil(targetPosition.y))
        {
            Vector2 roundedTarget  = new Vector2(Mathf.RoundToInt(targetPosition.x), Mathf.RoundToInt(targetPosition.y));
            Vector2 roundedCurrent = new Vector2(Mathf.RoundToInt(currentPosition.x), Mathf.RoundToInt(currentPosition.y));

            Vector2 directionMovement = (roundedTarget - roundedCurrent).normalized;

            directionMovement.x = Mathf.Round(directionMovement.x);
            directionMovement.y = Mathf.Round(directionMovement.y);

            Vector2 movement    = directionMovement * movementSpeed;
            Vector2 newPosition = currentPosition + movement * Time.fixedDeltaTime;

            heroRigidbody.MovePosition(newPosition);
            characterAnimatorScript.AnimateRun(targetPosition);
        }
        else
        {
            if (pathFinding.path != null && pathFinding.path.Count > 0)
            {
                targetPosition = pathFinding.path.Pop();
            }
            characterAnimatorScript.AnimateStatic();

            heroPositionAndState.Position     = targetPosition;
            heroPositionAndState.IsAtPosition = true;

            placeSelectEvent.Raise(heroPositionAndState);
        }
    }
Beispiel #2
0
    public void MoveToTargetPosition()
    {
        Vector2 roundedTarget  = new Vector2(Mathf.RoundToInt(targetPosition.x), Mathf.RoundToInt(targetPosition.y));
        Vector2 roundedCurrent = new Vector2(Mathf.RoundToInt(currentPosition.x), Mathf.RoundToInt(currentPosition.y));

        movementDirection   = (roundedTarget - roundedCurrent).normalized;
        movementDirection.x = Mathf.Round(movementDirection.x);
        movementDirection.y = Mathf.Round(movementDirection.y);

        movement            = movementDirection * troopData.valMotionSpeed;
        fixedTargetPosition = currentPosition + movement * Time.fixedDeltaTime;

        troopRigidBody2d.MovePosition(fixedTargetPosition);
        troopAnimations.AnimateRun(targetPosition);
    }