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 Miaw666 · Jun 18, 2021 at 02:43 PM · scripting beginnersimplify

Make code shorter/prettier?

Hello! I have a lot of codes I wrote that work just fine but I think they're not "optimized" or at least they look like the easy but long way around solving a problem.

Right now I have a slide script, it works, but it's not "good looking". Basically, I want a shorter and smarter way to write it. My code is below, I was wondering if there is a way to make it shorter. [cc is a Charactercontrller, Bobbing.camOffset is the cameras localPosition on the y-axis as a child of the player.]

Code is:

     void DoSlide()
     {
         if (cc.height > 1f)
         {
             cc.height -= Time.deltaTime * 2f * slideCrouchSpeed;
         }
         if (cc.center.y > -0.5f)
         {
             cc.center -= new Vector3(0f, 1f, 0f) * Time.deltaTime * slideCrouchSpeed;
         }
         if (Bobbing.camOffset > -0.15f)
         {
             Bobbing.camOffset -= Time.deltaTime * 2f * slideCrouchSpeed;
         }
 
         if (moveVector.magnitude > 0f) { movementSpeed = slidingSpeedThingy; }
         if (slidingSpeedThingy > walkingSpeed)
         {
             slidingSpeedThingy -= Time.deltaTime * slideSpeedFallOff;
         }
     }
     void StopSlide()
     {
         if (cc.height < 2f)
         {
             cc.height += Time.deltaTime * 2f * slideStandSpeed;
         }
         if (cc.center.y < 0f)
         {
             cc.center += new Vector3(0f, 1f, 0f) * Time.deltaTime * slideStandSpeed;
         }
         if (Bobbing.camOffset < 0.75f)
         {
             Bobbing.camOffset += Time.deltaTime * 2f * slideStandSpeed;
         }
 
         slidingSpeedThingy = slidingSpeed;
         movementSpeed = walkingSpeed;
     }

Thank you so much!

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

Answer by eneIr · Jun 18, 2021 at 02:50 PM

For shortening you script, instead of using new Vector3(0f, 1f, 0f), you can use Vector3.up, and your if() statements only have one line of code so you can remove the {} brackets. E.g:

 if (cc.height > 1f)
          {
              cc.height -= Time.deltaTime * 2f * slideCrouchSpeed;
          }

change to:

 if (cc.height > 1f)
          cc.height -= Time.deltaTime * 2f * slideCrouchSpeed;

For performance optimization, the moveVector.magnitude can be changed to moveVector.sqrMagnitude, I learned this from a tutorial video from Brackeys. That's all I can think of.

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 Miaw666 · Jun 18, 2021 at 05:33 PM 0
Share

If I remove the brackets, does it impact preformance? Also, sqrMagnitude doesn't return the same value as magnitude? I just tried it and it broke the script?

avatar image eneIr Miaw666 · Jun 19, 2021 at 09:54 AM 1
Share

It just makes the script shorter. And use the sqrMagnitude only when you compare it to 0 like in this part: "if (moveVector.magnitude > 0f)", if you use it to compare it to other positive numbers, just square those numbers. However, if you want to use the vector magnitude to move or something, just use the magnitude for simplicity.

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

134 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Cinemachine rotate behind player sooner when walking towards the camera 0 Answers

Emission blinking script 1 Answer

Memory game in 3d space 1 Answer

Tracking changes in velocity 1 Answer

What is the best way to Lerp on only one axis? 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