Spaceship Ai

Hello I’ve been working on a freelancer or freespace style game and I’ve programmed a basic Ai movement script but I don’t know if it’s the right way to do it… It’s my first time programming something like that…:slight_smile: Here is what I’ve got so far : https://sites.google.com/site/dimitrijejankov/3d-space-shooter The movement script uses a modified version of the boids algorithm…
Here is the source :

using UnityEngine;
using System.Collections;

[RequireComponent(typeof (Rigidbody))]

public class EnemyMovement : MonoBehaviour {

	public Transform target;
	
	Vector3 acceleration;
	Vector3 velocty;
	float speed = 5.0f;
	
	GameObject[] boids;

	
	// Use this for initialization
	void Start () {
		
		boids = GameObject.FindGameObjectsWithTag("Enemy");
		
		//velocty = (target.position - transform.position).normalized * speed;
		
	}
	
	// Update is called once per frame
	void Update () {
		
		Vector3 r1 = Rule_1();
		Vector3 r2 = Rule_2();
		Vector3 r3 = Rule_3();
		
		acceleration = r1 + r2 + r3;
		velocty += 2 * acceleration * Time.deltaTime;
		
		if(velocty.magnitude > speed)
			velocty = velocty.normalized * speed;
		rigidbody.velocity = velocty;
		
		Quaternion desiredRotation = Quaternion.LookRotation(velocty);
		transform.rotation = Quaternion.Slerp(transform.rotation,desiredRotation,Time.deltaTime * 3);
	}
	
	
	Vector3 Rule_1(){
		
		Vector3 distance = target.position - transform.position;
		
		if(distance.magnitude < 3)
			return distance.normalized * -12;
		else
			return distance.normalized * 2;
	}
	
	Vector3 Rule_2(){
		
		if(!Physics.Raycast(transform.position,transform.forward,2.0f)){
			return -transform.up ;
		}
		
		return Vector3.zero;
	}
	
	Vector3 Rule_3(){

		Vector3 c = Vector3.zero;
		
		foreach (GameObject g in boids){
			if(g.transform.position != transform.position){
				if((g.transform.position - transform.position).magnitude < 1.0f)
				{
					c -= (g.transform.position - transform.position);
				}
			}
			
		}

		return c * 3.0f;
	 
	}
}

Any kind of help would be great :smile:

It’s a very good start (the models are pretty sweet too), however you need to add another rule to the algorithm that will make the boids more likely to point at the player, as their shooting is rather unrealistic at the moment. Other than that, a decent start to your game! :slight_smile:

What’s the license on this script? I’d like to use it in my game.

I’m totally new with AI but with your script and my skills i made a pretty cool result

I add a function to detect if the ship can shoot or not using Vector3.Angle and Vector3.Distance and a new rotation system made by me.