- Home /
How to make 2D multiple waypoints for sidescroller shooter or some other similiar movement
I'm making 2D Space SideScroller shooter, and I want to my enemies to have waypoints to move, or just some preset zig-zag routes. I'd like to hear as much as possible ways to do this, so far the ones I've found online in Unity answers haven't really worked for me.
Here's a similar example of what I want it to look like. http://youtu.be/Fd9UIy9J1q0?t=20s
Also, I prefer C#, though in worst cases I guess I can translate it over from JavaScript
Thank you for your help! ^_^
Answer by cryingwolf85 · May 20, 2014 at 06:14 PM
From the video, it seems to be some kind of Sin wave. Attach something like this to the enemy:
public float xPos = 0f;
public float speed = 5f;
public float waveHeight = 5;
void Update(){
// Calculate y position
float yPos = Mathf.Sin(Time.time * speed) * waveHeight / 6;
// X Position is just increasing over time
xPos += Time.deltaTime;
// Physically move the position
transform.position = new Vector3(xPos, yPos, 0);
}
Cheers
Thanks, really nice example. I like it, but I'd like more examples, if I'm not going to get any more, I'll just mark this question as answered. Wish I could +1 already xP
No problem. Honestly, one of the best ways to do it is just mess around with equations. You can get literally any movement patter you want. Check this out for just some examples of things you can do.
Wow, that seems like hard stuff, yet very neat. I'm in 9th grade and we still didn't start with all that sort of stuff yet, we will though next year, guess I'll also have to try harder in maths since I want to code, thanks again!