Im making a side scrolling shoot em up, I have almost finished my first level but have yet to create a boss, but i’m not really sure how to make a boss move around the screen. I have set up a script which stops a boss from leaving the edges of the screen, I dont need to worry about him attacking the player at this stage, I just need him to randomly move around along the Y and Z axis, does anyone know the easiest way I can achieve this?
Here is my script so far (which simply clamps him to the screen boundary):
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary2
{
public float yMin, yMax, zMin, zMax;
}
public class BossAI : MonoBehaviour
{
public Boundary2 boundary;
void FixedUpdate()
{
GetComponent<Rigidbody>().position = new Vector3
(
0.0f,
Mathf.Clamp(GetComponent<Rigidbody>().position.y, boundary.yMin, boundary.yMax),
Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
}
}
This is a pretty deep question and gets into AI and behavior, so I’ll just throw out a few light ideas:
pick a random spot, teleport to that spot, stop, wait, repeat
pick a random spot, begin moving towards that point, stop when you reach it, wait, repeat
pick a random direction and choose a distance: turn and walk in that direction until you reach the distance, stop, wait, repeat
Think about what you want to do, and perhaps go play another game that you are trying to imitate, and ask yourself, “What does it seem like this boss is doing?”
It’s hard to tell precisely what the enemy is doing because the screen scrolls a bit too, but it appears to be picking random locations in the right 2/3rds of the screen, then moving towards them.
On top of that he may have some extra bobbing and weaving going on, perhaps anchored to his notional “current” position in space, almost like he’s leaning far off a constantly moving barstool or something, to give a more erratic appearance.
I’d try just picking random spots, moving to them, then pick a new spot and repeat.
One handy way to use the Unity editor to specify stuff like this is put an invisible GameObject at the lower left corner of the movement rectangle you want him to use, and another at the upper right, then in your code you use the positions of those two invisible GameObjects to generate random locations for him to move.
That way your code doesn’t have to change, and you can change his movement limits right in the editor quickly and easily until you have the feel you want. You could just drive your current xMin/zMax variables with the contents of those two “limit” GameObjects.
That makes sense, but i’m still unsure on what script to use to actually make him move? At the moment my enemies either move by simply travelling from the right of the screen to the left, with some drifting up or down slightly, while others use a waypoint system. But neither of these systems can be used with a boss… what command/s should I be looking at using?
The enemy has some sort of entry sequence, which moves it to a specific part of the screen
Afterwards, it keeps track of the player’s position and sets new waypoints accordingly (seems like it wants to always match the same Y coordinate, at least within an acceptable offset)
Hence, you should start by creating a dedicated movement script placed on the enemy prefab. As the enemy gets instantiated, the script gets automatically activated. Then, you can have the OnEnable() part assign it the first waypoint on the screen. OnEnable() could then call a delayed coroutine function that updates the waypoint depending on the transform of the player, e.g. always the same Y and at least Z units away from the player’s X. Apply some random values here and there for a less mechanic look.
In LateUpdate(), for example, you could then apply force on the enemy’s rigidbody to move it towards the waypoint defined/updated by your coroutine. At the very beginning, your enemy would then head straight for the initial waypoint until the coroutine kicks in. If you are not using phyiscs, then you’d have to translate the transform towards the position, although I believe that this is generally discouraged from.
This is very helpful thanks. Unfortunately I still dont have a clue where to start with this, i’ve only been using Unity for around a month, so I still have a lot to learn. I might have to check out some more tutorials (especially some with enemy ai) before attempting to go further with this. I have almost everything else I want to do nailed down, but this is a little advance for me at the moment.