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
1
Question by Foemass · Apr 21, 2013 at 03:59 PM · collisioniphoneoptimizationlagpooling

Why do my first few collectable/powerup collisions cause lag but not later ones?

Hello, I’m having an issue with my iphone game where when the player object, (which uses a character controller) walks into a collectable/powerup it causes a very slight lag spike.

However, and here comes the weird part, this only seems to happen the first four times or so, after which the collectables behave very smoothly, I’d like to know why this is happening.

Heres the script I’m using:

 #pragma strict 
 
 var supplyValue:int = 1;
 internal var parentRigidbody: Rigidbody;
 internal var parentTransform: Transform;
 
 function Awake ()
 {
     parentRigidbody = this.transform.parent.rigidbody;
     parentTransform = this.transform.parent.transform;
 }
 
 function OnSpawned () 
 {
     parentRigidbody.isKinematic = false;
     countdownTillCleanup();  
     countdownTillPhysicsOff();
     parentRigidbody.AddForce(-Vector3.up * 400);  
 }
 
 function countdownTillCleanup()
 {
     yield WaitForSeconds(30);
     PoolManager.Pools["Consumables"].Despawn(parentTransform);
 }
 
 function countdownTillPhysicsOff()
 {
     yield WaitForSeconds(3); 
     parentRigidbody.isKinematic = true;
 }
 
 function OnTriggerEnter(col : Collider)
 {
     if (col.tag == "player")
     { 
         col.SendMessageUpwards("IncreaseSupplies", supplyValue, SendMessageOptions.DontRequireReceiver);
         PoolManager.Pools["Consumables"].Despawn(parentTransform);
     }
 
 }

The script uses poolmanager 4 to instance the collectables, twenty of which are pre spawned at the start of play, so this lag can’t be due to collectables being initiated or destroyed.

It may also be worth noting that the collectable gameobject consists of a mesh render, a rigidbody and a box collider. Then a child gameobject consisting of a much larger trigger box collider and the above script.

The player object meanwhile consists of a character controller, listener, then a bunch of unrelated scripts and numerous child objects none of which have the player tag or colliders of their own.

To clarify the lag spikes only occur on the iphone when the player object collides with a collectable, and only the first four or so times such collisions occur.

I’ve been trying all kinds of things for some time to try and resolve this without any luck and I’m really hoping one of you has encountered something like this before, i'd be very much grateful for any responses though.

Comment
Add comment · Show 3
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 DESTRUKTORR · Apr 21, 2013 at 06:52 PM 0
Share

Sending messages tends to be highly inefficient, especially for iOS. How many times is this called, the first time?

avatar image Foemass · Apr 21, 2013 at 07:38 PM 0
Share

Thanks for the reply!

I tried replacing the send message with a direct reference to the script being messaged and it had no effect on the lag. Though that reference did do a getComponent(script) in the trigger portion of the script, which I suppose might have been a little processor intense itself. Can you think of a better approach to try? The script which I want to activate a function on is a component of the player/col object.

As for how many times its called the first time... i'm not entirely certain what you mean. The if (col == player) portion is called only once for each collision, so its called once during the first collision.

avatar image Foemass · Apr 22, 2013 at 02:11 PM 0
Share

A little update on this, i've manage to track the source of the lag down to the actual act of displaying the supplies variable (which the send message changes) within the GUI, as removing it from the onGUI removes the lag associated with running into a collectable. However I still haven't been able to figure out why displaying this string based number causes lag when its updated, whilst a practically identically functioning countdown variable I'm displaying with the GUI doesn't cause lag.

1 Reply

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

Answer by Foemass · Apr 22, 2013 at 05:41 PM

SOLVED IT! Alright, for the sake of anyone with a similar problem who stumbles upon this later, the lag spikes for the first few collisions were the fault of me displaying the variable being changed as a result of the collision using a font with Character set to dynamic. This seemingly meant that the first time it showed any one character it needed a moment to create a texture for it on the fly, thusly the first few times my player object picked up a collectable it had to create the textures for numbers 1 - 9, which in the case of a few of them, resulted in lag spikes. Changing the font character to ASCII has resolved the problem since all textures for fonts for that variable displaying GUI are now generated beforehand.

Comment
Add comment · Show 2 · 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 whydoidoit · Apr 22, 2013 at 05:45 PM 0
Share

Converted your comment to an answer (as it is one!)

Good to know that - thanks....

avatar image Jovy83 · May 11, 2015 at 08:20 AM 0
Share

I've been having this issue with my current game as well. Setting it to ASCII removed the darn sudden fps drop which is critical for gameplay.

Thanks alot!

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

13 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

Related Questions

iOS. Jerks in first 15-20 seconds after starting scene 0 Answers

Is this code inefficient? It is causing Lag. 1 Answer

How to combine sprites to create a seamless and efficient level 0 Answers

Game works on old iPhones, less than 6s plus. Player movement lags bad on 6s or greater. 1 Answer

Can someone explain SendMessage to me? 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