WACK-A-MOLE QUEST: ‘Tricky Patterns’ - Solutions

Quest: Wack-A-Mole Quest
Challenge: Tricky Patterns

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

Using the animation editor, you can move the Body (not Enemy) to a third location. When you click the record button and slide the white line past the end of the animation and move the body, it will add that movement to the animation. I only moved mine at a right angle and made it pop up at the third location. You have to keep in mind that the box may end up out of the play field.

1 Like

this was a major headache for me and ended up taking about 3 hours. First I remade the animation (had to review how that stuff works together) so that all it does is pop the enemy up and then back down. Then I created a new EnemyMover class to deal with the hidden movement. I had some trouble with getting the animation to run properly but eventually got it working just the way I intended

using UnityEngine;

public class EnemyMover : MonoBehaviour
{
    [SerializeField] Animator animator;
    [SerializeField] Vector2 feildSize;
    [SerializeField] float moveSpeed = 1f;
    [SerializeField] float moveDistance = 1;

    Vector3 nextPosition;
    bool animationIsActive = false;

    public Vector3 GetNextPosition() { return nextPosition; }

    void Start()
    {
        AssignNextPosition();
        PopUp();
    }

    void Update() { if (!animationIsActive) MoveToNewPosition(); }

    void MoveToNewPosition()
    {
        transform.position = Vector3.MoveTowards(transform.position, nextPosition, moveSpeed);
        if(transform.position == nextPosition) PopUp();
    }

    void PopUp()
    {
        animationIsActive = true;
        animator.SetTrigger("StartAnimation");
        AssignNextPosition();
    }

    void ResetAnimationFlag() { animationIsActive = false; }

    void AssignNextPosition()
    {
        bool isTooClose = true;
        Vector3 newPosition = new Vector3(0,0,0);
        while(isTooClose)
        {
            newPosition = CalculatePosition(transform.position.y);
            if(Vector3.Distance(transform.position, newPosition) > moveDistance) isTooClose = false;
        }
        nextPosition = newPosition;

        Vector3 CalculatePosition(float yPos)
        {
            float xAxisValue = ((Random.value - .5f) * feildSize.x);
            float yAxisValue = transform.position.y;
            float zAxisValue = ((Random.value - .5f) * feildSize.y);
            return new Vector3(xAxisValue, yAxisValue, zAxisValue);
        }
    }
}