Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 darkhog · May 20, 2018 at 11:00 PM · movementoptimizationsine-wave

Game lags if there are many objects with this script, how to fix?

I'm really at loss here. The game seems to lag pretty much if there are many game objects with this script attached (the script is definitely the culprit here). "Many" is just under 50. But the script is just simple sinewave motion thing? No idea how to optimize it.

Here's the script in question:

 using UnityEngine;
 using System.Collections;
 
 public class BopUpDown : MonoBehaviour {
     public float upoffset;
     public float speed;
     private float defaulty;
     // Use this for initialization
     void Start () {
         defaulty = this.transform.localPosition.y;
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 currentpos = this.transform.localPosition;
         float newy = defaulty+((float)System.Math.Sin((Time.timeSinceLevelLoad*speed)+currentpos.x+currentpos.z)*upoffset);
         currentpos.y = newy;
         this.transform.localPosition = currentpos;
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by winterfluxstudio · May 20, 2018 at 11:30 PM

a few things

every object with that script adds 60 calls per second. so 60 x 50 = 3000 calls per second. (That's 180,000 per minute)

Additionally, to make it worse you are performing mathematical calculations.... 180,000 times per minute. (not even getting into the fact that you also compare time)

1) normally, you only put stuff like that in Update when it's unavoidable and for a critical function. Do you have a reason for doing this? would these functions not be better as coroutines that are called once per second (rather than 60 times a second)

2) Although it could be an obvious cause you should really use the profiler to determine the issue. You might be able to cutback on lighting (lights probes, reduced shadowmask, reduced mapping issues etc, or something else in order to improve performance. (if you have to keep the script "as is")

https://docs.unity3d.com/ScriptReference/Coroutine.html https://docs.unity3d.com/Manual/Coroutines.html

Comment
Add comment · Show 4 · 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 darkhog · May 20, 2018 at 11:38 PM 0
Share

1) I want the pickups bop up/down nicely and smoothly. Doing it once per second would miss the point entirely. 2) Actually it was the profile what "told" me that this script was the problem.

avatar image winterfluxstudio darkhog · May 20, 2018 at 11:46 PM 0
Share

(once per second was just an example.. you might have been able to try something like 10 times a second or something lower than 60, to strike a balance between smoothness and performace) What about if you animated them to get the same effect? that would remove the need for anything more than a simple script to set the animation and switch between two states (to get the floaty motion)

avatar image darkhog winterfluxstudio · May 20, 2018 at 11:49 PM 0
Share

In this case it wouldn't work. As you can see, the motion is based on x/z positions as well, which means that if you put a grid of these, you'll get nice waves. Not to mention it would be hard to get it animated without it looking either too loose or too stiff.

Show more comments

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

135 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

Related Questions

Movement cutting frames in half? 1 Answer

how to move between scenes 1 Answer

ECS design problem 0 Answers

progressive circular player movement 1 Answer

Best practice to make NavMeshAgent chase a moving target? 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