• 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 EarlGreyMatter · Apr 28, 2020 at 01:42 PM · script.lag

How can I improve my boid scripts to reduce lag?

I wrote four scripts for my boids and instantiated a good 50 of them in-game. I found that 30 was kind of a max limit because anything above that lagged unity. I am assuming it has to do with my code. Can I have help improving my code to reduce the lag? DISCLAIMER: I do not have a full understanding of this code because I just learned about it from a youtube video, but I do have a good understanding.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class boidScript : MonoBehaviour 
 {
     public Vector3 velocity;
     public float maxVelocity;
 
     void Start () 
     {
         velocity = this.transform.forward * maxVelocity;
     }
 
     void Update () 
     {
         if (velocity.magnitude > maxVelocity) 
         {
             velocity = velocity.normalized * maxVelocity;
         }
 
         this.transform.position += velocity * Time.deltaTime;
         this.transform.rotation = Quaternion.LookRotation (velocity);
     }
 }




 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class boidCohesion : MonoBehaviour 
 {
     private boidScript boid;
     public float radius;
     
     void Start () 
     {
         boid = GetComponent<boidScript> ();
     }
         
     void Update () 
     {
         boidScript [] boids = FindObjectsOfType<boidScript> ();
         Vector3 average = Vector3.zero;
         int found = 0;
 
         foreach (boidScript b in boids) 
         {
             Vector3 diff = b.transform.position - this.transform.position;
             if (diff.magnitude < radius) 
             {
                 average += diff;
                 found += 1;
             }
         }
 
         if (found > 0) 
         {
             average = average / found;
             boid.velocity += Vector3.Lerp (Vector3.zero, average, average.magnitude / radius);
         }
     }
 }




 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class boidAlignment : MonoBehaviour
 {
     private boidScript boid;
     public float radius;
 
     void Start () 
     {
         boid = GetComponent<boidScript> ();
     }
 
     void Update () 
     {
         boidScript [] boids = FindObjectsOfType<boidScript> ();
         Vector3 average = Vector3.zero;
         int found = 0;
 
         foreach (boidScript b in boids) 
         {
             Vector3 diff = b.transform.position - this.transform.position;
             if (diff.magnitude < radius) 
             {
                 average += b.velocity;
                 found += 1;
             }
         }
 
         if (found > 0) 
         {
             average = average / found;
             boid.velocity += Vector3.Lerp (boid.velocity, average, Time.deltaTime);
         }
     }
 }




 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class boidRepulsionForce : MonoBehaviour 
 {
     private boidScript boid;
     public float radius;
     public float repulsionForce;
 
     void Start () 
     {
         boid = GetComponent<boidScript> ();
     }
 
     void Update () 
     {
         boidScript [] boids = FindObjectsOfType<boidScript> ();
         Vector3 average = Vector3.zero;
         int found = 0;
 
         foreach (boidScript b in boids) 
         {
             Vector3 diff = b.transform.position - this.transform.position;
             if (diff.magnitude < radius) 
             {
                 average += diff;
                 found += 1;
             }
         }
 
         if (found > 0) 
         {
             average = average / found;
             boid.velocity -= Vector3.Lerp (Vector3.zero, average, average.magnitude / radius) * repulsionForce;
         }
     }
 }




 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class boidContainment : MonoBehaviour 
 {
     private boidScript boid;
     public float radius;
     [SerializeField] float boundaryForce;
 
     void Start () 
     {
         boid = GetComponent<boidScript> ();
     }
 
     void Update () 
     {
         if (boid.transform.position.magnitude > radius) 
         {
             boid.velocity += this.transform.position.normalized * (radius - boid.transform.position.magnitude) * boundaryForce * Time.deltaTime;
         }
     }
 }




 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class boidSpawner : MonoBehaviour 
 {
     [SerializeField] int boids;
     [SerializeField] GameObject boid;
     [SerializeField] float radius;
 
     void Start () 
     {
         for (int i = 0; i < boids; i++) 
         {
             Instantiate (boid, this.transform.position + Random.insideUnitSphere * radius, Random.rotation);
         }
     }
 }


Comment
Add comment · Show 1
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 toddisarockstar · Apr 28, 2020 at 10:16 PM 0
Share

in any type of scripting it's very important that fuctions are called anymore often than they have to be called. anything you put in update is going to get called around 60 times a second. if you have this script running on 50 characters then whatever you put in update in your case is run around 3000 times a second! when you call FindObjectsOfType (); it is going to page through all your objects to get a return for you 3000 times a seconds. It would be sufficient to call and store information like this once in some sort of master script when a spawn happens. then store the data where all the scripts can access it. you are breaking this golden rule when you are taking your averages too. this should be done on some sort of master script as well.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

More scripts = more lag? 2 Answers

Scripts freezes game for a few seconds after build. 1 Answer

calculate time required if timescale is changed 0 Answers

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer

Mouse Movement In Pause Menu 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