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 05, 2013 at 10:22 PM · javascriptinstantiatevector3artificial intelligence

Help with my Flocking Algorithm

 @HideInInspector var averagepos : Vector3;
 @HideInInspector var keepaway : Vector3;
 @HideInInspector var matchvel : Vector3;
 var raptor : GameObject;
 var raptorarray : GameObject[];
 
 
 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 ;
     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;
     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;
 }


I pasted all the code because I don't know exactly where my problem is. Basically when one enemy spawns on his own everything's fine, but as soon as the next one spawns, they both disappear and I get the error message "transform.position assign attempt for 'Enemy 1(Clone)' is not valid. Input position is { 244941330874883540000000000000000000000.000000, 13465854283936009000000000000000000000.000000, Infinity }. UnityEngine.Transform:set_position(Vector3) Flocking:Update() (at Assets/Flocking.js:20)"

I get the feeling there's something wrong with my rule1 procedure but I'm not discounting the possibility of the others being buggy too. Can somebody please help?

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

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

Avoiding instantiating two things in same location? 1 Answer

How can I create a follower Ai which is restricted on the Y Axis? 0 Answers

Problems In Flocking Algorithm 1 Answer

I keep getting: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(float, UnityEngine.Vector3, UnityEngine.Quaternion)' was found. 1 Answer

instantiate object and align with object surface 2 Answers


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