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 asdf123 · Jul 17, 2014 at 06:44 PM · updateoncedivision

Divide a number once in update

This is my Aim Down Sights code, and i want when i ads my recoil divide by 3. But it divide by 3 every frame until it reach 0. what i want is: if recoil is 9 it stay 3 on button fire2 pressed

the AdsController function is called on Update

someone can help?

 void AdsController() {
             Vector3 adsRecoilKickBack = currentWeapon.recoilKickBack / 3;
             Vector3 adsRecoilRotation = currentWeapon.recoilRotation / 3;
     
             if(Input.GetButton("Fire2")) {
                 isAiming = true;
                 AdsAnimHolder.transform.localPosition = Vector3.Lerp (AdsAnimHolder.transform.localPosition, currentWeapon.currentSight.adsPos, 0.25f);
                 currentWeapon.recoilKickBack = adsRecoilKickBack;
                 currentWeapon.recoilRotation = adsRecoilRotation;
             }
             else {
                 isAiming = false;
                 AdsAnimHolder.transform.localPosition = Vector3.Lerp (AdsAnimHolder.transform.localPosition, Vector3.zero, 0.25f);
             }
         }









Comment
Add comment · Show 2
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 Huacanacha · Jul 17, 2014 at 07:21 PM 0
Share

If you only want something changed if "Fire2" is pressed then only change it if "Fire2" is pressed, i.e. within the IF clause code block.

If you only want to change the recoil if the current magnitude of the recoil is approximately 9 then just add another IF to check for this:

 if ($$anonymous$$athf.Approximately(9, currentWeapon.recoil$$anonymous$$ickBack.magnitude)) {
     // Change recoil here
 }

You probably don't want to store the temporarily adjusted recoil on the currentWeapon itself, otherwise you have to remember to store the original value and always set it back (not to mention that the weapons internal state should only be managed by the weapon, not a third party script). If the weapon will always have 1/3 recoil when firing (or whatever you are doing) then maybe set and unset a boolean flag on the weapon script that will adjust the recoil... something like:

 public bool IsFiring = false;
 
 private Vector3 recoil$$anonymous$$ickBack;
 public Vector3 Recoil$$anonymous$$ickBack {
     get {
         if (IsFiring)
             return recoil$$anonymous$$ickBack * recoilReduction;
         else
             return recoil$$anonymous$$ickBack;
     }
 }

 float recoilReduction = 1f/3;
 

This way the weapon script itself manages it's state and properties which is much better (low coupling) design.

avatar image asdf123 · Jul 18, 2014 at 03:25 AM 0
Share

Thanks for your answer, but isnt what im looking for :C

What i want is to recoil$$anonymous$$ickback divide per 3 and stay divided per 3

what hapens is that recoilkickback divide per 3 each frame until it reaches 0

1 Reply

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

Answer by smallbit · Jul 18, 2014 at 03:43 AM

You need to store your variables out of Update loop ( and methods called there too). Declare your variables globally :

  Vector3 adsRecoilKickBack;
  Vector3 adsRecoilRotation;

Than after you switch weapon adjust their values according to current weapon parameters; Call this method when you switch weapon:

   void UpdateWepData()
     {
         adsRecoilKickBack = currentWeapon.recoilKickBack / 3;
         adsRecoilRotation = currentWeapon.recoilRotation / 3;
     }

Finally remove two lines from your AdsControllerMethod;

 void AdsController()
     {
         
 
         if (Input.GetButton("Fire2"))
         {
             isAiming = true;
             AdsAnimHolder.transform.localPosition = Vector3.Lerp(AdsAnimHolder.transform.localPosition, currentWeapon.currentSight.adsPos, 0.25f);
             currentWeapon.recoilKickBack = adsRecoilKickBack;
             currentWeapon.recoilRotation = adsRecoilRotation;
         }
         else
         {
             isAiming = false;
             AdsAnimHolder.transform.localPosition = Vector3.Lerp(AdsAnimHolder.transform.localPosition, Vector3.zero, 0.25f);
         }
     }



Should work that way.

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

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

24 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

Related Questions

Update() Function only fires once. 1 Answer

Check if key is pressed once 1 Answer

How to run a update function once 3 Answers

Calling a function only once in Update 6 Answers

Call A Function At A Certain Time 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