Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. 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
  8. 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:
    643
    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.
     
    gameangel147 likes 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!