Spaceship AI

I’m new to unity development, but I know some Java. I wanted to modify the spaceshooter game of the unity official tutorials and make a 3D version. I managed to create a space ambientation with random spawn of asteroids near the player, but I have a problem with the AI of the enemy.
I want him to chase the player and shoot at him, but the player can also shot him and chase him. I scripted the enemy to follow the player and shoot him from a certain distance, but he is too strong. I mean, i never killed him and always get killed.
Do you have any idea of how i could solve the situation? Perhaps programming him not to always chase the player, but also to have some sort of ‘survival istinct’ and do some evasive moves or not shoot continuosly while he is close to the player…

This is the script:

    public Transform shotSpawn;
    public GameObject enemyShot;
    public GameObject enemyExplosion;

    public bool canShoot = true;
    public float moveSpeed = 100;
    public float maxDist = 150;
    public float minDist = 30;
    public float shotDist = 60;

    public float fireRate = 0.25f;
    private float nextFire = 0.0f;

    private Transform player;

    void Start () {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }
   
    void Update () {

        //move towards the player
        float distance = Vector3.Distance(transform.position, player.position);

        if(distance < maxDist) {
            transform.LookAt(player);
            if(distance > minDist) {
                transform.position += transform.forward * moveSpeed * Time.deltaTime;
            }
        }
        if (distance <= shotDist) {
            if (canShoot) {
                Shoot();
            }
        }
    }
    void Shoot() {
        //fire
        if (Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            Instantiate(enemyShot, shotSpawn.position, shotSpawn.rotation);
        }
    }

might be of interest:

(you don’t have to use forces, the idea of combining the various directions into a final “go this way” is the more important :slight_smile: )

Thank you, I’ll try! :slight_smile: