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 lolzrofl · Dec 05, 2014 at 08:58 PM · c#physicsperformance

How could this code be optimized?

I am building a black hole gun in my game. Everything works perfectly, but it takes a pretty big hit on performance due to all the physics calculations.

Here is the script that calculates all the physics: http://pastebin.com/SGj8qxLJ

For reference, here is a gif of what it looks like: https://gfycat.com/BraveVerifiableArchaeocete

I know it's really bad to use FindObjectsOfType() every frame, but I can't think of any other way to make sure I'm always affecting every particle system.

If you would like more clarification on how any of the code works, please ask.

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

2 Replies

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

Answer by Kiwasi · Dec 06, 2014 at 01:06 AM

That's a huge amount of code to process every frame.

For better performance you want to use your heavy Find functions as rarely as possible. Maintain a list of Components of the type you are interested. Have the Component itself check in in OnEnable and out in OnDestroy. This will eliminate your FindObjectsOfType call.

Run this through the profiler to find which of your various sub methods costs the most. Optimise the worst performing method.

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 lolzrofl · Dec 06, 2014 at 03:09 AM 0
Share

I like your idea of having components check in and out. I'll try making an object that maintains lists of all active game objects so I can get rid of the find functions.

avatar image
1

Answer by uuil. · Dec 05, 2014 at 09:48 PM

I'm afraid I don't feel like reading that code (too spread out!). If you can paraphrase it that'd be nice ;)

I reckon there's too much code there. This seems like a pretty simple problem.

But to address your problems without reading it:

  1. If you're instantiating from one place, just store a reference to all the instantiated objects (or rigidbodies, see below) in the blackhole.

  2. I don't know if this still creates a performance hit.. but avoid referencing rigidbodies and transforms with .notation lookups. These used to be crazy slow. I'd try and store references in the blackhole directly to the rigidbody. You can use "rigidbody.worldCenterOfMass" to avoid transform lookups completely.

    // FOUR .notation COMPONENT LOOKUPS currentPowerup.rigidbody2D.velocity = Vector3.Lerp(currentPowerup.rigidbody2D.velocity, currentPowerup.transform.position.CalculateBlackHoleForce(outerForce, transform.position, outerRadius.radius, outerRotation), 0.5f);


I'd go for implementing something like this:

BLACKHOLE SCRIPT

  • Find reference to every physics component on creation. - Some physical constants. - Simple loop, applying force to every referenced RigidBody every tick.

    SPAWNER SCRIPTS

When spawning a physics object check for black holes and add the RigidBody (not the object) to the black holes lists.

Ideally you'd have one custom instantiate() command which would automatically check for black-holes.

This reduces lookups considerably, and keeps the code concise, simple, and re-usable.

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

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

27 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

Related Questions

[C#] Game runs oddly slow 2 Answers

Understanding Raycast How Actually works in Unity [As Algorithm] 2 Answers

Multiple Cars not working 1 Answer

Improve Script Performance Physics 2 Answers

Distribute terrain in zones 3 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