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 Macdude2 · Jun 24, 2011 at 12:52 AM · efficiencyfast

How to make code more efficient

Is there any possible way to optimize my code further? I am really aiming to squeeze out as much performance as possible.

A part of my code is below, but my entire code is this snippet about 20 times copied and pasted with different numbers (in one update function of course).

var other: Transform;

var other2: Transform;

 function Update () {    
     if(other2.transform.eulerAngles.y > -1 && vother2.transform.eulerAngles.y < 1){
         other.transform.localPosition.z = -(transform.position.z - other2.transform.position.z);
         other.transform.localPosition.x  = -(transform.position.x - other2.transform.position.x);
         other.transform.localPosition.y = transform.position.y - other2.transform.position.y;
     }
 }

Is there any faster way, in regards to speed, processing time, and cpu impact, to do this? The position and local positions of all my variables will be changing constantly, or else I would declare the variables in my start function which I know can speed things up a little.

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

3 Replies

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

Answer by Eric5h5 · Jun 24, 2011 at 01:35 AM

other and other2 are already transforms, so remove "transform" when referring to them. It would be faster to cache the transform of the current object.

 var other: Transform;
 var other2: Transform;
 private var myTransform : Transform;

 function Start () {  
     myTransform = transform;
 }
 
 function Update () {    
     if(other2.eulerAngles.y > -1 && other2.eulerAngles.y < 1){
         other.localPosition.z = -(myTransform.position.z - other2.position.z);
         other.localPosition.x  = -(myTransform.position.x - other2.position.x);
         other.localPosition.y = myTransform.position.y - other2.position.y;
     }
 }
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 Macdude2 · Jun 24, 2011 at 05:19 AM 0
Share

WOW!! This combined with answer 3 speeded up my game by more than 1ms/frame. Thanks a lot for your help.

avatar image
1

Answer by aldonaletto · Jun 24, 2011 at 01:34 AM

You can calculate the difference, save it in a Vector3 var, change what you have to and assign it to other.position:

 function Update () {    
 
     var angles2 = other2.eulerAngles;
     var pos1: Vector3;
 
     if (angles2.y>-1 && angles2.y<1){
         // calculate all differences at once in pos1
         pos1 = transform.position - other2.position;
         // change signal of the coordinates you want
         pos1.z = -pos1.z;
         pos1.x = -pos1.x;
         // assign the result to other.localPosition at once
         other.localPosition = pos1;
     }
 }
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
avatar image
0

Answer by Dreamblur · Jun 24, 2011 at 01:03 AM

I don't get why you're using other.transform and other2.transform when those variables already contain the actual Transform components that you're trying to access. other.transform IS other.

You need to cache all those positions and angles immediately after Update() starts.

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 Macdude2 · Jun 24, 2011 at 01:18 AM 0
Share

So it should be:

function Update(){ other.localPosition.x =otherx; other.localPosition.y =othery; other.localPosition.z =otherz;

  other2.position.y = other2y;
  other2.position.x = other2x;
  other2.position.z = other2z;

  transform.position.x = transx;
  transform.position.y = transy;
  transform.position.z = transz;

// code with all necessary changes here }

or should it be:

function Update(){

  other.localPosition =other2lp;

  other2.position = otherp;

  transform.position = transp;

//code with all necessary changes here }

Thanks for your help.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to achevie Shorter code less repetition 1 Answer

Script Efficiency 1 Answer

Source code for library function "RecalculateNormals()"? 4 Answers

Finding object via script 1 Answer

Respawning script question. 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