[Help] Moving Enemies around?

Hello guys,

I have a question, it’s a follow up question to one I posted earlier, though this is quite different. Fortunately I had a significant amount of help from one user on the forum on the previous one and resolved it quickly.

So, this is a description on what I’ve done, I have a gameobject that moves side to side much like the main formation - had to remove it from the main formation as is serves a specific function. It wouldn’t play nicely with the animator so I had to remove that component as well so there’s no Arrival animation like the formation. After a certain amount of time has passed the enemy dives directly at the hero ship - I wanted that to occur.

Now what I want to occur is if the enemy survives the dive - that is the hero ship doesn’t shoot him or moves out of the way - I need the ship to reappear at the top and then moves back into the side to side motion without using the Animator. I can’t use the Animator because I need to hard code the direction of attack and no matter what I tried the enemy won’t work with it? I have individual members of the formation diving, but, they all follow the same set path relative to their position and that works well with the Animator.

So in a nutshell what should I look at doing? Hard code everything? Like code the arrival from a spawn point, the movement into side to side, then move to enemy back to the top of the screen and then somehow try and link it back up with the spawn point and arrival code. Or, is there a way to avoid that or better way to do it?

As always any help, tips, links, or any tutorials that may help are greatly appreciated!

Regards,
Vaughan.

Hello again :smiley:

Really dunno if I’ve got it right.
But maybe simple way to do it would be set new parents to enemies once all the objects aren’t needed instead of destroying them, and then simply spawn new enemy into undestroyed object?

If i’m understanding what you are saying - I think that would be a good workaround it’s not exactly what I’m after - but, this would be incredibly easy and at this point I’ll take easy :laughing:

This is how the spawning works in Glitch Garden. There are effectively spawners which are off of the viewable play space, you instantiate a new enemy within the desired spawner.

I’m not sure I understand where the difficulty is. Which part exactly are you having trouble with?

It’s a multistep process, a little like the state machines from text 101. You have a ReadyToDive state and an IsDiving state and a HasDived state. You have three different behaviors to call on for each different state, but the individual behaviors don’t seem very complicated. Unless I’m missing something.

Are you trying to rejoin the formation after an unsuccessful dive?

It sounds like your problem with the animator might be its root motion. Make sure that’s not checkparked so that the animator doesn’t play with its position when you are trusting your code to do that. This way you can still have animation effects and changes, but your position movement will be based on your code, not how it was animated.

(I think you can find the checkmark by clicking on the animation file/icon in the Project window. If I’m wrong about that, then it’s an option in the animation controller. But it’s called “root motion” and you don’t want it checkmarked in this particular case.)

If your animator is just making them fly in, then obviously turning off root motion will also turn off this behavior. But if they were doing something else while flying in, you could animate the “something else” this way while your code flies them in.

I have the boolean value set for hasDived that is working. The ship initially moves sideways, and when the random amount of time has elapsed the ship dives to the position of the hero ship at that time - the problem I am having is getting the ship to arrive at whatever position to move into sideways formation, and what be the best process for Arrival & at the exact position on the x co-ord at the top of the screen where it went offscreen without instantiating another object. Yes, I could probably do it if I really had time to sit down and code it out, but, with my limited experience it wouldn’t be pretty and it sure as hell wouldn’t be efficient - Rob can attest to that - and there might be a better way of doing it, hence the question.

From my point of view this is difficult and having you say that there is no difficulty is well not helping matters :rage:. Especially after receiving a rather large knock at Jujustsu training tonight and my patience is extremely thin at the moment. Maybe it’s difficult because I have had one to many head knocks over the years.

Who ****** knows - perhaps you are also an expert on repeated head trauma.

I wasn’t trying to insult you. I was just pointing out that a problem like this can be broken into several smaller steps that can be addressed one by one. I want to know what step you are having problems with so I can help you. If it’s the first step, that’s okay. I just need to know where in the process you are. I’m not an expert, but I’ve been working on my own version of Laser Defender for awhile and have been playing with advanced flight patterns over the last couple weeks.


At some point, your enemy is instantiated off-screen somewhere, likely above the top of the screen. In my game, I have a Vector3 point called “destination” and an enemy is flying toward that point. When they reach that point (or just get close), their status can change from “Arriving” to “SideToSide” causing them to begin their side to side movement. You can do this by continually changing the coordinates of the “destination” point every time they get near it. When they get close, call a method that changes “destination” to a new point on the other side of the screen. The critieria for the new destination point is based on your Enemy’s current state (Arriving,SideToSide,Diving,HasDived,etc.)

Using this system, I’ve been able to dynamically tweak the flight patterns of all my enemies in code instead of relying on pre-determined flight paths. This allows for complex maneuvers like you are describing.

At some point, perhaps on a timer, or just when the player’s X position is close enough, they can begin a dive toward the player. “Destination” will be changed to the player’s position. If they miss, they will pass the player and fly toward the bottom of the screen. Getting them to actually turn around and fly back toward the top of the screen is a bit challenging. Rotations give me a slight headache. It’s not that hard (nothing really is after awhile), it’s just another thing to learn and I’ve been focusing on other areas. It’s hard to do by code, but it could be done easier by fussing with the animator once you are used to how it works – which likely won’t be until after the next section, Glitch Garden.

An easier solution to turning around would be just to have it continue flying off the screen, and then move its position back to its place above the top of the screen and change its state back to “Arriving”. (Essentially, you “teleport” the ship back to its original place.) If necessary, you can add a “HasDived” boolean to make sure it doesn’t dive again.


To move toward something, you need three things. First, you need a vector going from your current position to another point or position.

vector3 VectorToDestination = destinationPoint - transform.position;
//destinationPoint can be another Vector3 you’ve constructed or could be the transform.position of another object.

This vector3 will represent both direction and distance. You’ll need to be comfortable with separating those out. A vector3.magnitude will get you the distance, and a vector3.normalized will get you the direction.

float distanceToDestination = VectorToDestination.magnitude;
Vector3 directionToDestination = VectorToDestination.normalized;

With the direction and a float to represent your speed, you have everything you need to fly “toward” something. You can use the distance to continuously check when you are close to your destination (so you can decide on your next destination and start the process over).

transform.position = directionToDestination * Speed * Time.deltaTime;

3 Likes

You can easily do that using one child for each kind of independent movement. Just make a empty parent that has a trigger that makes it fall (you can use the animator for that, no problem at all), and then you can have the ship as a child, you can even make both children of the regular formation.

You can use a coroutine to call the method to make the ship move towards the player. you can use a trigger collider too if you want to verify when the ship has passed by the player, you can make everything through the animation too if you want (the proccess of diving and returning on a single animation), there are a hundred ways to do that actually :joy:, many roads lead to Rome.

Updated Mon Mar 13 2017 15:47

The way I would do it? I’d make a empty formation that have the ship as child and that have a script like this:


public Animator myAnim;
public float secondsToDive;


void Start()
{
StartCoroutine(timerToDive());
}
IEnumerator timerToDive()
{
yield return new WaitForSeconds(secondsToDive);
myAnim.SetTrigger("Dive");
StartCoroutine(timerToDive());
}

Haven’t test it out but it should work (sorry for any miss spelling), would just need the animator with the trigger to call the Dive animation

2 Likes