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 BobbleHead · Sep 15, 2012 at 09:02 AM · javascriptprofilerlookat

Slow down with multiple enmies using LookAt

Hey guys and girls.

I'm coding a shoot 'em up; and with this I'm finding problems. My main problem is how to move an enemy, when there are 200+ enemies on the screen. It creates serious performance issues. I've optimized my scripts via the docs help; however once there are 100 or so enemies on screen the FPS drops, and for the most part my enemies (cubes at present) look like crap.

The first script, that is really causing a problem is my first enemy script, and thankfully this is the test script.

 #pragma strict
 static var target : Transform; //the enemy's target
 var target2 : Transform; //the enemy's target
 static var moveSpeed = 5; //move speed
 var rotationSpeed = 5; //speed of turning
 var myTransform : Transform; //current transform data of this enemy
 var distanceToWall = float; 
 var life : float =3;
 var boom : Transform;
 static var baseScore : float = 500;
 
 
 function Awake()
 
 {
     myTransform = transform; //cache transform data for easy access/preformance
 
 }
 
 function FixedUpdate(){
 
     target = GameObject.FindWithTag("Player").transform; //target the player
 
     transform.LookAt(Vector3(target.position.x, target.position.y, 0)); 
 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 }


Here's a screen grab of my Profiler:

alt text

I've never worked with so many enemies, and moving objects before.

The enemies are rigidbody, though they only need to recognize a collision between the player or a bullet.

I'm quite lost, as I've been using this kinda code to move objects for a while - and well, it's never been a problem, but then never on this scale.

Any help is greatly appreciated.

hmmm.png (185.8 kB)
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 voncarp · Sep 15, 2012 at 10:02 AM 1
Share

I don't think you are going to want to call GameObject.FindWithTag("Player").transform for 200 hundred enemies. Try removing that just to see if there is any performance improvement in the profiler. Your probably going to want to assign that in the inspector.

Also, perhaps you may want to try to look at the player every second or two ins$$anonymous$$d of a fixed update.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Sirex · Sep 15, 2012 at 10:13 AM

I have not worked with that problem but i can try to provide some optimisation methods.

  1. First you could cache GameObject.FindWithTag("Player") in Start.

  2. Second you could fetch player transform with 1 second intervall, maybe randomise so not all agents fetch at the same frame.

  3. Instead of making lookat every frame you could use the stored transform and make an angle calculation and decide on a treshold value, only when this threshold has been exceeded do you make a lookat proceduer.

  4. You could use a ghost class that list enemies according to their positions and calculate lookat and set it to several agents which could use roughly the same values.

  5. Data driven design, you have a ghost class that calculate the lookat for all the agents in its own update function. The rationale being that if you call the same code multiple times from the same place you will gain processing power seance the cpu don't have to calculate lookat one time then execute a lot of code to switch to anouther object and do the same thing.

Comment
Add comment · 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
0

Answer by DesiQ · Sep 15, 2012 at 10:15 AM

FindWithTag, GetComponent, and other variants that use strings to find a game object are extremely expensive. I found it funny that you were caching the enemy's transform 'for performance' (I see no reason to do so, since the transform will just change once the enemy moves around), but not caching the player's gameobject reference, which will never change. :p

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 voncarp · Sep 15, 2012 at 10:43 AM 0
Share

I suspect he is just caching the transform component for reference. Which unity recommends in the documentation, and not the actual transforms position.

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

12 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

Related Questions

Follow up to AI Pathfinding Question 0 Answers

Rotate character to the moving direction problems? 2 Answers

LookAt transform plus a y value 1 Answer

Semi-Quick question about "LookAt" script after respawn. 1 Answer

hoe to rotate a AI as a animation 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