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 /
  • Help Room /
avatar image
0
Question by PRO_future · Jan 01, 2016 at 04:24 PM · tutorialtank

Identical code presenting different results

I have been following through the Tanks! tutorial and seem to have hit a snag. Below are two blocks of code, which through all tools I have used appear as though they should compile identically. However, the first does NOT update the =_AimSlider.value but the second does... If I copy and paste JUST the Update function from 2 to 1, it works... the second example is the pre-completed version so I REALLY want to know what I have done wrong. I THINK it is an underlying linespace issue but I get no warnings and can't fix it :/.

  1. My version

    private void Update() { // Track the current state of the fire button and make decisions based on the current launch force. m_AimSlider.value = m_MinLaunchForce; // Set slider value to minimum possible initially.

      if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired) // At max charge, not yet fired...
         {
             m_CurrentLaunchForce = m_MaxLaunchForce; // So we set the launch force to the maximum so it cannot be over max charge.
             Fire(); // Then call the fire function to fire the shell.
         }
    
         else if (Input.GetButtonDown(m_FireButton)) // Have we pressed fire for the first time?
         {
             m_Fired = false; // We know the button has just been pressed to we have not fired yet.
             m_CurrentLaunchForce = m_MinLaunchForce; // We also know that the launch force will (or should) be at it's minimum value, so we set that as well.
    
             m_ShootingAudio.clip = m_ChargingClip; // Sets the current audio clip to play, as the charging clip as we have just started holding shoot (hence charging a shot).
             m_ShootingAudio.Play(); // Play the charging sound.
         }
    
         else if (Input.GetButtonDown(m_FireButton) && !m_Fired) // Holding the fire button but not yet fired.
         {
             m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime; // Set the current launch force to equal it's current value plus charge speed * time held.
    
             m_AimSlider.value = m_CurrentLaunchForce; // Update the arrow shape/size to match the current launch force.
         }
    
         else if (Input.GetButtonUp(m_FireButton) && !m_Fired) // We released the button, having not fired yet.
         {
             Fire(); // Fire button has been released so we simply call the fire function.
         }
     }
    
    
  2. Example version

private void Update () { // The slider should have a default value of the minimum launch force. m_AimSlider.value = m_MinLaunchForce;

         // If the max force has been exceeded and the shell hasn't yet been launched...
         if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
         {
             // ... use the max force and launch the shell.
             m_CurrentLaunchForce = m_MaxLaunchForce;
             Fire ();
         }
         // Otherwise, if the fire button has just started being pressed...
         else if (Input.GetButtonDown (m_FireButton))
         {
             // ... reset the fired flag and reset the launch force.
             m_Fired = false;
             m_CurrentLaunchForce = m_MinLaunchForce;

             // Change the clip to the charging clip and start it playing.
             m_ShootingAudio.clip = m_ChargingClip;
             m_ShootingAudio.Play ();
         }
         // Otherwise, if the fire button is being held and the shell hasn't been launched yet...
         else if (Input.GetButton (m_FireButton) && !m_Fired)
         {
             // Increment the launch force and update the slider.
             m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;

             m_AimSlider.value = m_CurrentLaunchForce;
         }
         // Otherwise, if the fire button is released and the shell hasn't been launched yet...
         else if (Input.GetButtonUp (m_FireButton) && !m_Fired)
         {
             // ... launch the shell.
             Fire ();
         }
     }
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
0
Best Answer

Answer by jmonasterio · Jan 01, 2016 at 05:16 PM

Code is not the same in both.

In the first one, line 16 calls GetButtonDown()

But in the second one, line 20 calls GetButton()

You could generally resolve this issue yourself by using a program called a DIFF utility. A good example for windows is WinDiff, but there are many free and available for purchase.

Comment
Add comment · Show 3 · 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 PRO_future · Jan 06, 2016 at 06:00 PM 1
Share

Believe it or not I did run this through WIN$$anonymous$$erge. Perhaps I wasn't using it properly or WinDiff is better for this as Win$$anonymous$$erge returned no differences between the files. This was a great lesson in the obscurity of debugging (certainly for a beginner) and how important it is to know how to use the correct tools.

Thank you so much for your help. :)

avatar image jmonasterio PRO_future · Jan 06, 2016 at 08:32 PM 0
Share

No problem. Here is a related tip: I'd avoid comments on the end of lines like...

      Fire(); // Then call the fire function to fire the shell.

If you always put comments on a separate line, your diffs will be easier to read (especially if you just changed comment, and didn't change code on that line).

The "original" code didn't have those comments, but it looks like you added comments to help you understand (which can be helpful). Just don't do end of line comments and you'd have quickly seen which lines of code changed.

Or (second best) get a diff tool that can ignore comments and whitespace.

avatar image PRO_future jmonasterio · Jan 07, 2016 at 12:59 PM 0
Share

Great advice. Thanks so much, I'm always happy to take on board any experience no matter how small.

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

38 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

Related Questions

Tanks Tutorial Image separating 0 Answers

problem with tank tutorial 1 Answer

OverslapSphere dont detect enemyhealth 0 Answers

How to create a Cookie Clicker (idle) game clone? 1 Answer

Error CS0246 in GameManager (Roguelike 2D 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