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 sonic220 · Dec 29, 2012 at 02:26 PM · android

Help for code optimization

Hi guys I need some help for optimize this come, usually this doesn't make problem on pc, but it makes the game framerate drop on android

 function OnTriggerStay (other : Collider) {
     if(other.gameObject.tag == "Player"){
             var player : GameObject = other.gameObject;    
         if(!found){
             var dir : Vector3 = player.transform.position - transform.position;
             transform.Translate(dir.normalized * Time.deltaTime * 15 * (1/dir.magnitude*2));
         }
         if(dir.magnitude < 1){
             if(!found)
                 player.SendMessage("AddEnergy",energy);
             found = true;
             gameObject.light.enabled = false;
                if(isPowerup){
                    particleSystem.loop = false;
                    other.GetComponent(playercontroller).bullet_speed += bullet_factor;
                    other.GetComponent(playercontroller).speed += speed_factor;
                    other.GetComponent(playercontroller).reload_speed -= reload_factor;
                }
                else
                {
                 gameObject.renderer.enabled = false;
                }
             yield new WaitForSeconds(2.5);
             Destroy(gameObject);
         }
         
     }
 }

I'm using this for making some energyballs that are attracted from the player, can you suggest me something to make this code faster?

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 deltamish · Dec 29, 2012 at 03:43 PM 0
Share

what is the percent of memmory consumed by your game

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by deltamish · Dec 29, 2012 at 03:53 PM

Hi,Why are you calling Getcomponent three times.This affects the performence slightly I have edited your script slightly.hopefully this should you

 var me:Transform;
 function Awake(){
 me = transform;///Cache transform so that unity doesn't have to check for it every frame
 }
 function OnTriggerStay (other : Collider) {
     if(other.gameObject.tag == "Player"){
            var player : GameObject;
 if(!player)
 player = other.gameObject;   
         if(!found){
            var dir : Vector3 = player.transform.position - transform.position;
            me.Translate(dir.normalized * Time.deltaTime * 15 * (1/dir.magnitude*2));
         }
         if(dir.magnitude < 1){
            if(!found)
              player.SendMessage("AddEnergy",energy);
            found = true;
            gameObject.light.enabled = false;
            if(isPowerup){
              particleSystem.loop = false;
        var otgt:playercontroller;
 if(!otgt)///call it only once 
 otgt =   other.GetComponent(playercontroller) as playercontroller;    
 otgt.bullet_speed += bullet_factor;
              otgt.speed += speed_factor;
              otgt.reload_speed -= reload_factor;
            }
            else
            {
              gameObject.renderer.enabled = false;
            }
            yield new WaitForSeconds(2.5);
            Destroy(gameObject);
         }
 
     }
 }
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 sonic220 · Dec 29, 2012 at 07:30 PM 0
Share

This helped but didn't solve the problem, still the game goes really slow somehow.

avatar image deltamish · Dec 31, 2012 at 01:45 AM 0
Share

do you have any script other than this.Are you using high poly models?. See the profiler it helps to know where the problem lies

avatar image
0

Answer by Ben Ezard · Dec 29, 2012 at 03:16 PM

Might be something to do with having "yield" in the function? Just use Destroy(gameObject, 2.5) instead - it does 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

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

10 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

Related Questions

Android Bundle Version and Bunde Version Code? 0 Answers

Creating a Game for IPhone/Android but testing it on a P.C. 1 Answer

How to move 2D textures? 0 Answers

Tilt control for jump 0 Answers

Android/iOS Thumbstick Code help 4 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