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?