Ok, so here is the thing. I’ve been working in a school project where i need to implement an AI working system. This is for an airplane chase situation. So i wrote two scripts, one has the classes i need and the other has the script for the flee behaviour. The only thing that have been frustrating me for the last 4 hours is why the hell my enemy fall in to the ground and how can i fix it, because… it is a plane you see? Is hell falling me from the ground instead of flying towards me… -.-’ Can you help me please?
#pragma strict
// Math
class AiMath {
static function randomBinomial() {
return Random.value - Random.value;
}
static function angleBetween(v1 : Vector3, v2 : Vector3) {
if(v1==null || v2==null)
return 0;
var cross : Vector3 = Vector3.Cross(v1, v2);
var ang : float = Vector3.Angle(v1, v2);
return cross.y < 0 ? ang * -1 : ang;
}
}
//Debugging
class AiDebug {
static function selectObject(object : GameObject) {
if(!object)
return;
var sel : GameObject[] = new GameObject[1];
sel[0] = object;
Selection.objects = sel;
}
}
class Kinematic {
var velocity : Vector3 = Vector3.zero;
var rotation : float;
}
class Steering {
//Aceleracao linear
var linear : Vector3 = Vector3.zero;
//Aceleracao angular
var angular : float;
}
class Character {
var velocity : Vector3 = Vector3.zero;
var rotation : float = 0;
var drag : float = 0.4;
function SimpleUpdate(cc : CharacterController, steering : Steering, maxSpeed : float) {
velocity += steering.linear * Time.deltaTime;
// rotation += steering.angular * Time.deltaTime;
drag = 1 - drag;
var d : float = 0;
if(drag != 1) {
d = Mathf.Pow(drag, Time.deltaTime);
velocity *= d;
// rotation *= d;
}
if(velocity.magnitude>maxSpeed) {
velocity.Normalize();
velocity *= maxSpeed;
}
Debug.Log("Acc: " + steering.linear.magnitude + "m/s², Speed: " + velocity.magnitude + "m/s, Drag: " + d );
cc.SimpleMove(velocity);
// Look at where we're going
var at : Vector3 = cc.transform.position + velocity.normalized;
at.y = cc.transform.position.y;
cc.transform.LookAt(at);
}
/**
* Aply steering rotation
*/
function Update(cc : CharacterController, steering : Steering, maxSpeed : float) {
velocity += steering.linear * Time.deltaTime;
rotation += steering.angular * Time.deltaTime;
drag = 1 - drag;
var d : float = 0;
if(drag != 1) {
d = Mathf.Pow(drag, Time.deltaTime);
velocity *= d;
rotation *= d;
}
if(velocity.magnitude>maxSpeed) {
velocity.Normalize();
velocity *= maxSpeed;
}
Debug.Log("Acc: " + steering.linear.magnitude + "m/s², Speed: " + velocity.magnitude + "m/s, Drag: " + d );
cc.SimpleMove(velocity);
//Debug.Log("Rotation: " + rotation);
var at : Vector3 = cc.transform.forward;
var qRot : Quaternion = Quaternion.AngleAxis(rotation, Vector3.up);
at = qRot * at;
cc.transform.LookAt(cc.transform.position + at);
}
}
And the script to make it chase:
@script RequireComponent(CharacterController)
/*
* As replacement for the classes that handle
* kinematic use Unity API, in this case
* use CharacterController
*/
private var character : CharacterController;
var target : GameObject;
var maxSpeed : float = 4.0;
var debugging : boolean = true;
var isEnabled : boolean = false;
function Start () {
// Fazer 'caching' do valor do CharacterController para nao estar
// a obter frame-a-frame
character = transform.GetComponent.<CharacterController>();
}
function getSteering() {
var steering : Kinematic = new Kinematic();
steering.velocity = transform.position - target.transform.position;
steering.velocity.Normalize();
steering.velocity *= maxSpeed;
return steering;
}
function Update() {
if(!isEnabled)
return;
var steering : Kinematic = getSteering();
if(debugging) {
// Velocidade Atual
Debug.DrawRay(transform.position, steering.velocity, Color.red);
}
// Para ja olhamos na direcao em que estamos a andar
var at : Vector3 = transform.position + steering.velocity;
transform.LookAt(at);
var proximity : Vector3 = target.transform.position - transform.position;
if(proximity.magnitude > 0.2)
character.SimpleMove(steering.velocity);
}