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 Essential · Dec 13, 2011 at 10:44 PM · functiontutorial2d platformer

Is there a good reason the Unity 2D platformer tutorial code is like this?

In the Lerpz 2D platforming tutorial there is a PlatformerController script that calculates movement and jumping. A variable inside the ApplyJumping function calls to another function to get the result…

 function ApplyJumping () {
     movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height);
 }
 
 
 function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
     return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
 }



I'm trying to learn as many efficient coding techniques as I can so I'm wondering if there's a reason the programmer chose to put this into its own function? Why didn't he just write it as:

 function ApplyJumping () {
     movement.verticalSpeed = Mathf.Sqrt (2 * jump.height * movement.gravity);
 }


…because to me, this seems more efficient. Why did he create a whole function to call to? Was it unnecessary or is there a reason for it? The CalculateJumpVerticalSpeed function is not called anywhere else in the project.

//edit: might sound like I'm criticizing but I'm not! Just trying to learn. :)

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 Lo0NuhtiK · Dec 13, 2011 at 10:53 PM 0
Share

did you try commenting out the original code and putting your version into play to see if it made any kind of difference?

1 Reply

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

Answer by syclamoth · Dec 13, 2011 at 11:01 PM

It doesn't make any difference. The reason is good programming technique. While the function is currently not called anywhere else in the project, it might be at some point in the future- it's always best to split this kind of thing down into small, modular operations so that it's easy to see exactly what parts do what. For example, you might want to make some kind of 'jump height calculator' (for AI use, or to give the player a feel for how high they will jump) which uses the same function. Instead of copy-pasting the code from the jumping part, just make another call to the 'calculate jump height' function! This way, if you make a change to one, the change propagates to the other without you having to do any work- since they are both using the same line of code.

Comment
Add comment · Show 11 · 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 Lo0NuhtiK · Dec 13, 2011 at 11:52 PM 1
Share

comment it out, use yours, and see if it has an effect on performance or if it all runs the same.

avatar image Statement · Dec 14, 2011 at 12:54 AM 2
Share

Yes, it's a small overhead but the code is much cleaner and easier to follow. And since it's a tutorial, the code should be easy to follow and not contain a lot of "WTF" moments for the reader. I regulary break down code into small methods like that.

WTF

I just realized the audience might be going "WTF" for different reasons.

avatar image Statement · Dec 14, 2011 at 01:00 AM 1
Share

@syclamoth, apparently it doesn't inline methods that return float.

avatar image syclamoth · Dec 14, 2011 at 01:53 AM 1
Share

The number is the amount of thumbs up that post has been given. It's a small measure of how much people like the comment.

avatar image DanSuper · Dec 12, 2012 at 06:40 PM 1
Share

Whether the compiler optimizes it out or not the amount of overhead is negligible. It's really unlikely that you will ever have that amount of optimization make a difference in the performance of your system working in Unity.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Call function from other script 0 Answers

Where can i find tutorial for unity free Not unity pro ? 2 Answers

Saving data. 1 Answer

Multiplayer FPS Tutorial 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