How to get object to face direction it's flying rather than looking at the target

I wrote some of my own AI code using some help from a source on the internet, It works fine including with the floating origin solution for large game worlds, problem is however is that the AI will always face the target rather than face the direction in which it is travelling.

I have provided a picture to explain what i’m trying to do.

And the code that is being used, I have only done it up to the Seek behavior as all the behaviors use the same basic template.

#pragma strict

//Positional Values

var position : Vector3;
var desiredVelocity : Vector3;
var velocity : Vector3;
var steering : Vector3;
var avoidance : Vector3;
var direction : Vector3;
var targetFuturePosition : Vector3;

//Speed, Mass and Force Values

var mass : float;
var maxVelocity : float;
var maxForce : float;
var speedx : float;
var speedy : float;
var speedz : float;
var distance : float;
var slowingRadius : float;

//Target Holders

var target : Transform;

//Decision Holders

var avoidObject : boolean = false;

function OnTriggerStay (collider : Collider)

{
	avoidObject = true;
	Avoid(collider.gameObject);
}	

function OnTriggerExit ()

{
	avoidObject = false;
}

function Avoid (obstacle : GameObject)

{	
	avoidance = (transform.position - obstacle.transform.position).normalized * maxVelocity;;
	steering = avoidance - velocity;
	steering = steering * maxForce;
	steering = steering / mass;
	velocity = (velocity + steering).normalized * maxVelocity;
	position = transform.position + velocity;
	direction = transform.position + velocity;
	speedx = velocity.x;
	speedy = velocity.y;
	speedz = velocity.z;
	transform.Translate (speedx * Time.deltaTime, speedy * Time.deltaTime, speedz * Time.deltaTime);
}


function Seek () 

{
	desiredVelocity = (target.position - transform.position).normalized * maxVelocity;
	steering = desiredVelocity - velocity;
	steering = steering * maxForce;
	steering = steering / mass;
	velocity = (velocity + steering).normalized * maxVelocity;
	position = transform.position + velocity;
	direction = transform.position + desiredVelocity;
	speedx = velocity.x;
	speedy = velocity.y;
	speedz = velocity.z;
	transform.Translate (speedx * Time.deltaTime, speedy * Time.deltaTime, speedz * Time.deltaTime);
}

and this code is to make the object look in the direction the AI brain is facing, i.e, which direction it’s looking at.

#pragma strict

var brain : Transform;
var brainDirection : Vector3;

function Start () 

{

}

function Update () 

{
	//brainDirection = brain.GetComponent(Seek).direction;
	brainDirection = brain.GetComponent(Behaviours).direction;
	transform.LookAt(brainDirection);
}

Any solutions?

If brainDirection is a direction then use Quaternion.LookRotation which takes a direction - LookAt looks at a place in the world.

   transform.rotation = Quaternion.LookRotation(brainDirection);

It seems that you are confusing direction vectors with target vectors.

If I read your code correctly, brainDirection is a directional vector that you imagine as attached to your AI. If you call “transform.lookAt” with this vector as a parameter, unity will intepret it as an absolute vector instead and point the object there.

Solution: Add the direction to your current location and this will be your target to look at.

function Update () 
 
{
    //brainDirection = brain.GetComponent(Seek).direction;
    brainDirection = brain.GetComponent(Behaviours).direction;
    transform.LookAt(brainDirection + transform.position);
}