C# Creating AI For Top down 2d space shooter

I need some help coming up with an randomize AI script for a 2d top down shooter. The enemies are randomly generated through the following script below. But since the enemies are randomly generated I’m not sure how I would create the AI’s waypoints. Randomly creating waypoints for each enemy would get messy but I also don’t want simply make them to head straight for the player upon their instantiation. I also need to consider how to handle collision detection since I don’t want individual enemies colliding into each other. Do you guys have any suggestions for methods of creating AI?

    public void SpawnRandom()
    {
        Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new
        Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), 1));
        screenPosition = Camera.main.ViewportToWorldPoint(screenPosition);
        Instantiate(enemy,screenPosition,Quaternion.identity);
    }

To avoid colliding with other enemies you can simply ignor their collisions using

Physics.IgnoreLayerCollision(this.gameObject.layer, this.gameObject.layer);

Place that in the Start() method and anyone with similar layer tags, will not bump into each other.

As for the A.I. I am also currently finding a way to build a simple conclusion to this problem. Maybe we can help each other out.

As of right now my simple theory is have the canvas of the game be static with each wave make their way down the screen. The “Wave” Gameobject will have spawn nodes attached to it, these nodes spawn enemies when the wave reaches set destination. My wording seems a bit off but it makes sense in my head :wink: But as for direction and avoidance, I am still working on it.

Assets from the store.

For something to start, you could just have each ship find one random destination and fly towards it, and then, check distance to player every second or so, and break off if the distance was close enough. If it gets to destination, choose another.

Steering behaviours might help here.

I’m doing something similar. I used iTween to define about 10 different attack paths, mostly symmetrical left and right. I also defined some single attack paths for supply ships and bosses. Depending on the enemy I randomly generate, I choose one of the paths and let iTween handle things for me. I add some randomisation to the duration of the tween and also plan to reduce the tween time at higher stages to make the enemy go faster. Not perfect but it is playable.

Hth.

I found a steering behaviour script and have been trying to improve/condense it. When I found it was rather messy so I cleaned up a lot of the syntax and removed a bunch of unnecessary variables. However I’m having trouble trying to condense the following code below. I at first thought I could use a vector3 list to store the vector3s then use a for loop to call all of the vector3s. But I can’t reference the vector3s since they are nonstatic. I also tried turning the vector3s into functions but for some reason I was unable to get them to run.

    //Calculate() returns all steering forces
    public Vector3 Calculate()
    {
        Vector3 total_steering_force = Vector3.zero;
        switch(currentSteeringParameter){
        case "Seek":
            total_steering_force += Seek(target.transform.position);
        break;
        case "arrive":
            total_steering_force += Arrive(target.transform.position);
        break;
        case "flee":
            total_steering_force += Flee(target.transform.position);
        break;
        case "pursuit":
            total_steering_force += Pursuit();
        break;
        case "evade":
            total_steering_force += Evade();
        break;
        case "wander":
            total_steering_force += Wander();
        break;
        case "avoid":
            total_steering_force += Avoid();
        break;
        case "hide":
            total_steering_force += Hide(HideObject,target.transform.position);
        break;          
        case "alignment":
            enemy_list = GameObject.FindGameObjectsWithTag("Player");
            TagNeighbors();
            total_steering_force += Alignment();
        break;
        case "cohesion":
            enemy_list = GameObject.FindGameObjectsWithTag("Player");
            TagNeighbors();
            total_steering_force += Cohesion();
        break;
        case "separation":
            enemy_list = GameObject.FindGameObjectsWithTag("Player");
            TagNeighbors();
            total_steering_force += Separation();
        break;
        }
        return total_steering_force;
    }

1931552–124812–SteeringBehavior.cs (12.4 KB)