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 asimov · Aug 02, 2012 at 04:09 PM · optimizationitweenfadevector3.distance

How to best optimise this script

Hi,

I have written a script which is used to fade out enemy objects that come within a specified distance from the camera. The script is to be used for iOS devices and I was wondering if anyone can recommend how I could optimise it to make it as light as possible on performance. It uses iTween.FadeTo() to fade the object.

I know that the distance check has to be run every frame and so must be inside the Update() function, but are there other areas I can improve on? The script is attached to the MainCamera.

Any help would be great.

//---

    // Variable to hold the distance between the camera and the object
     private float Dist;
 
     // The distance at which the object will begin to fade
     public float FadeDistance;
 
     // The array of GameObjects (Enemies) in the scene that we want to be faded
     public GameObject[] Enemies;
 
     void Start()
     {
         // Find all of the object with tag "Enemy" in the scene and
         // copy them into the Enemies array
         Enemies = GameObject.FindGameObjectsWithTag("Enemy");
     }
 
     void Update()
     {
          foreach (GameObject enemy in Enemies)
          {
             // If the element is null (false) the enemy is dead, so we ignore them
             if (enemy) 
             {
                 // Find the distance between the Camera and the enemy getting its 
                 // Absolute value (positive only)
                 Dist = Mathf.Abs(transform.position.z - enemy.transform.position.z);
 
                 // if the object has tag "enemy" AND is closer than our fade distance..
                 if (enemy.collider.tag == "Enemy" && Dist <= FadeDistance)
                 {
                     // Fade the object (level of alpha (0 = transparent), time to take to fade)
                     iTween.FadeTo(enemy, 0.0f, 0.2f);
 
                     // Destroy the enemy after 1 second
                     Destroy(enemy, 1);
                 }
             }
         }
     }
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
1
Best Answer

Answer by FlyingOstriche · Aug 02, 2012 at 06:36 PM

Would it be faster to create a large sphere collider with FadeDistance radius around the camera. Then use 'is-trigger' to calculate when the enemy gets in fade range. (The enemies will go through the sphere collider if 'is-trigger' is on)

Then set it so that the Sphere collider only collides with the enemy layer.

Unity's collision code should be pretty optimized.

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 Fattie · Aug 02, 2012 at 07:48 PM 0
Share

This is exactly correct.

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

11 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

Related Questions

how to move or fade GUITextures with iTween 2 Answers

Fading out alpha on a 3d object with iTween 0 Answers

Optimization - Distance vs Collisions 3 Answers

Fade in object using iTween 0 Answers

iTween :: fading out an empty with Children 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