Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Creating an Enemy Movement Pattern

Discussion in 'Physics' started by gameangel147, Jun 13, 2018.

  1. gameangel147

    gameangel147

    Joined:
    Oct 16, 2016
    Posts:
    21
    I'm not sure if this question goes in this sub but I think so since it deals with standard velocity movement.

    I'm making a sort of tower defense game in which enemies move to the right, seen in a side-view. I want some of the enemy types to move in certain patterns, kind of like in Galaga, where different enemies have different movement patterns.

    For example, one will move up and down in a "sound wave" pattern. Another will kind of move around randomly and unpredictably. Another might move in a few circles, to throw the player off.

    I don't think a navmesh will help since this is not pathfinding, plus I don't know how to use them yet, so I've been experimenting with adjusting the velocity or translating their position, but not much luck yet.

    Anyone know how to do this or have any tips?

    Thanks for the help!

    Note: Using Unity 2017.1.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    Navmesh could work for fairly straight lines. It wouldn't be much different than moving them manually, though, since it would consist of determining the next point and sending the agent there. The "hard" work is determining the points, not changing the enemy's position.

    I've used the two following relatively simple options:

    Basic Math Functions: In the enemy's Update, continuously change its position based on some math functions, like sine or sawtooth functions. For this, just set one axis's values to something like `Mathf.Sin(Time.timeSinceLevelLoad * SomeSpeedConstant)`. For more random motion, you could run a Coroutine that picks a new value for the axis at a regular interval, but picks a random number for the value. Once you get one of your enemies moving with a sine function you should almost certain feel like you've captured the Galaga feel... Those old games did this kind of stuff all the time.

    Animations: If you want more fine control over things, you can add an Animator Controller to your character, and create Animations that modify the position of the object. Unity's animation system can be used to animation (change over time) just about any values on a game object. It's not just for animating models. For example, I have lights that dim and brighten. I created an animation and which adjusted the intensity over time, so that I can fine tune how quickly the intensity changes. Using animations seems a bit complex at first, you it really gives you a lot of control, and you can easily preview the changes in the editor.
     
    Westland and gameangel147 like this.
  3. gameangel147

    gameangel147

    Joined:
    Oct 16, 2016
    Posts:
    21
    Thanks, I ended up going with math functions. I found some formulas used in wave mathematics, and then I did a random behavior where it randomly, moves up or down as it moves forward.

    Thanks for the help!