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 dendens2 · Apr 03, 2013 at 09:02 AM · javascriptmathfclamp

Mathf.Clamp is not Clamping.

Ok, So I have a crosshair that gets bigger, as the gun becomes less accurate. I want it to stop at the lowest accuracy (highest value). I first tried an if statement to check if the current accuracy was greater than the lowest accuracy, and then changed the value, but that ended up being jerky. I got the idea to just use clamping, but it is not working and I can't understand why. The current accuracy's value just keeps rising past the lowest accuracy.

CODE:

 //Update Loop
 
 Mathf.Clamp(currentAccuracy, highestAccuracy, lowestAccuracy);
 
 if (currentAccuracy >= highestAccuracy && firing == false)
     {
             
         currentAccuracy -= Time.deltaTime * (speedAccuracyIncrease);
             
     }
 
 
 if(Input.GetButtonDown("Fire1") && autoFire == false || Input.GetButton("Fire1") && autoFire == true)
     {
         if(fireDelay <= 0 && ammoClip > 0)
         {
             firing = true;
         
             if (bullet)
             {
             
                 currentAccuracy += accuracyDecrease;

                     }
                    else
                     {
     
                         firing = false;
     
                      }


P.S

lowestAccuracy has the highest value because it is the least accurate and has the most effect on the raycast's rotation. Same goes for highestAccuracy.

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 dendens2 · Apr 03, 2013 at 09:03 AM 0
Share

Also, sorry for the weirdly typed code, I removed a lot of it in the editor so you only see what is important and I kind of screwed it up.

1 Reply

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

Answer by EliteMossy · Apr 03, 2013 at 09:11 AM

Clamp works on the Lowest value to highest value, so assuming your code:

Mathf.Clamp(currentAccuracy, lowestAccuracy, highestAccuracy);

Also you need to assign it to a variable, like:

accuracy = Mathf.Clamp(currentAccuracy, lowestAccuracy, highestAccuracy);

And the accuracy will contain your clamped accuracy.

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 LyanApps · Apr 03, 2013 at 02:16 PM 1
Share

Also you need to assign it to a variable

Definitely

avatar image dendens2 · Apr 03, 2013 at 06:09 PM 0
Share

Right, I forgot to assign it to a variable, thanks! Also lowestAccuracy means it is that least accurate. To make the gun less accurate I have to have a HIGHER value to affect the angle randomization of the raycast. That is why I have it as the max. Thanks!

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

12 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

Related Questions

Implementing mathf ? 1 Answer

Modify distance with var? 1 Answer

limit accelerometer controlled rotation 1 Answer

How to use Mathf.Clamp with child objects? 0 Answers

Clamping vertical position help 2 Answers


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