- Home /
Question by
Brian5cbis · Aug 23, 2013 at 04:36 AM ·
errorai
A Very Unusual Error
Hello I think I need some help on this piece of code here... I want to make an ally ai based on unity FPX 1.5. I copied the enemy ai script and edited to set the target to the enemy in order to make the ally ai attack enemies.. Here is the code:
Enemy AI
var Target : Transform;
//
private var myTransform : Transform;
private var controller : CharacterController;
var Damage : int = 1;
var Range : int = 50;
var seekMod : float = .80;
var dist : float;
//Shooting
var Bullet : Rigidbody;
var shotPos : Transform;
var shotSound : AudioClip;
var shotSpeed : float = .02;
var shotAccuracy : float = .02;
var Shoot : boolean = true;
private var shooting : boolean = false;
var dIrection = -1;
var maxDistance : float = 5;
var rotateSpeed : float = 10;
var directionDistance : float = 5;
var targetDistance :float = 5;
var followSpeed : float = 11;
//
//Effects
var Flash : boolean = true;
var Light : Light;
var origColor : Color;
var FlashColor : Color;
var line : ParticleEmitter;
//Movement & Rotation Variables
var Speed : float = 5;
private var Rot : Quaternion;
private var NextPos : Vector3;
private var patrolling : boolean = false;
//Animations
var Animate : boolean = false;
var animator : GameObject;
var Animations : AnimationClip[];
//---------------------------------------------------------------------------------//
function Awake (){
// Find Self
gameObject.AddComponent("AudioSource");
//gameObject.AddComponent(CharacterController);
//controller = GetComponent(CharacterController);
if(line){
line.transform.position = shotPos.transform.position;
line.emit = true;
}
//line.transform.parent = shotPos.transform;
myTransform = transform;
//Find Target
if(!Target){
Target =FindTarget();
}
//Get Out First Destination
NextPos = Patrol();
}
function Update () {
CheckGrounded();
if(!Target){
Target = FindTarget(); return;
}
//Stay Up
// If we don't see the Target (Patrol/Move)----------------------------------------------------------------------------------//
if(!CanSeeTarget()){
//Restore color
if(Flash){
Light.color = origColor;
Light.intensity = 5;
}
if(Animate){
animator.animation.CrossFade(Animations[2].name, .2);
}
if(Vector3.Distance(myTransform.position,NextPos) < Random.Range(1,10)){
NextPos = Patrol();
}
myTransform.Translate(Vector3.forward * Time.deltaTime * Speed,Space.Self);
var roto = Quaternion.LookRotation(NextPos - myTransform.position,Vector3.up);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,roto,Time.deltaTime * 5);
}
// We Do see the Target----------------------------------------------------------------------------------------------//
else if(CanSeeTarget()){
//Set Our Position
NextPos = Target.position;
//Flash
if(Flash){
var lerp : float = Mathf.PingPong (Time.time, .5 / .5);
Light.color = Color.Lerp (origColor, FlashColor, lerp);
Light.intensity = 8;
}
//Animate
if(Animate){
animator.animation.CrossFade(Animations[1].name, .2);
}
myTransform.Translate(Vector3.forward * Time.deltaTime * Speed,Space.Self);
roto = Quaternion.LookRotation(NextPos - myTransform.position,Vector3.up);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,roto,Time.deltaTime * 5);
//Move Towards Shoot/non Shoot
if(Shoot){
if(Vector3.Distance(myTransform.position,Target.position) < Range/2){
if (!shooting){
ShootProjectile();
}
myTransform.Translate(-Vector3.forward * Time.deltaTime * Speed,Space.Self);
}
}else{
if(Vector3.Distance(myTransform.position,Target.position) < .5){
Target.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
//Check Distance between current object and Target
dist = Vector3.Distance(Target.position, transform.position);
//print("Distance to other:" +dist);
// If distance is bigger then distance between target, wander around.
if (dist > targetDistance)
{
//Check if there is a collider in a certain distance of the object if not then do the following
if(!Physics.Raycast(transform.position, transform.forward, maxDistance))
{
// Move forward
transform.Translate(Vector3.forward * Speed * Time.smoothDeltaTime);
}
else
{
// If there is a object at the right side of the object then give a random direction
if(Physics.Raycast(transform.position, transform.right, directionDistance))
{
dIrection = -1;
}
// If there is a object at the left side of the object then give a random direction
if(Physics.Raycast(transform.position, -transform.right, directionDistance))
{
dIrection = 2;
}
// rotate 90 degrees in the random direction
transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime * dIrection);
}
}
// If current distance is smaller than the given ditance, then rotate towards player, and translate the rotation into forward motion times the given speed
if (dist < targetDistance)
{
transform.LookAt(Target);
transform.Translate(Vector3.forward * followSpeed * Time.smoothDeltaTime);
}
}
//-----------------------------------------------------------------------------------------------------------------------//
function CanSeeTarget () : boolean{
// If we are Too Far
if(Target != null){
if(Vector3.Distance(Target.position, myTransform.position) > Range){
//return false;
return true;
}
// If we are close Check, Also see if there are objects
else{
//Cast For Objects
var hit : RaycastHit;
if(Physics.Linecast(myTransform.position,Target.position,hit)){
if(hit.collider.gameObject.tag != "Player"){
//return false;
return false;
}
else{
return true;
}
}
}
}
}
function FindTarget () : Transform {
if(GameObject.FindWithTag("Player")){
who = GameObject.FindWithTag("Player"); //We have found a Target
}
return who.transform;
}
function CheckGrounded(){
var hit : RaycastHit;
if(Physics.Raycast(myTransform.position,myTransform.up,hit,.5)){
rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
myTransform.rotation.x = Mathf.Lerp(myTransform.rotation.x,rotation.x,Time.deltaTime * 15);
myTransform.rotation.z = Mathf.Lerp(myTransform.rotation.z,rotation.z,Time.deltaTime * 15);
}else{
myTransform.rotation.x = Mathf.Lerp(myTransform.rotation.x,0,Time.deltaTime * 15);
myTransform.rotation.z = Mathf.Lerp(myTransform.rotation.z,0,Time.deltaTime * 15);
}
}
//---------------------------------------------------------------------------------//
//Shoot Player ----------------------------------------------------------------------//
function ShootProjectile(){
if(Animate && Shoot){
animator.animation.CrossFade(Animations[1].name, .2);
}
if(Shoot){
var direction : Vector3 = Spread();
//-----
shooting = true;
audio.clip = shotSound;
audio.Play();
///-----
//var Shot : Rigidbody = Instantiate (Bullet, shotPos.transform.position,Quaternion.identity);
// Shot.velocity = direction * 50;
if(line){
line.emit = true;
line.worldVelocity = direction * Time.deltaTime * 1000;
yield WaitForSeconds(.01);
line.emit = false;
}
//RayCast-----------------------------------------/
var hit : RaycastHit;
// Did we hit
if (Physics.Raycast (shotPos.transform.position, direction, hit, Range/2)){
hit.collider.SendMessageUpwards("ApplyDamage", Damage/2, SendMessageOptions.DontRequireReceiver);
// Apply hit to Rigid Body
if (hit.rigidbody ){
//hit.rigidbody.AddForceAtPosition(500 * direction * Damage , hit.point);
}
}
//Stop Shooting----------------------------/
yield WaitForSeconds (shotSpeed);
shooting = false;
}
}
function Spread (){
var vx = (1 - 2 * Random.value) * shotAccuracy;
var vy = (1 - 2 * Random.value) * shotAccuracy;
var vz = 1.0;
return shotPos.transform.TransformDirection(Vector3(vx,vy,vz));
}
//Collision With Object------------------------------------------------------------//
function OnCollisionStay (colide : Collision){
//Move back
Patrol();
}
//Patrol-------------------------------------------------------------------------//
function Patrol (): Vector3{
var place : Vector2 = Random.insideUnitCircle * Range * 10000;
var pos : Vector3 = new Vector3(place.x, myTransform.position.y, place.y);
return pos;
}
Ally AI
var Target : Transform;
//
private var myTransform : Transform;
private var controller : CharacterController;
var Damage : int = 1;
var Range : int = 50;
var seekMod : float = .80;
var dist : float;
//Shooting
var Bullet : Rigidbody;
var shotPos : Transform;
var shotSound : AudioClip;
var shotSpeed : float = .02;
var shotAccuracy : float = .02;
var Shoot : boolean = true;
private var shooting : boolean = false;
var dIrection = -1;
var maxDistance : float = 5;
var rotateSpeed : float = 10;
var directionDistance : float = 5;
var targetDistance :float = 5;
var followSpeed : float = 11;
//
//Effects
var Flash : boolean = true;
var Light : Light;
var origColor : Color;
var FlashColor : Color;
var line : ParticleEmitter;
//Movement & Rotation Variables
var Speed : float = 5;
private var Rot : Quaternion;
private var NextPos : Vector3;
private var patrolling : boolean = false;
//Animations
var Animate : boolean = false;
var animator : GameObject;
var Animations : AnimationClip[];
//---------------------------------------------------------------------------------//
function Awake (){
// Find Self
gameObject.AddComponent("AudioSource");
//gameObject.AddComponent(CharacterController);
//controller = GetComponent(CharacterController);
if(line){
line.transform.position = shotPos.transform.position;
line.emit = true;
}
//line.transform.parent = shotPos.transform;
myTransform = transform;
//Find Target
if(!Target){
Target =FindTarget();
}
//Get Out First Destination
NextPos = Patrol();
}
function Update () {
if(!Target){
Target = FindTarget(); return;
}
CheckGrounded();
//Stay Up
// If we don't see the Target (Patrol/Move)----------------------------------------------------------------------------------//
if(!CanSeeTarget()){
//Restore color
if(Flash){
Light.color = origColor;
Light.intensity = 5;
}
if(Animate){
animator.animation.CrossFade(Animations[2].name, .2);
}
if(Vector3.Distance(myTransform.position,NextPos) < Random.Range(1,10)){
NextPos = Patrol();
}
myTransform.Translate(Vector3.forward * Time.deltaTime * Speed,Space.Self);
var roto = Quaternion.LookRotation(NextPos - myTransform.position,Vector3.up);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,roto,Time.deltaTime * 5);
}
// We Do see the Target----------------------------------------------------------------------------------------------//
else if(CanSeeTarget()){
//Set Our Position
NextPos = Target.position;
//Flash
if(Flash){
var lerp : float = Mathf.PingPong (Time.time, .5 / .5);
Light.color = Color.Lerp (origColor, FlashColor, lerp);
Light.intensity = 8;
}
//Animate
if(Animate){
animator.animation.CrossFade(Animations[1].name, .2);
}
myTransform.Translate(Vector3.forward * Time.deltaTime * Speed,Space.Self);
roto = Quaternion.LookRotation(NextPos - myTransform.position,Vector3.up);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,roto,Time.deltaTime * 5);
//Move Towards Shoot/non Shoot
if(Shoot){
if(Vector3.Distance(myTransform.position,Target.position) < Range/2){
if (!shooting){
ShootProjectile();
}
myTransform.Translate(-Vector3.forward * Time.deltaTime * Speed,Space.Self);
}
}else{
if(Vector3.Distance(myTransform.position,Target.position) < .5){
Target.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
//Check Distance between current object and Target
dist = Vector3.Distance(Target.position, transform.position);
//print("Distance to other:" +dist);
// If distance is bigger then distance between target, wander around.
if (dist > targetDistance)
{
//Check if there is a collider in a certain distance of the object if not then do the following
if(!Physics.Raycast(transform.position, transform.forward, maxDistance))
{
// Move forward
transform.Translate(Vector3.forward * Speed * Time.smoothDeltaTime);
}
else
{
// If there is a object at the right side of the object then give a random direction
if(Physics.Raycast(transform.position, transform.right, directionDistance))
{
dIrection = -1;
}
// If there is a object at the left side of the object then give a random direction
if(Physics.Raycast(transform.position, -transform.right, directionDistance))
{
dIrection = 2;
}
// rotate 90 degrees in the random direction
transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime * dIrection);
}
}
// If current distance is smaller than the given ditance, then rotate towards player, and translate the rotation into forward motion times the given speed
if (dist < targetDistance)
{
transform.LookAt(Target);
transform.Translate(Vector3.forward * followSpeed * Time.smoothDeltaTime);
}
}
function FindTarget () : Transform {
if(GameObject.FindWithTag("Enemy")){
who = GameObject.FindWithTag("Enemy"); //We have found a Target
}
return who.transform;
}
function CheckGrounded(){
var hit : RaycastHit;
if(Physics.Raycast(myTransform.position,myTransform.up,hit,.5)){
rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
myTransform.rotation.x = Mathf.Lerp(myTransform.rotation.x,rotation.x,Time.deltaTime * 15);
myTransform.rotation.z = Mathf.Lerp(myTransform.rotation.z,rotation.z,Time.deltaTime * 15);
}else{
myTransform.rotation.x = Mathf.Lerp(myTransform.rotation.x,0,Time.deltaTime * 15);
myTransform.rotation.z = Mathf.Lerp(myTransform.rotation.z,0,Time.deltaTime * 15);
}
}
//---------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------------------------------------------//
function CanSeeTarget () : boolean{
// If we are Too Far
if(Target != null){
if(Vector3.Distance(Target.position, myTransform.position) > Range){
//return false;
return true;
}
// If we are close Check, Also see if there are objects
else{
//Cast For Objects
var hit : RaycastHit;
if(Physics.Linecast(myTransform.position,Target.position,hit)){
if(hit.collider.gameObject.tag != "Enemy"){
//return false;
return false;
}
else{
return true;
}
}
}
}
}
//Shoot Player ----------------------------------------------------------------------//
function ShootProjectile(){
if(Animate && Shoot){
animator.animation.CrossFade(Animations[1].name, .2);
}
if(Shoot){
var direction : Vector3 = Spread();
//-----
shooting = true;
audio.clip = shotSound;
audio.Play();
///-----
//var Shot : Rigidbody = Instantiate (Bullet, shotPos.transform.position,Quaternion.identity);
// Shot.velocity = direction * 50;
if(line){
line.emit = true;
line.worldVelocity = direction * Time.deltaTime * 1000;
yield WaitForSeconds(.01);
line.emit = false;
}
//RayCast-----------------------------------------/
var hit : RaycastHit;
// Did we hit
if (Physics.Raycast (shotPos.transform.position, direction, hit, Range/2)){
hit.collider.SendMessageUpwards("ApplyDamage", Damage/2, SendMessageOptions.DontRequireReceiver);
// Apply hit to Rigid Body
if (hit.rigidbody ){
//hit.rigidbody.AddForceAtPosition(500 * direction * Damage , hit.point);
}
}
//Stop Shooting----------------------------/
yield WaitForSeconds (shotSpeed);
shooting = false;
}
}
function Spread (){
var vx = (1 - 2 * Random.value) * shotAccuracy;
var vy = (1 - 2 * Random.value) * shotAccuracy;
var vz = 1.0;
return shotPos.transform.TransformDirection(Vector3(vx,vy,vz));
}
//Collision With Object------------------------------------------------------------//
function OnCollisionStay (colide : Collision){
//Move back
Patrol();
}
//Patrol-------------------------------------------------------------------------//
function Patrol (): Vector3{
var place : Vector2 = Random.insideUnitCircle * Range * 10000;
var pos : Vector3 = new Vector3(place.x, myTransform.position.y, place.y);
return pos;
}
As you can see I just edited the targets and the damage recievers, but when I put the ally ai script to the dummies, they just stop there... and don't do anything... Please help Thank you
Comment
Your answer
Follow this Question
Related Questions
What's wrong with my AI script. 3 Answers
Continual position error 0 Answers
Need help calling a variable from another C# script 1 Answer
Why am I getting this error? 1 Answer