Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. 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


    Oct 16, 2016
    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


    Jul 1, 2016
    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.