Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

BCE0051: Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float' !!! 1 Answer

Why am I getting this error? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges