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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ebamber · Dec 08, 2013 at 05:19 PM · javascriptmovementinstantiateai

Problems In Flocking Algorithm

 function Start () {
 }
 
 function Update () {
 raptorarray= GameObject.FindGameObjectsWithTag(raptor.tag);
 
 if ( raptorarray.length == 1)
      return;
 else 
  {
              for (var l = 0;l<raptorarray.length;l++)
              transform.position = transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);        
  }
 }
 
 
 
 function rule1(rap : GameObject) //keep to the average position of the flock
 {
     var pos : Vector3 = Vector3.zero;
     for (var i= 0; i<raptorarray.length;i++)
     {
         pos+=raptorarray[i].transform.position;
     }
     averagepos=pos/raptorarray.Length;
     return averagepos/100;
 }
 
 function rule2(rap : GameObject) //avoid other objects including others in the flock
 {
     var away : Vector3 =Vector3.zero;
     for (var i= 0; i<raptorarray.length;i++)
     {
         var distance :float = Vector3.Distance(rap.transform.position, raptorarray[i].transform.position);
         if (distance < 1)
         keepaway = rap.transform.position + (rap.transform.position - raptorarray[i].transform.position);    
     }
     return keepaway;
 }
 
 function rule3(rap : GameObject) //match the velocity of the others in the flock
 {
     for (var k=0; k<(raptorarray.length);k++)
     {    
         for (var i= 0; i<(raptorarray.length-1);i++)
         {
             if(rap.rigidbody.velocity != raptorarray[i].rigidbody.velocity)
             {
                 rap.rigidbody.velocity+= raptorarray[i].rigidbody.velocity;
                 rap.rigidbody.velocity/=2;
             }
         }
     }
     matchvel=rap.rigidbody.velocity/8;
     return matchvel;
 }

The problems are that whenever the 2nd enemy prefab is instantiated at any time, both the first and the second disappear and I get an error message "transform.position assign attempt for 'Enemy 1(Clone)' is not valid. Input position is { 244287365142033000000000000000000000000.000000, 11839572652397211000000000000000000000.000000, Infinity }. UnityEngine.Transform:set_position(Vector3) Flocking:Update() (at Assets/Flocking.js:20)"

Normally the Enemy AI would randomly roam looking for the character if not closeby and move towards the user if closeby The same message shows in both cases

the bug is allegedly in the line "transform.position = transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);" but since there are 3 procedure calls in that line of code I'm not going to discount the possibility of a bug being in one of the procedures.

I'm also wondering if rule 3 is redundant since all enemies are supposed to be "spawned" with the same velocity

Comment
Add comment · Show 5
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
avatar image robertbu · Dec 08, 2013 at 05:59 PM 0
Share

In 'rule2()', if no objects meet the '(distance < 1)' calculation, then 'keepaway' is never set by this routine. Not knowing how this variable is initialized, I don't know if it is an issue or not.

avatar image Ebamber · Dec 08, 2013 at 06:32 PM 0
Share

I just fixed that issue - but that wasn't the bug I was looking for =/

avatar image robertbu · Dec 08, 2013 at 06:42 PM 0
Share

Just before you set the position, add a Debug.Log to isolate the issue:

 Debug.Log(transform.position + ", " + rule1(raptorarray[l]) + ", " + rule2(raptorarray[l]) + ", " + rule3(raptorarray[l]);
avatar image Ebamber · Dec 08, 2013 at 07:30 PM 0
Share

(1167.6, 32.0, 2206.7), (11.7, 0.6, 22.4), (1167.6, 32.0, 2206.7), (0.0, -0.2, -0.4) UnityEngine.Debug:Log(Object) Flocking:Update() (at Assets/Flocking.js:20)

that was the first error message I got

avatar image robertbu · Dec 08, 2013 at 08:29 PM 0
Share

You are looking for the error message just before you get the error. According to what you outlined, one of the three functions is returning something ugly. This Debug.Log() should tell you which one.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Dec 08, 2013 at 08:30 PM

I've got my doubts about the logic in a couple of sections of your code, but the thing that's actually causing the error is, as you say, here:

 for (var l = 0;l<raptorarray.length;l++)
 transform.position = transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);

You're looping through each element in the raptorarray and then adding the three rules of every element to the transform.position of the gameobject to which this script is attached. This is causing the huge numbers you quote, which would result in an arithmetic overflow.

If this script is attached to a "manager"-type object that's meant to set all the raptor positions on their behalf, then I'm pretty sure what you meant was to loop through each element in the raptorarray and set their own transform.position based on the three rules. Something like:

 for (var l = 0;l<raptorarray.length;l++)
 raptorarray[l].transform.position = raptorarray[l].transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);

Alternatively, if this script is attached to each object in the raptorarray directly, then you need to get rid of

 for (var l = 0;l<raptorarray.length;l++)
Comment
Add comment · Show 1 · Share
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
avatar image Ebamber · Dec 08, 2013 at 09:07 PM 0
Share

The script is attached to a prefab, the prefab is spawned itself rather than something else managing all the enemy prefabs for me - I'm guessing that's what you meant by attached to each object. I arranged that but it's still not working - should I just start the algorithm over?

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

18 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 avatar image avatar image avatar image

Related Questions

Networking Synchronize Problem. 0 Answers

Enemy AI With changing Player 0 Answers

AI walk through solid walls 1 Answer

why is collider not colliding? 1 Answer

How to make an enemy backpedal? 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